SQL Select Distinct

HTML
CSS
C#
SQL

SQL Select Distinct

The SQL DISTINCT command is used with the SELECT keyword to retrieve only distinct or unique data. In a table, there may be a chance to exist a duplicate value and sometimes we want to retrieve only unique values. In such scenarios, SQL SELECT DISTINCT statement is used.

Example:

SELECT DISTINCT column_name , column_name  FROM  table_name; 

Let’s try to understand it by the table given below

Here is a table of students from where we want to retrieve distinct information For example: distinct home-town.

SELECT DISTINCT home_town  FROM students;

Now, it will return two rows.

Course Video

1. Write a SQL query to get unique cities AND PostalCode from Person_Address table.

[OUTPUT: 24 ROWS]

2. Write a SQL query to get unique CountryRegionCode from Sales_SalesTerritory table.

[OUTPUT: 6 ROWS]

3. Write a SQL query to get unique JobTitle from HumanResources_Employee table.

[OUTPUT: 24 ROWS]

4. Write a SQL query to get unique Title from Person_Person table.

[OUTPUT: 3 ROWS]

5. Write a SQL query to get unique productID from Sales_SalesOrderDetail table.

[OUTPUT: 45 ROWS]