Can we apply
DISTINCT to a
SELECT query with multiple columns?
DISTINCT clause can be applied to any valid
SELECT query. It is important to note that
DISTINCT will filter out all rows that are not unique in terms of all selected columns.
Feel free to test this out in the editor to see what happens!
Let’s assume that in the Codecademy database there is a table
bugs which stores information about opened bug reports. It might have columns like
report_url, etc. For the purpose of this example, let’s say that this is our table:
id course_id exercise_id reported_by 1 5 4 Tod 2 5 4 Alex 3 5 3 Roy 4 5 4 Roy 5 7 4 Alex 6 7 8 Tod 7 14 2 Alex 8 14 4 Tod 9 14 6 Tod 10 14 2 Roy
Community Manager would like to know the names of the users who have reported bugs in order to send them a special Thank You note. We can use a
SELECT query with
DISTINCT keyword to pick unique values from the
> SELECT DISTINCT reported_by FROM bugs; reported_by Alex Tod Roy
Awesome! Exactly what we were expecting!
Our coworker would like to know in which exercises bugs have been reported. This gets trickier because now we have to query two columns:
exercise_id. Let’s try to use the same approach as before:
> SELECT DISTINCT course_id, exercise_id FROM bugs; course_id exercise_id 14 2 5 4 14 4 14 6 5 3 7 4 7 8
Is this the result we were hoping for? Yes. It is true that there are duplicated values in the
exercise_id, but every row is unique (there are no two rows with the same value in