SQL SELECT DISTINCT Keyword


The SQL SELECT DISTINCT keyword is used to fetch only unique (without duplicates) values or records.

It removes all the duplicate records from query results while fetching data from table.

It can filter only unique data for a single column value or an entire records from query result.

The SQL Distinct keyword will not ignore any NULL values from query result.

You can also search these topics using sql distinct clause, select distinct oracle, sql server distinct, select distinct sql, sql distinct join, distinct query in sql, select distinct multiple columns, distinct on multiple columns, sql distinct on multiple columns, distinct multiple columns, sql distinct example, sql server select distinct multiple columns, where distinct sql, select distinct example.

SQL SELECT DISTINCT Syntax

The basic syntax of SQL SELECT DISTINCT statement is as follows:


SELECT DISTINCT column_name1 or expression1, column_name2  or expression2
FROM table_name;

  • column_name or expressions - The table columns or calculations(formula) that you want to extract.
  • tables - The tables that you want to fetch records from.

Sample Database Table - Books

BookId BookName BookPrice RelYear DomainName AuthorName
1 From Access To SQL Server 145 2007 Database Geetha
2 Simply MySql 70 2008 Database Siva Kumar
3 Understanding the New SQL 75 2012 Optimization Padmavathi
4 SQL: The Complete Reference 155 2012 Optimization Harish Karthik
5 Mastering Oracle SQL 65 2008 Optimization Padmavathi
6 Mastering Oracle SQL 105 2012 Security Dharan

SQL SELECT DISTINCT Example - With Single Column

The following SQL SELECT statement selects all the unique data of "DomainName" from "Books" Table.


SELECT DISTINCT DomainName 
FROM Books;

The result of above query is:

DomainName
Database
Optimization
Security

SELECT DISTINCT Example - With Multiple Column

The following SQL SELECT statement selects all the unique data of "DomainName" and "RelYear" from "Books" Table.


SELECT DISTINCT DomainName, RelYear 
FROM Books;

The result of above query is:

DomainName RelYear
Database 2007
Database 2008
Optimization 2008
Optimization 2012
Security 2012
You can also search these topics using sql distinct command, sql distinct where, distinct sql query example, sql distinct keyword, count distinct oracle, count distinct access, select count distinct oracle, sql server count distinct, postgresql distinct on, sql count distinct values, sql distinct on two columns, sql query to select unique records, insert values in sql.