Skip to content
In This Tutorial
SQL Distinct clause video tutorial
The distinct statement
- The distinct statement is used to fetch the unique records from the table.
- The distinct SQL clause removes duplicate records from fetched data.
- The distinct clause is used with the Select statement
Syntax of SQL Distinct
The basic syntax of using the SQL select with distinct is:
Select Distinct column1, Column 2 from table_name
Where col1=val1 ;
Note: you can use one or more columns with the distinct clause.
Examples of using select with distinct statement
Following examples shows how to use the Distinct clause in the select statement. In our examples, we will use the tbl_emp_salary_paid table that stores employees salaries.
Click on demo links below to see the complete table data and records fetched after the distinct clause.
Example of using distinct clause with simple select statement
As our example table contains repeated employee names in the emp_name column (see fig. below by clicking the demo link).
The select – distinct statement:
select distinct emp_name from tbl_emp_salary_paid
The distinct Example with count function
This example returns a total number of unique employee names exist in the emp_name column by using the count function with SQL DISTINCT clause.
The Distinct statement:
select count(DISTINCT emp_name) as Total_unique_employees from tbl_emp_salary_paid
As you can see in the select distinct query, the total number of unique employee names are returned as 5.
Related to this: SQL count