DISTINCT

Each value once with DISTINCT

SQLite in your browser
Lesson concept What you'll do
1 / 4

List the different values in a column, each once.

SELECT DISTINCT drops the repeats.

The database
9 rows
book_idtitleauthorgenreyearpages
1A Study in ScarletArthur Conan DoyleMystery1887108
2Charlie and the Chocolate FactoryRoald DahlChildren1964155
3FrankensteinMary ShelleyHorror1818280
4DraculaBram StokerHorror1897418
5Pride and PrejudiceJane AustenRomance1813432
6The Hound of the BaskervillesArthur Conan DoyleMystery1902256
7EmmaJane AustenRomance1815474
8MatildaRoald DahlChildren1988240
9The Da Vinci CodeDan BrownThriller2003454
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How DISTINCT works

  • What does DISTINCT do?

    SELECT DISTINCT genre FROM books;

    It removes repeated rows from the result, so each different value comes back once.

  • Where does DISTINCT go?

    Straight after SELECT, before the columns. It applies to the whole row, not just the first column.

  • What about two columns?

    SELECT DISTINCT genre, author keeps every different pair of genre and author. Two rows only count as repeats when every column matches.

  • Is there another way?

    GROUP BY genre gives the same list, and it's how you count per genre later (COUNT(*)). For a plain list, DISTINCT says what you mean.

  • How is this like Excel?

    It's like the UNIQUE function: =UNIQUE(D2:D12). The original rows stay as they were (Remove Duplicates, by contrast, deletes them from the sheet).

Know someone fighting with a spreadsheet? Send them this page.