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 benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller tararoopram. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.58. You're not tied to anything after your purchase.