SQL Distinct statements with query example – Distinct statements in SQL is used to retrieve unique / different records from a column. It can return records from single or multiple columns both.

POINTS:

  • Remove the duplicate records from the result set.
  • It is used with only SQL SELECT Statement.
  • Distinct always retrieves first value from a repeating group (i.e. if multiple records with same data)
  • It requires a sort for finding repeating values.
  • Distinct can operate on single column or multiple columns.
  • Every row obtains unique data for specified set of columns to apply distinct keyword.

SQL Distinct Syntax:

SQL Distinct syntax to get unique data by removing duplicate data from result set. The distinct query we can apply for single column or multiple columns of a table in SQL.

Distinct for Single Column

Syntax
SELECT DISTINCT [column_name]  FROM [table_name]   [WHERE [condition]];

Example
Query to get unique price from PRICE column from BOOKS table.

Query
SELECT DISTINCT PRICE FROM BOOKS;

The above query results the price column with unique values by removing the duplicate data from result set.

Distinct for Multiple Columns

Syntax:
SELECT DISTINCT [column_name1],[column_name2],…………………….. FROM  [TABLE_NAME]
[ WHERE [CONDITION]]

Related Posts