Skip to content
2 examples to learn SQL distinct clause
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
See distinct example online
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:
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.
Related to this: SQL count
Leave A Comment?