1. SQL basics: ask a table a question
1.4 DISTINCT and AS: remove repeats, rename columns
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
A class register has 40 names, but only 4 different house colours. If you ask "which houses are there?" you want 4 answers, not 40. DISTINCT gives each different value once.
AS gives a column a new name in the result. The table itself does not change. Use it to make results easy to read: amount AS price.
DISTINCT looks at the whole row you selected. SELECT DISTINCT product, status gives each different pair, not each different product.
Each product once
orders has eight rows.
SQL
SELECT DISTINCT productFROM orders;
Rows at this step
product -------- Laptop Mouse Keyboard Monitor
Quick check
A column has the values A, B, A, C, B. How many rows does SELECT DISTINCT return?
Rename and sort
AS gives the column a clearer name.
SQL
SELECT DISTINCT status AS order_statusFROM ordersORDER BY order_status;
Rows at this step
order_status ------------ cancelled delivered pending
Quick check
What does amount AS price do?
Practice in SQL ✓ Solved
List each different city from customers once. Name the column place and sort it A to Z.
Tables you can query: customers, orders
customers id | name | city ---+-------+------ 1 | Asha | Noida 2 | Ravi | Delhi 3 | Meera | Pune 4 | Kabir | Noida 5 | Zoya | NULL orders id | customer_id | product | amount | status | order_date ----+-------------+----------+--------+-----------+----------- 101 | 1 | Laptop | 55000 | delivered | 2026-01-05 102 | 2 | Mouse | 700 | delivered | 2026-01-06 103 | 1 | Keyboard | 1500 | delivered | 2026-01-09 104 | 3 | Monitor | 12000 | cancelled | 2026-01-12 105 | 2 | Laptop | 52000 | delivered | 2026-01-15 106 | 4 | Mouse | 650 | pending | 2026-01-18 107 | 1 | Monitor | 11500 | delivered | 2026-01-20 108 | 3 | Keyboard | NULL | pending | 2026-01-22
Expected output
place ----- NULL Delhi Noida Pune
Reference solution
SELECT DISTINCT city AS place
FROM customers
ORDER BY place;
Question 1 · Easy
What does DISTINCT do?
Question 2 · Medium
Which keyword gives a column a new name in the result?
Question 3 · Hard
SELECT DISTINCT product, status FROM orders returns…
Key takeaways
DISTINCT returns each different row once. AS renames a column in the result. DISTINCT works on all the selected columns together.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in