All 40 lessons
Lesson 4 of 40 · SQL Basics
SQL DISTINCT and Aliases
By Skillancy Editorial Team
Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.
DISTINCT removes duplicate rows from a result, and AS gives a column or table a temporary name. Both are small, but you'll use them in almost every report.
Syntax
SELECT DISTINCT column1, column2
FROM table_name;
SELECT column1 AS new_name
FROM table_name AS t;DISTINCT examples
1. Unique values in one column
Which cities do ShopKart customers come from?
SELECT DISTINCT city
FROM customers
ORDER BY city;Try it in the SQL CompilerResult:
| city |
|---|
| Ahmedabad |
| Bengaluru |
| Chennai |
| Delhi |
| Hyderabad |
| Jaipur |
| Kochi |
| Kolkata |
| Lucknow |
| Mumbai |
| Pune |
(11 rows)
There are 12 customers but only 11 cities, because two customers live in Bengaluru.
2. Unique combinations of columns
With two columns, DISTINCT keeps each unique pair:
SELECT DISTINCT status, payment_method
FROM orders
ORDER BY status, payment_method;Try it in the SQL CompilerResult:
| status | payment_method |
|---|---|
| Cancelled | Card |
| Cancelled | UPI |
| Delivered | COD |
| Delivered | Card |
| Delivered | NetBanking |
| Delivered | UPI |
| Pending | COD |
| Pending | UPI |
| Returned | COD |
| Returned | Card |
| Shipped | Card |
| Shipped | UPI |
(12 rows)
3. Count distinct values
COUNT(DISTINCT ...) counts unique values. How many different customers have placed an order?
SELECT COUNT(*) AS total_orders,
COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;Try it in the SQL CompilerResult:
| total_orders | unique_customers |
|---|---|
| 24 | 11 |
(1 row)
24 orders came from 11 customers. This "orders vs. unique customers" pattern shows up constantly in analyst work.
Alias examples
4. Column aliases
Aliases make calculated columns readable:
SELECT product_name AS product,
price - cost_price AS margin,
ROUND((price - cost_price) / price * 100, 1) AS margin_pct
FROM products
LIMIT 4;Try it in the SQL CompilerResult:
| product | margin | margin_pct |
|---|---|---|
| Redmi Note 13 Pro | 4499.00 | 18.7 |
| boAt Airdopes 141 | 599.00 | 46.1 |
| Samsung 43" Smart TV | 5990.00 | 18.7 |
| Men's Cotton Kurta | 949.00 | 50.0 |
(4 rows)
If an alias contains spaces or capitals you want to keep, wrap it in double quotes: AS "Margin %".
5. Table aliases
Table aliases are short names for tables. They save typing and become essential once you join tables:
SELECT p.product_name, p.brand
FROM products AS p
LIMIT 3;Try it in the SQL CompilerResult:
| product_name | brand |
|---|---|
| Redmi Note 13 Pro | Xiaomi |
| boAt Airdopes 141 | boAt |
| Samsung 43" Smart TV | Samsung |
(3 rows)
Common mistakes
Using a column alias in WHERE. The alias doesn't exist yet when WHERE runs:
SELECT product_name, price - cost_price AS margin
FROM products
WHERE margin > 3000;Try it in the SQL CompilerError: column "margin" does not exist
Repeat the expression instead:
SELECT product_name, price - cost_price AS margin
FROM products
WHERE price - cost_price > 3000;Try it in the SQL CompilerResult:
| product_name | margin |
|---|---|
| Redmi Note 13 Pro | 4499.00 |
| Samsung 43" Smart TV | 5990.00 |
| Women's Silk Saree | 3299.00 |
(3 rows)
- Using
DISTINCTto hide a bad join. If a join creates duplicates, fix the join.DISTINCTjust hides the symptom and can make totals wrong. - Putting
DISTINCTin the wrong place. It goes right afterSELECT, not before a single column in the middle of the list.
Practice question
List every unique brand and category_id combination in products, sorted by category_id.
Show solution
SELECT DISTINCT category_id, brand
FROM products
ORDER BY category_id, brand;Try it in the SQL CompilerResult:
| category_id | brand |
|---|---|
| 1 | Samsung |
| 1 | Xiaomi |
| 1 | boAt |
| 2 | Campus |
| 2 | Fabindia |
| 2 | Nalli |
| 3 | Hawkins |
| 3 | Milton |
| 3 | Prestige |
| 4 | Himalaya |
| 4 | Kama Ayurveda |
| 5 | Jaico |
(showing 12 of 15 rows)
Key takeaways
DISTINCTremoves duplicate rows across all selected columns.COUNT(DISTINCT col)counts unique values.ASrenames columns and tables in the output only.- Column aliases work in
ORDER BYbut not inWHERE.
Frequently asked questions
Does DISTINCT apply to one column or all columns?
DISTINCT applies to the whole row of selected columns. SELECT DISTINCT city, state returns each unique city-and-state pair, not unique cities only.
Is the AS keyword required for aliases?
No. SELECT price p works the same as SELECT price AS p. Using AS for column aliases makes queries easier to read, so it is a good habit.
Can I use a column alias in WHERE?
No. WHERE is processed before SELECT, so the alias does not exist yet. Repeat the expression in WHERE, or wrap the query in a subquery or CTE. ORDER BY can use aliases.
Keep learning SQL
One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.
Prefer a one-page summary? Download the free SQL cheat sheet (PDF)