Discussion 11: SQL๐Ÿ–จ๏ธ

Attendance

Your TA will come around during discussion to check you in. You can start on the worksheet before being checked in; you don't need to wait for your TA to get started.

If you didn't attend for a good reason (such as being sick), fill out this form (within 2 weeks of your discussion): attendance form

If you get stuck, there are walkthrough videos (@berkeley.edu login to YouTube required).

Getting Started

Everybody say your name, and then share your favorite restaurant, cafe, or boba shop near campus. (Yes, Kingpin Donuts counts as a restaurant.)

Select Statements

A SELECT statement describes an output table based on input rows. To write one:

  1. Describe the input rows using FROM and WHERE clauses.
  2. Format and order the output rows and columns using SELECT and ORDER BY clauses.

SELECT (Step 2) FROM (Step 1) WHERE (Step 1) ORDER BY (Step 2);

The WHERE and ORDER BY clauses are optional.

See Lab 10 for more descriptions of SQL.

Joins

Step 1 may involve joining tables to form input rows that consist of two or more rows from existing tables. An explicit join uses JOIN to name the next table and ON to describe how its rows relate to the rows already present. For example, if titles has columns tconst (a unique ID for each movie), title, and year, and ratings has columns tconst and averageRating, then this statement lists the rating of each movie. Since both tables have a tconst column, a dot specifies which table's column is meant:

SELECT title, averageRating FROM titles JOIN ratings
  ON titles.tconst = ratings.tconst;

To join a table with itself, give each copy its own name with AS, then use a dot to refer to the columns of each copy, such as a.title and b.title. This statement lists each pair of movies released in the same year:

SELECT a.title AS first, b.title AS second
  FROM titles AS a JOIN titles AS b
  ON a.year = b.year
  WHERE a.title < b.title;

Pizza Time

The pizzas table contains the names, opening, and closing hours of great pizza places in Berkeley. The meals table contains typical meal times (for college students). A pizza place is open for a meal if the meal time is at or within the open and close times.

CREATE TABLE pizzas (name TEXT, open INTEGER, close INTEGER);

INSERT INTO pizzas VALUES
  ("Artichoke", 12, 15),
  ("La Val's", 11, 22),
  ("Sliver", 11, 20),
  ("Cheeseboard", 16, 23),
  ("Emilia's", 13, 18);

CREATE TABLE meals (meal TEXT, time INTEGER);

INSERT INTO meals VALUES
  ("breakfast", 11),
  ("lunch", 13),
  ("dinner", 19),
  ("snack", 22);

Q1: Late Night Snack

What's still open for a late night snack? Create a late table with one column named status that has a sentence describing the closing time of each pizza place that closes at or after snack time. Important: Don't use any numbers in your SQL query! Instead, use a join to compare each restaurant's closing time to the time of a snack. The rows may appear in any order.

late table:

status
Cheeseboard closes at 23
La Val's closes at 22

The || operator in SQL concatenates two strings together, just like + in Python.

CREATE TABLE late AS
  SELECT ____ || " closes at " || ____ AS status
  FROM ____ JOIN ____ ON ____
  WHERE ____;
Hint

To compare a pizza place's close time to the time of a snack, join the pizzas and meals tables using FROM pizzas JOIN meals, and use ON to compare the time of the snack to the close of the pizza place. Then, use only rows where the meal is a "snack".

Q2: Double Pizza

If two meals are more than 6 hours apart, then there's nothing wrong with going to the same pizza place for both, right? Create a double table with three columns. The first column is the earlier meal, the second column is the later meal, and the name column is the name of a pizza place. Only include rows that describe two meals that are more than 6 hours apart and a pizza place that is open for both of the meals. The rows may appear in any order.

double table:

first second name
breakfast dinner La Val's
breakfast dinner Sliver
breakfast snack La Val's
lunch snack La Val's
CREATE TABLE double AS
  SELECT ____ AS first, ____ AS second, name
  FROM ____ AS a JOIN ____ AS b ON ____ JOIN ____ ON ____;
Hint

Join meals with itself so that each row has info about two meals, using the ON clause to keep meals that are more than 6 hours apart. Then join pizzas and use its ON clause to keep pizza places that are open for both meals.

