100. Bought Product X But Not Product Y
MediumAdvanced FilteringEXISTSNOT EXISTSAnti-join
Two correlated subqueries express 'has one thing and lacks another': EXISTS (...) asserts a matching purchase exists, while NOT EXISTS (...) asserts none exists. Together they find rows satisfying a positive and a negative condition without duplicating customers.
The purchases table records (customer, product) rows.
Task: find customers who bought 'Camera' but did not buy 'Lens'.
Return one column customer, ordered by customer ascending.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.