Exam Prep 1
SQL
Announcements
assignment
Project 1 (SQL)
Assignment Deadline
Thursday, 9/10
at 11:59 PM
lightbulb
Vitamin 2
Assignment Deadline
Monday, 9/14
at 11:59 PM
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:
~ : following expression follows regex format
Examples:
Note: ~ cannot be used in SQLite (which Project 1 will be using)
More SQL Things
More SQL Things
Worksheet: Q1
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.
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.
SELECT DISTINCT b.bid, b.genre
FROM Books b, Checkouts c
WHERE b.bid = c.book
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)).
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)).
SELECT title, day
FROM Books b LEFT OUTER JOIN Checkouts c � ON c.book = b.bid
WHERE b.genre = Fantasy
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. (Note: bid is unique for each “instance” of a book)
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. (Note: bid is unique for each “instance” of a book)
SELECT title, count(*) as cnt
FROM Books b, Checkouts c
WHERE b.bid = c.book
GROUP BY b.title
ORDER BY cnt DESC
LIMIT 1
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.
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
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.
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.
Correct: Filters rows in cross join where the alphabetic order is respected and book titles are the same
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.
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.
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
Worksheet: Q2
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.
Question 2a
Correct: owner references rid so we don’t need to join on another table. Just aggregate!
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.
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.
Question 2a
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
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.
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.
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.
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.
Question 2b
Select the bid of all bikes that have never been ridden.
Incorrect: The subquery returns the rows in bikes with the same bid as the current row. NOT EXISTS always evaluates to false, so no rows are returned by the query.
Question 2b
Select the bid of all bikes that have never been ridden.
Question 2b
Select the bid of all bikes that have never been ridden.
Correct: Finds all bikes in the Bikes table for which there are no entries in the Rides table
Question 2b
Select the bid of all bikes that have never been ridden.
Question 2b
Select the bid of all bikes that have never been ridden.
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.
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.
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.
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?
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