Basics
Query on one Table
select column1, column2, column3 as name
from table
where left=right;
Query on one Table with null Condition
select column1, column2, column3 as name
from table
where column is null;
Join
Join of two Tables
select column1, column2, column3 as name
from table1 t1
join table2 t2 on t2.column=t1.column
where left=right;
Join of three Tables
select column1, column2, column3 as name
from table1 t1
join table2 t2 on t2.column=t1.column
join table3 t3 on t3.column=t2.column
where left=right;
Join of four Tables
select column1, column2, column3 as name
from table1 t1
join table2 t2 on t2.column=t1.column
join table3 t3 on t3.column=t2.column
join table4 t4 on t4.column=t3.column
where left=right;
Aggregaton
Aggregation without Grouping
select
sum(column) as total,
avg(column) as mean,
count(*) as number_of
from table;
Aggregation with Grouping
select column1, column2, sum(column3) as total , count(*) as number_of
from table
group by column1, column2;
Aggregation with Condition on Group Level
select column1, column2, sum(column3) as total , count(*) as number_of
from table
group by column1, column2
having count(*) = 10;
Aggregation with Join
select column1, column2, sum(column3) as total , count(*) as number_of
from table t1
join table2 t2 on t2.column=t1.column
group by column1, column2
having count(*) = 10;
Subquery
Subquery in Where Part
select column1, column2
from table
where left = (
subquery here
);
Subquery in Select Part
select column1, (subquery here)
from table
where left = right;
Subquery in From Part
select column1, column2
from
table t1
join
(subquery here) t2
on t2.column=t1.column
where left = right;