Review for the First Databases Quiz

Last updated on April 5, 2026 pm

This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.

Given the following schema:

employee(person_name, street, city)
works(person_name, company_name, salary)
company(company_name, city)

Find the names of employees whose salary is $\ge$ the salary of everyone in the database

Solution: There is no MAX() function in relational algebra, so we use set difference to solve it:

  1. Find all people whose salary is not the highest.
  2. Subtract these "not highest salary" people from "all people".

$$\text{LowerPay} = \Pi_{works.person_name}(works \bowtie_{works.salary < d.salary} \rho_d(works))$$

$$\text{Result} = \Pi_{person_name}(works) - \text{LowerPay}$$


How to express the $\div$ operation using basic operators:

The basic idea: list the full set, use set difference against the full set to find the missing combinations, then remove those missing ones.

Suppose $R$ has attributes $(A, B)$ and $S$ has attribute $(B)$, and we want to compute $R \div S$:

$T_1 = \Pi_A(R)$ finds all candidates that could possibly satisfy the condition

$T_2 = T_1 \times S$ finds the full set, producing all possible $(A,B)$ combinations

$T_3=T_2-R$ gives the missing part

$T_4 = \Pi_A(T3)$ keeps only $A$

$Result = T_1 - T_4 = \Pi_A(R) - \Pi_A((\Pi_A(R) \times S) - R)$


Exercise on scalar subqueries:

In University Schema, find the sections that had the maximum enrollment in Fall 2017

Use a with subquery to first compute the enrollment for each section; then use a scalar subquery:

with cte as (
    select course_id, sec_id, count(*) as enrollment
    from takes
    where takes.year = 2017 and takes.semester = 'Fall'
    group by course_id, sec_id, year, semester
    -- 这里带不带 year semester 无所谓,where 已经筛过了
)
select course_id, sec_id
from cte
where enrollment = (select max(enrollment) from cte)

Any column that appears after SELECT but is not wrapped in an aggregate function (such as COUNT, SUM) must be written after GROUP BY.

The so-called scalar subquery is exactly this line: where enrollment = (select max(enrollment) from cte).

Of course, this problem can also be solved with a window function:

with cte as (
    select course_id, sec_id, count(*) as cnt
        rank() over (
            partition by year, semester
            order by count(*) desc
        ) as rk
    from takes
    where semester = 'Fall' and year = 2017
    group by course_id, sec_id, year, semester
)
select course_id, sec_id
from cte
where rk = 1

-- Find each customer who has an account at *every* branch located in 'Brooklyn'
select customer.ID, customer.customer_name
from customer
natural join depositor
join account on account.account_number = depositor.account_number
join branch on branch.branch_name = account.branch_name
where branch.branch_city = 'Brooklyn'
group by customer.ID, customer.customer_name, account.branch_city
having count(distinct branch.branch_name) = (select count(*) from branch where branch_city = 'Brooklyn')

The common pitfall in this problem is that the last part needs to be distinct branch.branch_name, because, for example, a branch may open many accounts, which does not match the intent of the question.



Review for the First Databases Quiz
https://blog.wendain.ing/en/posts/db-quiz-review-1/
Author
wendaining
Posted on
April 5, 2026
Updated on
April 5, 2026
Licensed under