TechnoLearnAcademy

TECHNOLEARN

SQL DISTINCT STATEMENT

SQL SELECT DISTINCT statement is used to retrieve only unique records from the Database table.
In a column, there might be some values that are repeated. To display only the unique values, we use the Select Distinct command.

Syntax:

                SELECT DISTINCT column1, column2 , column3 , ,,, from  tablename;
Let’s understand the select distinct statement with an example. For demonstration purposes, we will be using the table emp.

Example:

   Objective: There are multiple department names in the department column. Select only the unique department name.
                      SELECT DISTINCT Department FROM Tbl_Emp;
    Output:

Example:

   Objective:  There are multiple location names in the department column. Select only the unique location values.  
                       SELECT Distinct Location FROM Tbl_Emp;
    Output:
    How can we use the count function to obtain unique values?

Example:

   Objective:  There are multiple location names in the department column. Find the count of a distinct location.
                       SELECT COUNT(Distinct Location) as [Unique Location] FROM Tbl_Emp;
    Output:
Scroll to Top