CS W186 Exam Prep Section 1:
SQL
SQL
Single-Table SQL
SELECT [DISTINCT] <column list>� FROM <table1>� [WHERE <predicate>]� [GROUP BY <column list>]� [HAVING <predicate>]� [ORDER BY <column list> [DESC/ASC]]� [LIMIT <amount>];
Single-Table SQL: Logical Processing Order
Join Variants
SELECT * FROM
T1 INNER JOIN T2
ON T1.a = T2.a;
Join Condition
String Comparison
LIKE: following expression follows SQL specified format
Examples:
Note: ~ cannot be used in SQLite
More SQL Things
More SQL Things
Worksheet
Question 1
Question 1a
Return the bid and genre of each book that has ever been checked out. Remove any duplicate rows with the same bid and genre.
Need to join Books and Checkouts to get genre and the fact that a book is checked out
INNER JOIN would work as well!
Question 1b
Find all of the fantasy book titles that have been checked out and the date when they were checked out. Even if a book hasn’t been checked out, we still want to output the title (i.e. the row should look like (title, NULL)).
Wanting NULL in the output is a good sign of a LEFT OUTER or RIGHT OUTER join
LEFT JOIN to preserve all books
Use WHERE to filter genre of the joined table
Question 1c
Select the name of the book that has been checked out the most times and the corresponding checked out count. You can assume that each book was checked out a unique number of times, and that the titles of the books are all unique
Want title from Books table, # of checked out from aggregating on Checkouts. Group by title, not bid --- bid is unique for each “instance” of a book!
Question 1d
Select the name of all of the pairs of libraries that have books with matching titles. Include the name of both libraries and the title of the book. There should be no duplicate rows, and no two rows that are the same except the libraries are in opposite order. To ensure this, the first library name should be alphabetically less than the second library name. There may be zero, one, or more than one correct answer.
Incorrect: does not return books with matching titles. Also incorrectly uses lname to compare to Books.library
Correct: Filters rows in cross join where the alphabetic order is respected and book titles are the same
Correct: Finds book-library pairs as inner subqueries. Outer query does the cross join of these pairs such that the titles are the same and l1 is alphabetically “less than” l2
Question 2
Question 2a
Select all of the following queries which return the rid of the Rider with the most bikes. Assume all Riders have a unique number of bikes.
Correct: owner references rid so we don’t need to join on another table. Just aggregate!
Correct: returns “all” owners that have at least as many bikes as all owners. Because all riders have a unique # of bikes, this returns the 1 rider with the most bikes
Incorrect: using MAX on the table bikes is nonsensical (what even would be aggregated?)
Question 2b
Select the bid of all Bikes that have never been ridden
Incorrect: Finds all bikes without an owner --- not what we want!
Correct: Finds all bikes in the Bikes table for which there are no entries in the Rides table
Correct: Finds all bikes in the Bikes table that do not exist in the join of Rides and Bikes
Question 2c
Select the name of the rider and the city_name of the src and dest locations of all their journeys for all rides. Even if a rider has not ridden a bike, we still want to output their name.
None of these are correct!
The INNER JOINs and WHERE clauses will filter out rows with NULL values produced by the OUTER JOIN.
Question 2c
Select the name of the rider and the city_name of the src and dest locations of all their journeys for all rides. Even if a rider has not ridden a bike, we still want to output their name.
How would we construct a correct answer?
Need to do a JOIN on Locations to get the city_name, but need to do this before the join onto riders
Attendance Link