Facebook
From melody, 10 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 118
  1. Case Statements:
  2.  
  3. sql
  4. Copy code
  5. SELECT column1, column2,
  6.   CASE
  7.     WHEN condition1 THEN result1
  8.     WHEN condition2 THEN result2
  9.     ELSE result3
  10.   END AS alias
  11. FROM table_name;
  12. Subqueries:
  13.  
  14. sql
  15. Copy code
  16. SELECT column1
  17. FROM table1
  18. WHERE column2 IN (SELECT column3 FROM table2 WHERE condition);
  19. Keys:
  20.  
  21. Primary Key:
  22.  
  23. sql
  24. Copy code
  25. CREATE TABLE table_name (
  26.   id INT PRIMARY KEY,
  27.   column1 datatype,
  28.   column2 datatype,
  29.   ...
  30. );
  31. Foreign Key:
  32.  
  33. sql
  34. Copy code
  35. CREATE TABLE table_name (
  36.   id INT PRIMARY KEY,
  37.   foreign_id INT,
  38.   column1 datatype,
  39.   column2 datatype,
  40.   FOREIGN KEY (foreign_id) REFERENCES other_table(id)
  41. );
  42. Wildcards:
  43.  
  44. sql
  45. Copy code
  46. SELECT column1
  47. FROM table_name
  48. WHERE column2 LIKE 'abc%'; Case Statements:
  49.  
  50. sql
  51. Copy code
  52. SELECT column1, column2,
  53.   CASE
  54.     WHEN condition1 THEN result1
  55.     WHEN condition2 THEN result2
  56.     ELSE result3
  57.   END AS alias
  58. FROM table_name;
  59. Subqueries:
  60.  
  61. sql
  62. Copy code
  63. SELECT column1
  64. FROM table1
  65. WHERE column2 IN (SELECT column3 FROM table2 WHERE condition);
  66. Keys:
  67.  
  68. Primary Key:
  69.  
  70. sql
  71. Copy code
  72. CREATE TABLE table_name (
  73.   id INT PRIMARY KEY,
  74.   column1 datatype,
  75.   column2 datatype,
  76.   ...
  77. );
  78. Foreign Key:
  79.  
  80. sql
  81. Copy code
  82. CREATE TABLE table_name (
  83.   id INT PRIMARY KEY,
  84.   foreign_id INT,
  85.   column1 datatype,
  86.   column2 datatype,
  87.   FOREIGN KEY (foreign_id) REFERENCES other_table(id)
  88. );
  89. Wildcards:
  90.  
  91. sql
  92. Copy code
  93. SELECT column1
  94. FROM table_name
  95. WHERE column2 LIKE 'abc%'
  96.  
  97.  
  98. Certainly! Here's an example of performing the ORDER BY clause on two columns in the EMPLOYEE table:
  99.  
  100. sql
  101. Copy code
  102. SELECT empId, name, dept, grade
  103. FROM EMPLOYEE
  104. ORDER BY grade DESC, name ASC;
  105. In this example, the ORDER BY clause is applied to the grade column in descending order (DESC) first. Then, if there are multiple rows with the same grade, the name column is used to sort those rows in ascending order (ASC).
  106.  
  107. The result will be a sorted result set where the rows are arranged based on the grade in descending order. Within each grade, the rows will be further sorted based on the name in ascending order.
  108.  
  109. You can modify the column names and the sort order based on your specific requirements. Simply specify the desired columns in the ORDER BY clause and indicate whether you want them sorted in ascending (ASC) or descending (DESC) order.
  110.  
  111. select avg(grade) from EMPLOYEE
  112. select avg(grade) from EMPLOYEE where dept='Accounting'
  113. select * from EMPLOYEE where grade<(select avg(grade) from EMPLOYEE where dept='Accounting')
  114.  
  115.  
  116.  
  117. Certainly! Here's an example of performing the ORDER BY clause on two columns in the EMPLOYEE table:
  118.  
  119. sql
  120. Copy code
  121. SELECT empId, name, dept, grade
  122. FROM EMPLOYEE
  123. ORDER BY grade DESC, name ASC;
  124. In this example, the ORDER BY clause is applied to the grade column in descending order (DESC) first. Then, if there are multiple rows with the same grade, the name column is used to sort those rows in ascending order (ASC).
  125.  
  126. The result will be a sorted result set where the rows are arranged based on the grade in descending order. Within each grade, the rows will be further sorted based on the name in ascending order.
  127.  
  128. You can modify the column names and the sort order based on your specific requirements. Simply specify the desired columns in the ORDER BY clause and indicate whether you want them sorted in ascending (ASC) or descending (DESC) order.