DISTINCT
SQL · Unit 3: Sorting and limiting
- Full plan
- SQL
- 10 exercises
What sections does the store have? If you ask for each product's category you get 8 rows, with Accessories three times, because SELECT shows the column as it is.
DISTINCT goes right after SELECT and removes the repeated rows. One is left for each different value.
SELECT DISTINCT category
FROM products
ORDER BY category;Returns
| category |
|---|
| Accessories |
| Audio |
| Devices |
| Displays |
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
Which sections have something under 100? Five products cost less than that. What does this query return?
2. Complete the code
Which days had sales? Back in unit 1, that list had repeated days. Fill in the blank so that each day shows up only once.
3. Put the lines in order
What ages are there among the customers under 40? Put in order the query that shows them without repeats, from lowest to highest. One line doesn't belong.
4. Predict the output
Which cities outside Denver have customers? Luke and Hannah live in Austin, and nobody knows Chloë's city. What does this query return?
5. Complete the code
Which customers have placed an order of more than one unit? Fill in the blank with the column that tells you.
6. Predict the output
Valerie is customer 5 and Dylan is customer 6. Valerie has placed one order of 1 unit and another of 3. Dylan has placed two orders of 1. What does this query return?
7. Find the bug
Someone wants a list with each of the store's sections only once. They get 8 rows, with Accessories repeated. Which line has the problem?
8. Multiple choice
The
orderstable has 12 rows, and this query returns 12 too. What does that tell you about the store?9. Predict the output
What were the last three days with sales? January 15 had two orders. What does this query return?
10. Write the code
Which products have ever been sold? Write a query that shows a single column, the
product_idfrom the orders, with no repeats and from lowest to highest.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.