DISTINCT
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Introduction to DISTINCT
The DISTINCT keyword in SQL is used to return only unique values in the result set. It removes all duplicate rows from your query results.
Basic syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;Let's see how DISTINCT works with our sales data:
SELECT DISTINCT product_name
FROM sales
ORDER BY product_name;You can also use DISTINCT with multiple columns. In this case, SQL looks for unique combinations of those columns:
SELECT DISTINCT product_name, region
FROM sales
ORDER BY product_name, region;You can also combine DISTINCT with COUNT() to count the number of unique values:
SELECT COUNT(DISTINCT region) AS region_count
FROM sales;DISTINCT vs GROUP BY
DISTINCT is simpler than GROUP BY and is used only to remove duplicates. GROUP BY is more powerful because it can be combined with aggregate functions to calculate summaries for each group.
Exercise 2 β Understanding DISTINCT
What would the following query return?
SELECT DISTINCT category FROM sales;Hint : Think about what the DISTINCT keyword does - it removes duplicates from the results.
Exercise 3 β Complete the DISTINCT Query
Complete the SQL query to show all unique regions where products in the 'Medicine' category are sold.
Hint : Think about which keyword removes duplicates, and remember to specify the correct category in the WHERE clause.
Exercise 4 β GROUP BY vs DISTINCT
Both GROUP BY and DISTINCT can be used to remove duplicates, but they serve different purposes:
- DISTINCT simply removes duplicate rows from the result
- GROUP BY groups rows and allows you to apply aggregate functions to each group
Let's see the difference with an example:
SELECT DISTINCT category, region
FROM sales
ORDER BY category, region;Now let's try with GROUP BY:
SELECT category, region
FROM sales
GROUP BY category, region
ORDER BY category, region;Notice that both queries return the same results in this case. But if we want to also get some aggregate information, we need GROUP BY:
SELECT category, region, COUNT(*) AS sales_count
FROM sales
GROUP BY category, region
ORDER BY category, region;Exercise 5 β Match Queries with Results
Match each SQL query with its expected result:
SELECT DISTINCT product_name FROM sales;SELECT product_name, COUNT(*) FROM sales GROUP BY product_name;SELECT COUNT(DISTINCT region) FROM sales;SELECT DISTINCT category, region FROM sales;Exercise 6 β DISTINCT with COUNT
What is the difference between these two queries?
SELECT COUNT(*) FROM sales;
SELECT COUNT(DISTINCT product_name) FROM sales;Hint : Pay attention to how COUNT() works with and without DISTINCT - they count different things.
Exercise 7 β The medicine shelf
The manager needs the list of medicine products for a new label β each product only once.
Write a query that returns the distinct product_name values of the Medicine category, in alphabetical order.
-- Write your query below