What is the equivalent SQL for πfirstname, birthday(Person)?
SELECT DISTINCT firstname, birthday FROM Person
Which SQL condition would fulfill the following:
- matches “bat”
- matches “chat”
- does NOT match “bank”
LIKE “%a_”
% = match everything
_ = match one thing
What is projection? Given a relation R, how to select columns A and B?
What does this mean:
πTitle (πgenre=”suspense”^”audience”=”Teenagers”(Books))
SELECT DISTINCT Title FORM Books WHERE genre = “Suspense” AND audience = “Teenagers”
What is the cartesian product? How to represent in SQL? in RA?
Contains every combination of rows in the 2 tables:
SQL: SELECT * FROM R1, R2;
RA: R1 x R2
In SQL and RA, show to how to use cartesian product to find all stores based in Winnipeg
SELECT storeName FROM Store, City
WHERE Store.CID = City.CID
AND cityName = “Winnipeg”
πstoreName(πStore.CID=City.CID^CityName=”Winnipeg”(Store x City))
Why doesn’t this work in cartesian product?
SπΈπΏπΈπΆπ π·πΌπππΌππΆπ π‘ππ‘ππ πΉπ
ππ π΅ππππ , π
πππ, ππππππ
ππ»πΈπ
πΈ ππππ π‘ = “π΄ππππ” π΄ππ· πππ π‘ = “π΅ππππβ
You need to tell SQL how the tables are connected. To do so, do joins on People, Read, and Books.
What is a natural join?
Join which matches on all columns that share a name, and doesn’t duplicate columns
How to represent this in RA?
ππΈπΏπΈπΆπ ππππ π‘, πππ π‘, π΄πΌπ· πΉπ
ππ ππππππ ππ΄πππ
π΄πΏ π½ππΌπ π΄π’π‘βπππ
πfirst,last,AID(PeopleβAuthors)
When to use a JOIN ON?
Tables don’t share same column name, or want to be more explicit about what’s happening.
SQL:
ππΈπΏπΈπΆπ ππππ π‘, πππ π‘, π΄πΌπ·
πΉπ
ππ ππππππ π½ππΌπ π΄π’π‘βπππ ππ ππππππ. ππΌπ = π΄π’π‘βπππ . ππΌπ
RA:
πfirst,last,AID(PeopleβPeople.SIN=Authors.SIN Authors)
In RA and SQL, Find the names of all the people who live in Winnipeg and have read at least
one book
SπΈπΏπΈπΆπ π·πΌπππΌππΆπ ππππ π‘, πππ π‘
πΉπ
ππ ππππππ ππ΄πππ
π΄πΏ π½ππΌπ πΆππ‘π¦ ππ΄πππ
π΄πΏ π½ππΌπ π
πππ
ππ»πΈπ
πΈ πππ‘π¦ππππ = “ππππππππβ
πfirst,last(πcityName=”Winnipeg”(PeopleβCityβRead))
Describe the different set operations and their corresponding SQL/RA translations
UNION (U in RA):
result set contains tuples in 1 or other, or both
INTERSECT (β© in RA):
result set contains only tuples in both
EXCEPT (- in RA) (aka subtraction):
result set contains tuples in first relation but not the second