SQL Cheat Sheet
SQL Cheat Sheet
Basic Queries
-->
Th e o of
j y JOINS
SELECT
table1
col1, col2, col3,...FROM
-->
fiWHERE
lter thecol4=1
rows
AND col5=2
-->
aggregate
GROUP the
by...
data
A B
-->
lHAVING
imit aggregated data
count(*)>1
resul ts
al l rows from
even if they do not exist in table tabl e
Useful keywords for SELECTS.
BETWEEN a AND b - l i m i t
values can be numbers, text, or dates the range, the
Data Modification
-->
A B
UPDATE
col2=2
table1 SET col1=1 WHERE
insert valINTO
-->
VALUES(1,
or by usinINTO
--> INSERT ‘Rebel’,
g thetable1(ID, ‘Labs’);
results of a query
LAST_NAME)
FIRST_NAME,
SELECT id, last_name,
first_name FROM table2 A B
w
al l rows from tabl e
resul
A
hey tanof a
bequery
used .
to create
,
com
T
CREATEpl ex queri
VIEW e
vis.
e w A
SELECT
FROM col1,
table1
col2
1 S
WHERE...
SQ L cheat sheet
Updates on JOINed Useful Utility Functions
Queries -->
STR_TO_DATE(MySQL)
-->
value”)
NULL;
CURRENT_TIMESTAMP
might be faster!
UNION/ EXCEPT / INTERSECT
Semi JOINs
Union - returns data from both queries
queries
(SELECT t1_id FROM table2
WHERE date>
CURRENT_TIMESTAMP)
Reporting
Use aggregation functions
Don’t forget:
UPDATE operations