SQL distinct – How to use select distinct in sql with examples

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:


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:


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

Was this article helpful?

Related Articles

Leave A Comment?