1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# Quick SQL Cheatsheet
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#quick-sql-cheatsheet)
A quick reminder of all relevant SQL queries and examples on how to use them.
This repository is constantly being updated and added to by the community. Pull requests are welcome. Enjoy!
# Table of Contents
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#table-of-contents)
1. [Finding Data Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#find)
2. [Data Modification Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#modify)
3. [Reporting Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#report)
4. [Join Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#joins)
5. [View Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#view)
6. [Altering Table Queries.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#alter)
7. [Creating Table Query.](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#create)
# 1. Finding Data Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#1-finding-data-queries)
### **SELECT**: used to select data from a database
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#select-used-to-select-data-from-a-database)
- `SELECT` * `FROM` table_name;
### **DISTINCT**: filters away duplicate values and returns rows of specified column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#distinct-filters-away-duplicate-values-and-returns-rows-of-specified-column)
- `SELECT DISTINCT` column_name;
### **WHERE**: used to filter records/rows
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#where-used-to-filter-recordsrows)
- `SELECT` column1, column2 `FROM` table_name `WHERE` condition;
- `SELECT` * `FROM` table_name `WHERE` condition1 `AND` condition2;
- `SELECT` * `FROM` table_name `WHERE` condition1 `OR` condition2;
- `SELECT` * `FROM` table_name `WHERE NOT` condition;
- `SELECT` * `FROM` table_name `WHERE` condition1 `AND` (condition2 `OR` condition3);
- `SELECT` * `FROM` table_name `WHERE EXISTS` (`SELECT` column_name `FROM` table_name `WHERE` condition);
### **ORDER BY**: used to sort the result-set in ascending or descending order
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#order-by-used-to-sort-the-result-set-in-ascending-or-descending-order)
- `SELECT` * `FROM` table_name `ORDER BY` column;
- `SELECT` * `FROM` table_name `ORDER BY` column `DESC`;
- `SELECT` * `FROM` table_name `ORDER BY` column1 `ASC`, column2 `DESC`;
### **SELECT TOP**: used to specify the number of records to return from top of table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#select-top-used-to-specify-the-number-of-records-to-return-from-top-of-table)
- `SELECT TOP` number columns_names `FROM` table_name `WHERE` condition;
- `SELECT TOP` percent columns_names `FROM` table_name `WHERE` condition;
- Not all database systems support `SELECT TOP`. The MySQL equivalent is the `LIMIT` clause
- `SELECT` column_names `FROM` table_name `LIMIT` offset, count;
### **LIKE**: operator used in a WHERE clause to search for a specific pattern in a column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#like-operator-used-in-a-where-clause-to-search-for-a-specific-pattern-in-a-column)
- % (percent sign) is a wildcard character that represents zero, one, or multiple characters
- _ (underscore) is a wildcard character that represents a single character
- `SELECT` column_names `FROM` table_name `WHERE` column_name `LIKE` pattern;
- `LIKE` ‘a%’ (find any values that start with “a”)
- `LIKE` ‘%a’ (find any values that end with “a”)
- `LIKE` ‘%or%’ (find any values that have “or” in any position)
- `LIKE` ‘_r%’ (find any values that have “r” in the second position)
- `LIKE` ‘a_%_%’ (find any values that start with “a” and are at least 3 characters in length)
- `LIKE` ‘[a-c]%’ (find any values starting with “a”, “b”, or “c”
### **IN**: operator that allows you to specify multiple values in a WHERE clause
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#in-operator-that-allows-you-to-specify-multiple-values-in-a-where-clause)
- essentially the IN operator is shorthand for multiple OR conditions
- `SELECT` column_names `FROM` table_name `WHERE` column_name `IN` (value1, value2, …);
- `SELECT` column_names `FROM` table_name `WHERE` column_name `IN` (`SELECT STATEMENT`);
### **BETWEEN**: operator selects values within a given range inclusive
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#between-operator-selects-values-within-a-given-range-inclusive)
- `SELECT` column_names `FROM` table_name `WHERE` column_name `BETWEEN` value1 `AND` value2;
- `SELECT` * `FROM` Products `WHERE` (column_name `BETWEEN` value1 `AND` value2) `AND NOT` column_name2 `IN` (value3, value4);
- `SELECT` * `FROM` Products `WHERE` column_name `BETWEEN` #01/07/1999# AND #03/12/1999#;
### **NULL**: values in a field with no value
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#null-values-in-a-field-with-no-value)
- `SELECT` * `FROM` table_name `WHERE` column_name `IS NULL`;
- `SELECT` * `FROM` table_name `WHERE` column_name `IS NOT NULL`;
### **AS**: aliases are used to assign a temporary name to a table or column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#as-aliases-are-used-to-assign-a-temporary-name-to-a-table-or-column)
- `SELECT` column_name `AS` alias_name `FROM` table_name;
- `SELECT` column_name `FROM` table_name `AS` alias_name;
- `SELECT` column_name `AS` alias_name1, column_name2 `AS` alias_name2;
- `SELECT` column_name1, column_name2 + ‘, ‘ + column_name3 `AS` alias_name;
### **UNION**: set operator used to combine the result-set of two or more SELECT statements
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#union-set-operator-used-to-combine-the-result-set-of-two-or-more-select-statements)
- Each SELECT statement within UNION must have the same number of columns
- The columns must have similar data types
- The columns in each SELECT statement must also be in the same order
- `SELECT` columns_names `FROM` table1 `UNION SELECT` column_name `FROM` table2;
- `UNION` operator only selects distinct values, `UNION ALL` will allow duplicates
### **INTERSECT**: set operator which is used to return the records that two SELECT statements have in common
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#intersect-set-operator-which-is-used-to-return-the-records-that-two-select-statements-have-in-common)
- Generally used the same way as **UNION** above
- `SELECT` columns_names `FROM` table1 `INTERSECT SELECT` column_name `FROM` table2;
### **EXCEPT**: set operator used to return all the records in the first SELECT statement that are not found in the second SELECT statement
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#except-set-operator-used-to-return-all-the-records-in-the-first-select-statement-that-are-not-found-in-the-second-select-statement)
- Generally used the same way as **UNION** above
- `SELECT` columns_names `FROM` table1 `EXCEPT SELECT` column_name `FROM` table2;
### **ANY|ALL**: operator used to check subquery conditions used within a WHERE or HAVING clauses
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#anyall-operator-used-to-check-subquery-conditions-used-within-a-where-or-having-clauses)
- The `ANY` operator returns true if any subquery values meet the condition
- The `ALL` operator returns true if all subquery values meet the condition
- `SELECT` columns_names `FROM` table1 `WHERE` column_name operator (`ANY`|`ALL`) (`SELECT` column_name `FROM` table_name `WHERE` condition);
### **GROUP BY**: statement often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#group-by-statement-often-used-with-aggregate-functions-count-max-min-sum-avg-to-group-the-result-set-by-one-or-more-columns)
- `SELECT` column_name1, COUNT(column_name2) `FROM` table_name `WHERE` condition `GROUP BY` column_name1 `ORDER BY` COUNT(column_name2) DESC;
### **HAVING**: this clause was added to SQL because the WHERE keyword could not be used with aggregate functions
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#having-this-clause-was-added-to-sql-because-the-where-keyword-could-not-be-used-with-aggregate-functions)
- `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING` `COUNT(`column_name1`)` > 5;
### **WITH**: often used for retrieving hierarchical data or re-using temp result set several times in a query. Also referred to as "Common Table Expression"
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#with-often-used-for-retrieving-hierarchical-data-or-re-using-temp-result-set-several-times-in-a-query-also-referred-to-as-common-table-expression)
- `WITH RECURSIVE` cte `AS` (
`SELECT` c0.* `FROM` categories `AS` c0 `WHERE` id = 1 `# Starting point`
`UNION ALL`
`SELECT` c1.* `FROM` categories `AS` c1 `JOIN` cte `ON` c1.parent_category_id = cte.id
)
`SELECT` *
`FROM` cte
# 2. Data Modification Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#2-data-modification-queries)
### **INSERT INTO**: used to insert new records/rows in a table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#insert-into-used-to-insert-new-recordsrows-in-a-table)
- `INSERT INTO` table_name (column1, column2) `VALUES` (value1, value2);
- `INSERT INTO` table_name `VALUES` (value1, value2 …);
### **UPDATE**: used to modify the existing records in a table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#update-used-to-modify-the-existing-records-in-a-table)
- `UPDATE` table_name `SET` column1 = value1, column2 = value2 `WHERE` condition;
- `UPDATE` table_name `SET` column_name = value;
### **DELETE**: used to delete existing records/rows in a table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#delete-used-to-delete-existing-recordsrows-in-a-table)
- `DELETE FROM` table_name `WHERE` condition;
- `DELETE` * `FROM` table_name;
# 3. Reporting Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#3-reporting-queries)
### **COUNT**: returns the # of occurrences
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#count-returns-the--of-occurrences)
- `SELECT COUNT (DISTINCT` column_name`)`;
### **MIN() and MAX()**: returns the smallest/largest value of the selected column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#min-and-max-returns-the-smallestlargest-value-of-the-selected-column)
- `SELECT MIN (`column_names`) FROM` table_name `WHERE` condition;
- `SELECT MAX (`column_names`) FROM` table_name `WHERE` condition;
### **AVG()**: returns the average value of a numeric column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#avg-returns-the-average-value-of-a-numeric-column)
- `SELECT AVG (`column_name`) FROM` table_name `WHERE` condition;
### **SUM()**: returns the total sum of a numeric column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#sum-returns-the-total-sum-of-a-numeric-column)
- `SELECT SUM (`column_name`) FROM` table_name `WHERE` condition;
# 4. Join Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#4-join-queries)
### **INNER JOIN**: returns records that have matching value in both tables
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#inner-join-returns-records-that-have-matching-value-in-both-tables)
- `SELECT` column_names `FROM` table1 `INNER JOIN` table2 `ON` table1.column_name=table2.column_name;
- `SELECT` table1.column_name1, table2.column_name2, table3.column_name3 `FROM` ((table1 `INNER JOIN` table2 `ON` relationship) `INNER JOIN` table3 `ON` relationship);
### **LEFT (OUTER) JOIN**: returns all records from the left table (table1), and the matched records from the right table (table2)
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#left-outer-join-returns-all-records-from-the-left-table-table1-and-the-matched-records-from-the-right-table-table2)
- `SELECT` column_names `FROM` table1 `LEFT JOIN` table2 `ON` table1.column_name=table2.column_name;
### **RIGHT (OUTER) JOIN**: returns all records from the right table (table2), and the matched records from the left table (table1)
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#right-outer-join-returns-all-records-from-the-right-table-table2-and-the-matched-records-from-the-left-table-table1)
- `SELECT` column_names `FROM` table1 `RIGHT JOIN` table2 `ON` table1.column_name=table2.column_name;
### **FULL (OUTER) JOIN**: returns all records when there is a match in either left or right table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#full-outer-join-returns-all-records-when-there-is-a-match-in-either-left-or-right-table)
- `SELECT` column_names `FROM` table1 `FULL OUTER JOIN` table2 `ON` table1.column_name=table2.column_name;
### **Self JOIN**: a regular join, but the table is joined with itself
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#self-join-a-regular-join-but-the-table-is-joined-with-itself)
- `SELECT` column_names `FROM` table1 T1, table1 T2 `WHERE` condition;
# 5. View Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#5-view-queries)
### **CREATE**: create a view
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#create-create-a-view)
- `CREATE VIEW` view_name `AS SELECT` column1, column2 `FROM` table_name `WHERE` condition;
### **SELECT**: retrieve a view
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#select-retrieve-a-view)
- `SELECT` * `FROM` view_name;
### **DROP**: drop a view
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#drop-drop-a-view)
- `DROP VIEW` view_name;
# 6. Altering Table Queries
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#6-altering-table-queries)
### **ADD**: add a column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#add-add-a-column)
- `ALTER TABLE` table_name `ADD` column_name column_definition;
### **MODIFY**: change data type of column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#modify-change-data-type-of-column)
- `ALTER TABLE` table_name `MODIFY` column_name column_type;
### **DROP**: delete a column
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#drop-delete-a-column)
- `ALTER TABLE` table_name `DROP COLUMN` column_name;
# 7. Creating Table Query
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#7-creating-table-query)
### **CREATE**: create a table
[](https://github.com/enochtangg/quick-SQL-cheatsheet?tab=readme-ov-file#create-create-a-table)
- `CREATE TABLE` table_name `(`
`column1` `datatype`,
`column2` `datatype`,
`column3` `datatype`,
`column4` `datatype`,
`);`
|