Lesson 3 of 12 · 20 min
EXISTS: does at least one matching row exist?
By the end of this lesson you can write a correlated EXISTS subquery, explain why it returns each outer row at most once, and spot a missing or broken correlation.
This lesson uses Rawabi Bank: customers, their accounts (accounts.customer_id is the primary holder), every holder of each account including joint holders (account_holders), and two years of transactions.
Suppose Compliance asks: which customers hold at least one dormant account? You need nothing from the account itself, only the answer to a yes/no question per customer. That is exactly what EXISTS is for.
EXISTS (subquery) is true if the subquery returns at least one row and false if it returns none. It does not care what the rows contain, only whether there are any.
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