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
The SQL SELECT DISTINCT statement retrieves unique values from a column, removing duplicates. It’s commonly used to analyze data or reduce redundancy in query results.
You can apply SELECT DISTINCT to multiple columns by listing them in the SELECT statement. For example:
sqlCopy codeSELECT DISTINCT column1, column2 FROM table_name;
This ensures unique combinations of the specified columns.
Yes! Advanced examples include combining SELECT DISTINCT with functions like COUNT, SUM, and JOINs to perform more complex queries. These examples are detailed in our tutorial.
You can use COUNT with a subquery that includes SELECT DISTINCT. For instance:
sqlCopy codeSELECT COUNT(*) FROM (SELECT DISTINCT column_name FROM table_name) AS subquery;
This counts the number of unique values in a column.
- Using it on unnecessary columns, which can reduce performance.
- Applying it inappropriately with aggregate functions.
- Forgetting to specify all desired columns in the SELECT statement.
Yes, our SQL SELECT DISTINCT Tutorial offers a free, detailed guide for beginners and advanced users.
Yes, you can add conditions to filter the results. For example:
sqlCopy codeSELECT DISTINCT column_name FROM table_name WHERE condition;
While SELECT DISTINCT is useful for eliminating duplicates, it can be resource-intensive for large datasets. Optimizing indexes and limiting the number of columns can improve performance.
Yes, you can use SELECT DISTINCT with JOINs to fetch unique rows based on joined tables. For example:
sql
Copy code
SELECT DISTINCT a.column1, b.column2
FROM table1 a
JOIN table2 b ON a.id = b.id;
Tools like SQL Server Management Studio (SSMS) and MySQL Workbench can help analyze and debug queries by providing execution plans and performance insights.