Basic SQL syntax
The first names of all students
select first
from Students
The first names of all students living in Springfield
select first
from Students
where address = 'Springfield'
Last names of all students with first name George
select last
from Students
where first = 'George'
select first
from Students S
where S.last = 'Orwell'
Combining Students and Results
select *
from Students as S, Results as R
where S.sid = R.sid -- join condition
Joins of multiple tables
The results of George Orwell
select first, last, category, number, points
from Students as S, Results as R
where S.sid = R.sid -- join condition
and first = 'George' and last = 'Orwell'
Joining three tables: all results of George Orwell
select S.first, S.last, E.category, E.number, R.points, E.maxPoints
from Students S, Results R, Exercises E
where S.sid = R.sid -- join condition for S-R
and R.category = E.category and R.number = E.number -- join condition for R-E
and S.first = 'George' and S.last = 'Orwell'
Persons who take classes
select P.name, C.kind
from Persons P, Classes C, TakesClasses T
where P.id = T.person_id
and C.id = T.class_id
1
, All students that have 9 points in homework 1 and 2
select S.first, S.last
from Students S, Results R1, Results R2
where S.sid = R1.sid
and S.sid = R2.sid
and R1.category = 'homework' AND R1.number = 1
and R2.category = 'homework' AND R2.number = 2
and R1.points >= 9 AND R2.points >= 9
Persons who like someone with blue eyes
select distinct PA.name
from Persons PA, Persons PB, Likes L
where PA.id = L.personA_id and PB.id = L.personB_id
and PB.eyeColor = "blue"
Persons who like at least two people with green eyes
select distinct PA.name
from Persons PA, Persons PB, Likes L
where PA.id = L.personA_id and PB.id = L.personB_id
and PB.eyeColor = "green"
group by PA.name
having count(*) >= 2
Men that play soccer with a female player
select distinct P.name
from Persons P, Persons PA, Persons PB, SportTogether S
where PA.id = S.personA_id and PB.id = S.personB_id
and (PA.name = P.name or PB.name = P.name)
and (P.gender = 'male' and PA.gender <> PB.gender)
and S.sport = 'soccer'
Last names without duplicates
select distinct last
from Students
(Inner) join
SELECT A.name, B.name
FROM TableA A JOIN TableB B
ON A.id = B.id
Left (outer) join
SELECT A.name, B.name
FROM TableA A LEFT JOIN TableB B
ON A.id = B.id
2
The first names of all students
select first
from Students
The first names of all students living in Springfield
select first
from Students
where address = 'Springfield'
Last names of all students with first name George
select last
from Students
where first = 'George'
select first
from Students S
where S.last = 'Orwell'
Combining Students and Results
select *
from Students as S, Results as R
where S.sid = R.sid -- join condition
Joins of multiple tables
The results of George Orwell
select first, last, category, number, points
from Students as S, Results as R
where S.sid = R.sid -- join condition
and first = 'George' and last = 'Orwell'
Joining three tables: all results of George Orwell
select S.first, S.last, E.category, E.number, R.points, E.maxPoints
from Students S, Results R, Exercises E
where S.sid = R.sid -- join condition for S-R
and R.category = E.category and R.number = E.number -- join condition for R-E
and S.first = 'George' and S.last = 'Orwell'
Persons who take classes
select P.name, C.kind
from Persons P, Classes C, TakesClasses T
where P.id = T.person_id
and C.id = T.class_id
1
, All students that have 9 points in homework 1 and 2
select S.first, S.last
from Students S, Results R1, Results R2
where S.sid = R1.sid
and S.sid = R2.sid
and R1.category = 'homework' AND R1.number = 1
and R2.category = 'homework' AND R2.number = 2
and R1.points >= 9 AND R2.points >= 9
Persons who like someone with blue eyes
select distinct PA.name
from Persons PA, Persons PB, Likes L
where PA.id = L.personA_id and PB.id = L.personB_id
and PB.eyeColor = "blue"
Persons who like at least two people with green eyes
select distinct PA.name
from Persons PA, Persons PB, Likes L
where PA.id = L.personA_id and PB.id = L.personB_id
and PB.eyeColor = "green"
group by PA.name
having count(*) >= 2
Men that play soccer with a female player
select distinct P.name
from Persons P, Persons PA, Persons PB, SportTogether S
where PA.id = S.personA_id and PB.id = S.personB_id
and (PA.name = P.name or PB.name = P.name)
and (P.gender = 'male' and PA.gender <> PB.gender)
and S.sport = 'soccer'
Last names without duplicates
select distinct last
from Students
(Inner) join
SELECT A.name, B.name
FROM TableA A JOIN TableB B
ON A.id = B.id
Left (outer) join
SELECT A.name, B.name
FROM TableA A LEFT JOIN TableB B
ON A.id = B.id
2