A Final Exam About Final Exams

Adapted from the Spring 2023 final exam.

The finals table has columns hall (strings) and course (strings), and has rows for the lecture halls in which a course is holding its final exam.

The sizes table has columns room (strings) and seats (numbers), and has one row per unique room on campus containing the number of seats in that room. All lecture halls are rooms.

CREATE TABLE finals (hall TEXT, course TEXT);

INSERT INTO finals VALUES
  ("RSF", "61A"),
  ("Wheeler", "61A"),
  ("Pimentel", "61A"),
  ("Li Ka Shing", "61A"),
  ("Stanley", "61A"),
  ("RSF", "61B"),
  ("Wheeler", "61B"),
  ("Morgan", "61B"),
  ("Wheeler", "61C"),
  ("Pimentel", "61C"),
  ("Soda 310", "61C"),
  ("Soda 306", "10"),
  ("RSF", "70");

CREATE TABLE sizes (room TEXT, seats INTEGER);

INSERT INTO sizes VALUES
  ("RSF", 900),
  ("Wheeler", 700),
  ("Pimentel", 500),
  ("Li Ka Shing", 300),
  ("Stanley", 300),
  ("Morgan", 100),
  ("Soda 306", 80),
  ("Soda 310", 40),
  ("Soda 320", 30);

Q3: Room Sharing

Create a sharing table with three columns, hall, first, and second, that has a row for each pair of different courses that use the same hall for their finals. The first course comes before the second course in alphabetical order, so each pair appears only once per hall. The rows may appear in any order.

Your query should work correctly for any data that might appear in the finals table, but for the example data above the result should be:

sharing table:

hall first second
RSF 61A 61B
RSF 61A 70
RSF 61B 70
Wheeler 61A 61B
Wheeler 61A 61C
Wheeler 61B 61C
Pimentel 61A 61C

Discussion Time: Why does CS 10 not appear in the result? Which join condition keeps courses in different halls from being paired? Which condition keeps a course from being paired with itself and keeps RSF|61B|61A out of the result?

CREATE TABLE sharing AS
  SELECT ____ AS hall, ____ AS first, ____ AS second
  FROM finals AS a JOIN finals AS b ON ____
  WHERE ____;
Hint

Join finals with itself using an ON clause that keeps pairs in the same hall. Then use < to compare the two courses: that rules out pairing a course with itself, and it keeps only one of the two orderings of each pair.

Q4: Two Rooms

Create a pairs table with one column, rooms, that has a string describing each pair of rooms that together have at least 1,000 seats. Each string lists the two room names in alphabetical order and then the combined number of seats in the two rooms. The rows appear in decreasing order of the total number of seats. (Pairs with the same total may appear in any order.)

Your query should work correctly for any data that might appear in the sizes table, but for the example data above the result should be:

pairs table:

rooms
RSF and Wheeler together have 1600 seats
Pimentel and RSF together have 1400 seats
Li Ka Shing and RSF together have 1200 seats
Pimentel and Wheeler together have 1200 seats
RSF and Stanley together have 1200 seats
Li Ka Shing and Wheeler together have 1000 seats
Morgan and RSF together have 1000 seats
Stanley and Wheeler together have 1000 seats

When adding numbers and including the result in a string, put parentheses around the arithmetic: "one plus two is " || (1 + 2).

CREATE TABLE pairs AS
  SELECT ____ || " and " || ____ || " together have " || ____ || " seats" AS rooms
  FROM sizes AS a JOIN sizes AS b ON ____
  WHERE ____
  ORDER BY ____ DESC;
Hint

Join sizes with itself using FROM sizes AS a JOIN sizes AS b ON a.room < b.room so that each row describes a pair of different rooms, with the names in alphabetical order. The total number of seats is a.seats + b.seats, which you can use in both the WHERE and ORDER BY clauses.


Back to Top

Accessibility Nondiscrimination

Copyright ยฉ2026, Regents of the University of California and respective authors.

This site is built following the Berkeley Class Site template, which is generously based on the Just the Class, and Just the Docs templates.

View all course offerings