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]
6. What is the total count of distinct ModifiedDate values in the dataset from Person_BusinessEntity table name ?
7. What are the unique GroupNames present in the HumanResources_Department table?
8. What are the unique DepartmentIDs for the GroupName ‘Executive General and Administration’ in the HumanResources_Department table?
9. What are the unique shiftIDs in the HumanResources EmployeeDepartmentHistory table?
10. What are the unique cultureID in the Production ProductModelProductDescriptionCulture table?
YouTube Reference :
1) SQL Select Distinct in Hindi/Urdu
2) SQL Select Distinct in English