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.
- Write the query: List every item
category, each one only once. Row order does not matter. - Write the query: Which players have bought something? Return each
player_idfrompurchasesonly once. Row order does not matter.
Previous: Sorting with ORDER BY and LIMIT · Next: Missing values: NULL · All lessons