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.


Search Keys

  • sql distinct clause
  • select distinct oracle
  • distinct in sql
  • mssql distinct
  • sql server distinct
  • select distinct sql
  • sql distinct join
  • list distinct
  • distinct query in sql
  • not distinct sql
  • select distinct multiple columns
  • distinct on multiple columns
  • where distinct
  • sql distinct on multiple columns
  • distinct multiple columns
  • distinct values
  • 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 Programming Geetha
2 Simply MySql 70 2008 Database Siva Kumar
3 Understanding the New SQL 75 2012 Optimization Padmavathi
4 SQL: The Complete Reference 155 2014 Performance Harish Karthik
5 Mastering Oracle SQL 65 2008 Optimization Padmavathi
6 Mastering Oracle SQL 105 2012 Security Dharan
7 Programming With MySQL 200 2012 Security Chandra
8 Easy Oracle PL/SQL Programming 120 2015 Optimization Varshini Kutty
9 Jump Start MySQL 190 2013 Optimization Harish Karthik
10 Pro MySql Administration 84.22 2010 Security Hari Krishnan
11 The Gurus Guide To SQL Server 90 2007 Database Balan
12 Oracle Fundamentals 175 2012 Optimization Pandurengan

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
Performance
Programming
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
Optimization 2013
Optimization 2015
Performance 2014
Programming 2007
Security 2010
Security 2012

Search Keys

  • distinct query
  • sql not distinct
  • sql distinct command
  • sql distinct where
  • distinct sql query example
  • distinct column
  • distinct example
  • sql distinct keyword
  • sqlite distinct
  • postgresql distinct
  • count distinct oracle
  • count distinct access
  • select count distinct oracle
  • sql server count distinct
  • distinct postgresql
  • postgresql distinct on
  • sql count distinct values
  • sql distinct on two columns
  • sql query to select unique records
  • insert values in sql