SQL SELECT STATEMENTS
SELECT * FROM tbl
Selectall rows and columns from table tb
SELECT c1,c2 FROM tbl
Select column c1, c2 and all rows from table th
SELECT e1,¢2 FROM tbl
WHERE conditions
ORDER BY c1 ASC, c2 DESC
Select columns e1, €2 with where conditions
And from table tbl order result by column e1
inascending order and c2in descending order
SELECT DISTINCT e1, 2
FROM tbl
Select distinct rows by columns ¢1 and e2 from
table tb
SELECT et, agaregate(expr)
FROM tht
GROUP BY et
Select column e1 and use aggregate function on
expression expr, group columns by column cl
SELECT ct, aggregate(expr) AS e2
PROM tb
GROUP BY ct
HAVING c2 >v
Select column e1 and c2as column alias ofthe
result of aggregate function on expr. iter group
‘of records with c2 greater than value v
SQL UPDATE TABLE
INSERT INTO thI(c1,2,
VALUES(v1,v2..)
Insertdata into table tbl
INSERT INTO thI(c1,c2,
)
SELECT ctc2..FROM thi2
WHERE conditions
Insert data from tbI2 into tbl
uppaTEt
WHERE conditions
Update data in table tbl
DELETE FROM thl
WHERE conditions
Delete records from table th based on WHERE
conditions,
‘TRUNCATE TABLE tbl
Drop table tbl and re-create ital datas lost
‘SQL TABLE STATEMENTS
(CREATE TABLE tbI(
1 datatypetlength)
2 datatypetlength)
PRIMARY KEY(e1)
a
Create table tbl with primary ey is el
DROP TABLE tbl
Remove table tl from database,
ALTER TABLE tht
ADD COLUMN c1 datatype(length)
‘Add column cto table tb
ALTER TABLE tbl
DROP COLUMN et
Drop column c1 from table th
SQL JOIN STATEMENTS
SELECT * FROM tbIL
INNER JOIN tbl2 ON join-conditions
Inner join table tlt with tbi2 based on jin
conditions
SELECT * FROM thI1
LEFT JOIN tbl2 ON join-conditions
Left join table tlt with tb12 based on jl
conditions.
SELECT * FROM thI1
RIGHT JOIN thI2 ON join-conditions
Right jin table tbl with tb2 based on join
conditions
SELECT * FROM thI1
RIGHT JOIN thl2 ON joi
Fall outer join table tll with th12 based on join
conditions
‘conditions