0% found this document useful (0 votes)
55 views

SQL Cheat Sheet

Uploaded by

rajeswaranm2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

SQL Cheat Sheet

Uploaded by

rajeswaranm2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

S Q L cheat sheet

Basic Queries
-->

filter your columns

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

--> order of the


ORDER BY col2

resul ts

al l rows from
even if they do not exist in table tabl e
Useful keywords for SELECTS.

LEFT OUTER JOIN - A

DISTINCT - return uni q ue resul ts

BETWEEN a AND b - l i m i t
values can be numbers, text, or dates the range, the
Data Modification
-->

update specific data with the WHERE clause

A B

UPDATE
col2=2
table1 SET col1=1 WHERE
insert valINTO
-->

INSERT ues manual l y

table1(ID, FIRST_NAME, fetch the resul ts tha


LAST_NAME)
exist in both tables
INNER JOIN - t

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

V ieVIEW issa virtual table which is a

w
al l rows from tabl e
resul
A

hey tanof a
bequery
used .

to create
,

vi r tual tabl e s of B, even if they do not exist in table


RIGHT OUTER JOIN -

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 -->

convert strings to dates:

TO_DATE ( Oracle, PostgreSQL),


You can use JOINs in your UPDATEs

STR_TO_DATE(MySQL)

UPDATE t1 SET a=1

-->

return the first non-NULL argument:

FROM table t1 JOIN table2 t2 ON


COALESCE(col1, col2, “default
t1.id= t2.t1_id

value”)

WHERE t1.col1=0 AND t2.col2 IS


-->

return current time:

NULL;

CURRENT_TIMESTAMP

--> compute set operations on two results sets

NB! Use database specific syntax, it


SELECT col1, col2 FROM table1

might be faster!
UNION/ EXCEPT / INTERSECT

SELECT col3, col4 FROM table2;

Semi JOINs
Union - returns data from both queries

You can use subqueries instead of


Except - rows from the first query that are
JOINs:

not present in the second quer y

SELECT col1, col2 FROM table1


Intersect - rows that are returned from both
WHERE id IN

queries
(SELECT t1_id FROM table2

WHERE date>

CURRENT_TIMESTAMP)
Reporting
Use aggregation functions

Indexes COUNT - return the number of rows

SUM - cumulate the values

If you query by a column, index it!

CREATE INDEX index1 ON table1


AVG - return the average for the group

MIN / MAX - smallest / largest value


(col1) FROM table1 WHERE id IN

Don’t forget:

Avoid overlapping indexes

Avoid indexing on too many columns

Indexes can speed up DELETE and

UPDATE operations

You might also like