# Review for the First Databases Quiz

> This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to [original Chinese version](<https://blog.wendain.ing/posts/db-quiz-review-1/>). I am continuously working to improve the translation.

Given the following schema:

```plaintext
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:

```sql
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:

```sql
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
```

---

```sql
-- 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.

---
