CodeBlanks Logo Icon CodeBlanks

DISTINCT on one column

List unique values of a single column.

Multi-column DISTINCT

DISTINCT works with multiple columns. Rows are unique by the combination of all columns.

SELECT DISTINCT city, category FROM customers;

This query returns rows unique by the city and category combination.

city, category is selected from customers (DISTINCT removes duplicates).

Multi-column DISTINCT — details

Each column combination appears once. Order does not matter; the same combination in a different order is also deduplicated.

Practice Multi-column DISTINCT

Select unique city and category combinations from customers.

Starter code

query.sql
-- two-column distinct
SELECT
FROM customers

Sample solution

query.sql
SELECT DISTINCT city, category
FROM customers

Expected output

city    category
İstanbul altın
İzmir   gümüş

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Two-column distinct

Question
SELECT DISTINCT ____
FROM customers;
Answer
SELECT DISTINCT city, category
FROM customers;

Explanation: city, category is the right answer.

2 What does DISTINCT city, category deduplicate by?

  • By the combination of both columns Correct answer
  • Only by city
  • Only by category
  • Random

Explanation: DISTINCT deduplicates by all columns combined.

3 DISTINCT on one column: match the terms

Term Meaning
DISTINCT deduplicate
comma separate columns
SELECT columns
FROM table

All lessons in Beginner