DbSchema Database Designer

SQL SELECT DISTINCT Statement Explained



SQL, or Structured Query Language, is the cornerstone of many data-related operations. One of its most powerful statements is the SELECT DISTINCT statement. This tutorial aims to guide fresh students through the nuances of this statement, ensuring a robust understanding by its conclusion.

1. Introduction to SQL SELECT DISTINCT

The SELECT DISTINCT statement is pivotal when you aim to retrieve unique values from a table, thereby filtering out duplicates.

Example:

Sample Table (Students):

StudentIDName
1Alice
2Bob
3Alice

Query:

SELECT DISTINCT Name FROM Students;

Result:

Name
Alice
Bob

Explanation:

The result showcases unique names. Even though 'Alice' appears twice in the table, the DISTINCT clause ensures it appears only once in the result.

2. SQL DISTINCT on Multiple Columns

This allows for unique combinations of several columns, rather than unique values from just one column.

Example:

Sample Table (Orders):

OrderIDProductColor
1AppleRed
2AppleGreen
3OrangeOrange
4AppleRed

Query:

SELECT DISTINCT Product, Color FROM Orders;

Result:

ProductColor
AppleRed
AppleGreen
OrangeOrange

Explanation:

The result provides unique combinations of Product and Color. Notice how Apple + Red only appears once, despite being present twice in the original table.

3. SQL DISTINCT on (Multiple) NULL Values

NULL values are always considered unique from one another in SQL.

Sample Table (Employees):

IDNameAddress
1JohnNULL
2Michael123 Lane St
3SarahNULL

Query:

SELECT DISTINCT Address FROM Employees;

Result:

Address
NULL
123 Lane St

Explanation:

While there are two NULL values in the Address column of the table, the result only displays one. This illustrates the principle that all NULLs are distinct.

4. SQL DISTINCT vs GROUP BY

While both provide unique results, GROUP BY is designed for aggregating data based on specific columns.

Example:

Sample Table (ProductSales):

ProductSaleDateUnitsSold
Apple2023-01-015
Apple2023-01-013
Orange2023-01-022
Apple2023-01-034

Using DISTINCT:

To retrieve unique combinations of Product and SaleDate:

SELECT DISTINCT Product, SaleDate FROM ProductSales;

Result:

ProductSaleDate
Apple2023-01-01
Orange2023-01-02
Apple2023-01-03

Using GROUP BY:

To aggregate the total units sold for each product on each date:

SELECT Product, SaleDate, SUM(UnitsSold) as TotalUnits
FROM ProductSales
GROUP BY Product, SaleDate;

Result:

ProductSaleDateTotalUnits
Apple2023-01-018
Orange2023-01-022
Apple2023-01-034

Explanation:

  • Using DISTINCT, we can fetch unique combinations of Product and SaleDate.
  • With GROUP BY, not only can we group data based on columns, but we can also perform aggregate functions (like SUM) to understand data at a more granular level. In this case, we aggregated the total units sold for each product on a specific date.

5. SELECT COUNT on DISTINCT

To measure the number of unique values in a column, DISTINCT can be paired with the COUNT function.

Query:

SELECT COUNT(DISTINCT Name) FROM Students;

Result:

COUNT(DISTINCT Name)
2

Explanation:

The result indicates there are two unique student names in the 'Students' table.

7. SQL SELECT VS SELECT DISTINCT

AspectSELECT StatementSELECT DISTINCT Statement
PurposeFetches all data from specified columns.Fetches unique data, removing duplicates.
PerformanceGenerally faster as it retrieves data as-is.Might be slower on large datasets due to filtering.
Use CasesWhen needing all data, including duplicates.When distinct values are required.
Example ResultCould get 'Apple', 'Apple', 'Orange'.Would get 'Apple', 'Orange'.

8. Common Mistakes

  • Applying DISTINCT to the wrong columns.
  • Inefficient performance due to excessive use of DISTINCT on large datasets.
  • Misinterpretation of NULL values with DISTINCT.

9. Frequently Asked Questions (FAQs)

  • Q: Can I combine DISTINCT with other SQL functions?

    A: Yes, DISTINCT can be used alongside functions like COUNT, SUM, and others.

10. Practice Questions

  1. Retrieve unique colors of products in the "Orders" table.
  2. Determine the count of unique student names in the "Students" table.
  3. Extract unique combinations of product and color for the product "Apple" from the "Orders" table.

To truly master the SELECT DISTINCT statement in SQL, regular practice and real-world application are essential. This guide offers a comprehensive overview, but hands-on experience will solidify your understanding.

DbSchema Database Designer
Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.