SQL Subquery vs JOIN
Choose a subquery or JOIN from the output grain and columns the question needs.
What & Why
A correlated EXISTS subquery answers an eligibility question without multiplying the outer rows, while a JOIN returns the matching child columns and therefore may produce several rows per parent.
The Queryflo examples intentionally have different grains: one row per eligible feature versus one row per positive feedback record. They demonstrate when each shape is appropriate, not interchangeable ways to return the same result.
See How It Works
Compare feature eligibility with the detailed positive-feedback rows behind it.
| id | name | description | team | released_at | deprecated_at |
|---|---|---|---|---|---|
| 1 | Activation Checklist | Guides new users through setup | growth | 2024-01-20 | NULL |
| 2 | CSV Export | Exports report data | platform | 2024-02-14 | NULL |
| 3 | Invite Nudges | Prompts workspace collaboration | growth | 2024-03-05 | NULL |
| 4 | Legacy Dashboard | Original reporting surface | analytics | 2023-08-10 | 2024-06-01 |
| id | user_id | feature_id | rating | nps_score | comment | created_at |
|---|---|---|---|---|---|---|
| 1 | 101 | 1 | 5 | 9 | Clear and useful | 2024-01-25 12:15:00+00 |
| 2 | 102 | 2 | 4 | NULL | Export worked well | 2024-02-20 15:40:00+00 |
| 3 | 103 | 1 | 4 | 7 | Helpful setup flow | 2024-03-18 09:05:00+00 |
| 4 | 104 | 3 | 3 | NULL | Needs clearer timing | 2024-04-08 17:30:00+00 |
| feature_id | feature | positive feedback exists |
|---|---|---|
| 1 | Activation Checklist | yes |
| 2 | CSV Export | yes |
| 3 | Invite Nudges | no |
| 4 | Legacy Dashboard | no |
EXISTS returns one row per eligible feature and does not expose child detail.
-- One row per eligible feature
SELECT
f.id AS feature_id,
f.name
FROM product.features f
WHERE EXISTS (
SELECT 1
FROM product.feedback fb
WHERE fb.feature_id = f.id
AND fb.rating >= 4
)
ORDER BY f.id;
-- One row per positive feedback record
SELECT
f.id AS feature_id,
f.name,
fb.rating,
fb.created_at
FROM product.features f
JOIN product.feedback fb ON fb.feature_id = f.id
WHERE fb.rating >= 4
ORDER BY f.name, fb.created_at, fb.id;This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario