Menu
CoddyTech

Unique Results with DISTINCT

Part of the First Queries section of Coddy's PostgreSQL journey. Lesson 12 of 20.

DISTINCT removes duplicate output rows. It applies to the full list of selected values, not to one chosen column. Filtering still happens before duplicate removal.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
SELECT DISTINCT category FROM products ORDER BY category;

The result lists books, home and travel once each even though several products belong to each category. Adding id would make every output pair different because every product has its own id.

SELECT DISTINCT returns each distinct combination of selected values once.

challenge icon

Challenge

Easy

Return each price used by an in-stock product exactly once. Name the column price and sort it ascending.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Queries

Practice on your own: PostgreSQL playground