Removing duplicates with DISTINCT

Stage 1: Basics · Lesson 4 of 17 · 3 min read · free, interactive

Many rows can share a value. Selecting country returns one row per player, so countries repeat:

SELECT country FROM players;

SELECT DISTINCT removes duplicate rows from the result:

SELECT DISTINCT country FROM players;

NULL (unknown) appears once too — DISTINCT treats NULLs as equal.

With several columns, DISTINCT removes rows where all the selected columns repeat together:

SELECT DISTINCT player_id, item_id FROM purchases;

Practice

Each answer runs in your browser and is checked on hidden data.

Previous: Sorting with ORDER BY and LIMIT · Next: Missing values: NULL · All lessons