Lesson 5 of 12 · 20 min

IN versus EXISTS (and when to join)

By the end of this lesson you can choose between IN, EXISTS and a JOIN for a given question, predict when NOT IN and NOT EXISTS disagree, and read the plan PostgreSQL uses for each.

This lesson uses Masar Holding's HR database. employees.manager_id points to another employee; it is NULL only for the CEO, who has no manager. Leavers stay in the table with a termination_date.

You now know three ways to ask “which employees manage someone?”:

Included with a Formula Quest membership

This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons