Labs ICT
โญ Pro Login

DISTINCT

Ever looked at a column and realized the same values keep showing up over and over? That is where DISTINCT comes in. It removes duplicate values from your results.

Basic DISTINCT

Let us say you want to know which cities your students are from. If you just do:

SELECT city FROM students;

You will get duplicates because multiple students are from the same city. Instead:

SELECT DISTINCT city FROM students;

Now you get each city only once. Simple but incredibly useful.

DISTINCT on Multiple Columns

You can use DISTINCT with multiple columns. It checks for unique combinations:

SELECT DISTINCT city, age FROM students;

This gives you every unique combination of city and age. If two students are both 22 and from Kano, it only shows up once.

Counting Distinct Values

A common pattern is to count how many distinct values exist:

SELECT COUNT(DISTINCT city) AS unique_cities FROM students;

This tells you how many different cities your students come from, without listing them all.

๐Ÿงช Quick Quiz

What does DISTINCT do in SQL?