Skip to content
We areTOP 1%

Article Views I

EasyLeetCodeSQL: SELECT & Filtering
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

Compare two columns of the same row, then use DISTINCT to remove repeats and ORDER BY to sort the answer.

Example: payments(payer_id, payee_id, amount) — who paid themselves?

payer_id | payee_id | amount
7        | 7        | 100
3        | 5        | 50
7        | 7        | 20
2        | 2        | 10
SELECT DISTINCT payer_id AS id
FROM payments
WHERE payer_id = payee_id
ORDER BY id;   -- returns 2, 7

More in SQL: SELECT & Filtering

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account