Prev     Next

DISTINCT or UNIQUE keyword in SQL is used with SELECT statement/query to select only unique records from a table, i.e. it eliminates all duplicate records from a table. Syntax of SQL DISTINCT/UNIQUE statement is given below.

SQL Syntax for DISTINCT/UNIQUE statement:

Syntax for SQL DISTINCT / UNIQUE SELECT DISTINCT column_name1, column_name2, etc
FROM table_name;
SELECT UNIQUE column_name1, column_name2, etc
FROM table_name;

Example for how to use SQL DISTINCT/UNIQUE keyword:

Please consider the following table with few records as given below.

Table name (for example): student
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows


First, we will execute a select query for fetching age records in the above table without a DISTINCT / UNIQUE keyword. Then, we can execute with DISTINCT/UNIQUE keyword to compare the results.

1. Select query without DISTINCT/UNIQUE keywords:

SQL query:

SELECT Age from student;

SQL query Output:


2. Select query with DISTINCT/UNIQUE keywords:

SQL query:

SELECT DISTINCT Age from student;


SELECT UNIQUE Age from student;

SQL query Output:


SQL SELECT query with DISTINCT/UNIQUE keyword retrieves only unique data from the column ‘Age’. There are only 2 distinct values ‘25’ and ‘27’ available in the table. Whereas, SQL SELECT query fetches all 4 records from the table if we do not use DISTINCT/UNIQUE keywords.

Prev     Next

Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com

Share this website !!!
Facebook Twitter Linkedin

We Support JALLIKATTU !!!
By Fresh2Refresh.com
Show Your Support as well. Thanks!