DISTINCT vs. GROUP BY
It’s worth noting that using DISTINCT, particularly in aggregations, can slow your queries down quite a bit.
- DISTINCT
- GROUP BY
- SUM
- unique values
As mentioned in Lesson 6.19 Video: DISTINCT:
DISTINCT - Expert Tip
It’s worth noting that using DISTINCT, particularly in aggregations, can slow your queries down quite a bit.
Before we talk about the difference between DISTINCT and GROUP BY let's explain each one of them.
The SQL DISTINCT clause is used to remove duplicates from the result set of a SELECT statement.
Notes:
- When only one expression is provided in the DISTINCT clause, the query will return the unique values for that expression.
- When more than one expression is provided in the DISTINCT clause, the query will retrieve unique combinations for the expressions listed.
- In SQL, the DISTINCT clause doesn't ignore NULL values. So when using the DISTINCT clause in your SQL statement, your result set will include NULL as a distinct value.
Example - Suppliers Table
|supplier_id|supplier_name |city |state |
|-----------|------------------|----------------|----------|
|100 |Microsoft |Redmond |Washington|
|200 |Google |Mountain View |California|
|300 |Oracle |Redwood City |California|
|400 |Kimberly-Clark |Irving |Texas |
|500 |Tyson Foods |Springdale |Arkansas |
|600 |SC Johnson |Racine |Wisconsin |
|700 |Dole Food Company |Westlake Village|California|
|800 |Flowers Foods |Thomasville |Georgia |
|900 |Electronic Arts |Redwood City |California|Finding Unique Values in a Column
For getting all unique states from suppliers table:
SELECT DISTINCT state
FROM suppliers
ORDER BY state;It results 6 records:
state |
----------|
Arkansas |
California|
Georgia |
Texas |
Washington|
Wisconsin |This example would return all unique state values from the suppliers table and remove any duplicates from the result set. As you can see, the state of California only appears once in the result set instead of four times.
Finding Unique Values in Multiple Columns
For getting unique values from multiple columns (states, city):
SELECT DISTINCT city, state
FROM suppliers
ORDER BY city, state;It results 8 records:
|city |state |
|----------------|----------|
|Irving |Texas |
|Mountain View |California|
|Racine |Wisconsin |
|Redmond |Washington|
|Redwood City |California|
|Springdale |Arkansas |
|Thomasville |Georgia |
|Westlake Village|California|This example would return each unique city and state combination. In this case, the DISTINCT applies to each field listed after the DISTINCT keyword. As you can see, Redwood City, California only appears once in the result set instead of twice.
GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.
Example - Employees Table
We will use this table to select from.
|employee_number|last_name|first_name|salary|dept_id|
| | | | | |
|1001 |Smith |John |62000 |500 |
|1002 |Anderson |Jane |57500 |500 |
|1003 |Everest |Brad |71000 |501 |
|1004 |Horvath |Jack |42000 |501 |Using GROUP BY with the SUM Function
Query:
SELECT dept_id, SUM(salary) AS total_salaries
FROM employees
GROUP BY dept_id;Result (2 records selected):
|dept_id|total_salaries|
|-------|--------------|
|500 |119500 |
|501 |113000 |In this query, we've used the SUM function to add up all of the salaries for each _dept_id_ and we've aliased the results of the SUM function as _total_salaries_. Because the _dept_id_ is not encapsulated in the SUM function, it must be listed in the GROUP BY clause.
For more information about GROUP BY and Aggregations check this post
A DISTINCT and GROUP BY usually generate the same query plan, so performance should be the same across both query constructs. GROUP BY should be used to apply aggregate operators to each group. If all you need is to remove duplicates then use DISTINCT. If you are using sub-queries execution plan for that query varies so in that case you need to check the execution plan before making decision of which is faster.
So, how do you decide which SQL command to use? GROUP BY is required if you're aggregating data, but in many cases, DISTINCT is simpler to write and read if you aren't aggregating data.
Recap
- Use
DISTINCTif you just want to remove duplicates. - Use
GROUP BYif you want to apply aggregate operators (MAX,SUM,COUNT,AVG, ..., or aHAVINGclause). - Check the query time before using any of them, If and only if you won't apply aggregations.