JournalSQLFiltering and sets2 min

For Those Who Do Not Know DISTINCT

The DISTINCT keyword ignores duplicate rows so that each row is unique on its own. We only write it right after the SELECT keyword.

  • DISTINCT
  • duplicate rows
  • SELECT

The DISTINCT keyword ignores duplicate rows so that each row is unique on its own. We only write it right after the SELECT keyword.

It has a strong similarity to GROUP BY, though the difference between them is bigger. If you don't know what GROUP BY is or what it does, read this topic. I won't go into those details right now and will try to clarify them at the end; the most important thing is to understand how DISTINCT works in theory and practice.

Suppose we have a students table like this:

photo_2018-02-12_07-39-12

If we want to get the student name values without duplicates and get rid of all those repeated "Mariam" rows, we write:

SELECT DISTINCT name
FROM students

The output will be:

photo_2018-02-12_07-39-24

Notice that the name Mariam was repeated 4 times, but thanks to DISTINCT we were able to overcome this problem 馃檪

Now, what if we want to get the grade values that students received without duplicates (maybe everyone got an A)? We write:

SELECT DISTINCT grade
FROM students

Unfortunately, we find the output is: Click on the image to view it in full

photo_2018-02-12_07-39-20

This means all grades were present in the table鈥攊n other words, every grade was earned by one or more students.

Let's try something better to make the picture clearer. If I ask you to get the student name and age without duplicates, you write:

SELECT DISTINCT name, age
FROM students

The output will be: Click on the image to view it in full

photo_2018-02-12_07-37-57

Notice that the name Mariam was repeated 4 times in the students table, but in the output we found it repeated only 3 times. Because there were two Mariams with the same age, which is 16 years old. And DISTINCT, as we said, prevents duplicate rows so that each row is unique. Of course, this means there can be duplicates in the name column or the age column individually, but not a duplicate of the entire row itself.

Let's try one last example to confirm the idea. If I ask you to get the student name and grade without duplicates, you write:

SELECT DISTINCT name, grade
FROM students

The output will be:

photo_2018-02-12_07-39-08

Notice again that the name Mariam is repeated 3 times, not 4 times as in the students table, because two Mariams got an A grade.

After you see what GROUP BY does and how we use it with examples from this topic, I will briefly talk about the difference between DISTINCT and GROUP BY because it doesn't need a separate topic.

When do we use GROUP BY? When I need to calculate something like COUNT, AVG, or SUM based on grouping those duplicates.

When do we use DISTINCT? When I just want to ignore duplicates and nothing more鈥攚hen I don't need the duplicates for anything and at the same time don't want to calculate anything.

If you want to understand the difference in more detail, read this topic: DISTINCT vs. GROUP BY

Because DISTINCT ignores duplicates rather than grouping/aggregating them.