Return users who have orders.
Section: subqueries and ctes
Use EXISTS for correlated filtering
sql
sql
SELECT u.id, u.email
FROM users u
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.user_id = u.id
);Explanation
`EXISTS` is often clearer and more efficient than counting when you only care whether a match exists.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing subqueries and ctes
Use a CTE to isolate recent orders
Create a readable query pipeline with `WITH`.
Left join to keep unmatched parent rows
Return all rows from the left table and matching rows from the right table.