SQL
##A brief overview of important SQL statements
select
statementdistinct
and like
in where
statement.%
can be used as wild card in like
statement.select distinct name
from empinfo
where age > 40 and firstname like 'bob%'
###Aggregation functions
distinct
, min
, max
, sum
, avg
, count
, and count(*)
where return the number of rows.select max(salary), firstname, lastname
from empinfo
###group by
statement
group by
statement will group rows with same attribute. It is usually followed with aggregation function.select max(salary), department
from empinfo
group by department
###having
statement
having
statement usually follows group by
statement.having
and where
statements are the followings:
avg()
, cannot be used in where
statement, but can be used in having
statement.where
can be used in insert
, delete
, select
statements, but having
can only be used in select
statement.having
can only use with group by
statement.select max(salary),department
from empinfo
where department like 'elec%'
group by department
having avg(salary) > 5000
###order by
statement
order by
statement can be used with two options ASC
or DESC
.select employee_id, department, firstname, lastname
from empinfor
where department like 'core%'
order by salary DESC, name ASC
###in
and between
statements
in
and between
statements can be combined to make more powerful boolean clauses.select employee_id, salary, department, firstname, lastname
from empinfor
where firstname in ('John', 'Bob', 'William')
and (salary between 2000 and 5000)
select title from movie
where id in ('11768','11955','21191')
###join
statement
join
statement makes ralational database ralational.join
statement can make the database operation easier. For example, one can use join
statement instead of writing a complicated code using select
and where
statements.select customers.firstname, customers.lastname, purchases.id, purchases.name, purchases.price
from customer, purchases
where customer.id == purchases.customerid
select customers.firstname, customers.lastname, purchases.id, purchases.name, purchases.price
from customer inner join purchases
on customer.id == purchases.customerid
###Arithmetic operation
select name, gdp/population
from world
where area > 500000
###Check the difference in date
datediff()
can be used to check the difference in date variables.select w2.Id
from Weather w1 join Weather w2
on datediff(w2.Date,w1.Date) = 1
where w1.Temperature < w2.Temperature
###coalesce
statement
coalesce
statement will replace the null
entries with 0
.select name,coalesce(mobile,'07986 444 2266')
from teacher
select c.m1,team1,COALESCE(s1,0),team2,COALESCE(s2,0)
from
(
select id id1,mdate m1,team1,s1
from game
left join
(
select id tmpid,m1,team1 tmpteam1,sum(score1) s1
from
(
SELECT id,mdate m1,team1,
CASE WHEN teamid=team1 THEN 1 ELSE 0 END score1
FROM game JOIN goal ON matchid = id
)a
group by tmpid
)b
on id = tmpid
)c
join
(
select id id2,mdate m2,team2,s2
from game
left join
(
select id tmpid,m2,team2 tmpteam2,sum(score2) s2
from
(
SELECT id,mdate m2,team2,
CASE WHEN teamid=team2 THEN 1 ELSE 0 END score2
FROM game JOIN goal ON matchid = id
)d
group by tmpid
)e
on id = tmpid
)f
on c.id1 = f.id2
order by m1
###isnull()
function
isnull()
function is usually worked with where
to form boolean claus.select name from teacher where isnull(dept)
join
statementsinner join
, left outer join
, and full outer join
.Suppose there are two tables
a | b |
---|---|
1 | 3 |
2 | 4 |
3 | 5 |
4 | 6 |
inner join
is the intersection of two tables. For example, the query select * from a INNER JOIN b on a.a = b.b
will generate the following table
a | b |
---|---|
3 | 3 |
4 | 4 |
left outer join
will also be the intersection of two tables. In addition, it keeps the elements from the first table and keep null
values from the second one. For example, the query select * from a left outer join b on a.a = b.b
will generate the following table
a | b |
---|---|
1 | null |
2 | null |
3 | 3 |
4 | 4 |
full outer join
is the union of two tables. For example, the query select * from a FULL OUTER JOIN b on a.a = b.b
will generate the following table
a | b |
---|---|
1 | null |
2 | null |
3 | 3 |
4 | 4 |
null | 5 |
null | 6 |
left join
is the same as left outer join
, and right join
is the same as right outer join
.###case
statement
case
allows SQL query returns different values under different conditions.Null
is returned.case
statement takes the following form
CASE WHEN condition1 THEN value1
WHEN condition2 THEN value2
ELSE def_value
END
select name,
case when dept=1 or dept=2 then 'Sci' else 'Art' end
from teacher
SELECT id,mdate m2,team2,
CASE WHEN teamid=team2 THEN 1 ELSE 0 END score2
FROM game JOIN goal ON matchid = id
SELECT name, population
,CASE WHEN population<1000000
THEN 'small'
WHEN population<10000000
THEN 'medium'
ELSE 'large'
END
FROM bbc
###cast()
function
cast()
function to transfer, e.g., char
to int
.ORDER BY CAST(thecolumn AS int)
.###concat()
function
concat()
function is used to combine, e.g., two strings.concat()
function is given as the following which is the solution to the fifth exercise in sqlzoo.
select name, concat(round(population/(select population from world where name = 'Germany')*100),'%')
from world
where continent = 'Europe'
###all
statement
all
statement allows boolean operators >
,<
,>=
,<=
act on a set of numbers.max()
or min()
operators first on the set of numbers.select name from world
where gdp >= all(select coalesce(gdp,0) from world where continent = 'Europe') and continent != 'Europe'
###Nested select
statements
select
statement can be used in the inner select
statement.select continent, name,area
from world x
where area >= all
(select area from world y
where x.continent = y.continent and y.area > 0)
###not exist
statement
select name,continent,population
from world x
where not exists (
select *
from world y
where y.continent = x.continent and y.population> 25000000
)
###More about in
statement
in
can be used as a value - it will be 0 or 1.SELECT winner, subject, subject IN ('Physics','Chemistry') ord
FROM nobel
WHERE yr=1984
ORDER BY ord,subject,winner
###’limit n offset m’ statement
select distinct Salary
from Employee
order by Salary DESC
limit 1 offset N
###Order and get the rank of some attribute
count()
and group by
statement to obtain the number of items that has value smaller/greater than the current value.count(distinct )
statement to allow ranking same value twice.select Department.Name,mytable.Name,mytable.Salary
from
(select e1.DepartmentId as DepartmentId, e1.Name as Name, e1.Salary as Salary, count(distinct e2.Salary) as Counter
from
Employee as e1 join Employee as e2 on e1.DepartmentId = e2.DepartmentId and e1.Salary<=e2.Salary
group by e1.Name) as mytable
join Department on mytable.DepartmentId = Department.Id
where mytable.Counter<=3
###delete
statement
delete
statement is used to delete one item from the database table following the form delete from table where [clause]
.where
clause.