Lessons from Relational Databases
where 20 > ( select count(e.ssn)
from ( Departments d left-outerjoin Employees e
having 20 > count(e.ssn);
Notes:
In relational databases, there already some methods that unnest some of these queries. They require outerjoins and grouping. For example, this SQL query finds all departments with less than 20 employees. Its unnested form joins departments with employees, then groups by each department, and then counts the employees in each group. If a department has no employees, this too must appear at the output. This is why an outerjoin is used instead of a join.