Quick Reach
The distinct statement
- The distinct statement is used to fetch unique records from the table.
- The distinct SQL clause removes duplicate records from fetched data.
- The distinct statement is used with the Select statement
See distinct example online
Syntax of SQL Distinct
The basic syntax of using SQL select with distinct is:
Select Distinct column1, Column 2 from table_name
Where col1=val1 ;
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 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 complete table data and records fetched after the distinct clause.
Example of using distinct SQL 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:
1
|
select distinct emp_name from tbl_emp_salary_paid
|
Demo Table with SQL select and distinct
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:
1
|
select count(DISTINCT emp_name) as Total_unique_employees from tbl_emp_salary_paid
|
See this example online with graphic
As you can see in the select distinct query, the total number of unique employee names are returned as 5.
Also see SQL count
Leave A Comment?