SQL Distinct Keyword

    When we want to fetch or display unique data from the table then we use the SQL DISTINCT keyword. The only purpose of the DISTINCT keyword to eliminate the duplicate record and select only the unique ones.

    DISTINCT Keyword Syntax

    Follow this syntax to use the DISTINCT keyword:

    SELECT DISTINCT column1, column2, ...
    FROM table_name;

    Example Consider this table of Students:

    +------+--------+------+--------+-------+----------+
    | id   | name   | age  | grades | marks | Trade    |
    +------+--------+------+--------+-------+----------+
    |    1 | Luffy  |   16 | A      |   970 | Science  |
    |    2 | Naruto |   18 | A      |   960 | Humanity |
    |    3 | Zoro   |   20 | A      |   940 | Commerce |
    |    4 | Sanji  |   21 | B      |   899 | Humanity |
    |    5 | Nami   |   17 | B      |   896 | Science  |
    |    6 | Robin  | NULL | B      |   860 | Humanity |
    +------+--------+------+--------+-------+----------+

    Query: Select all the unique Trades from the students;

    SELECT DISTINCT trade
    FROM students;
    

    Output

    +----------+
    | trade    |
    +----------+
    | Science  |
    | Humanity |
    | Commerce |
    +----------+

    Summary

    • The DISTINCT keyword is used to show the unique data.
    • It often used with the SELECT clause.

    People are also reading: