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 Compiler

Result:

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 Compiler

Result:

statuspayment_method
CancelledCard
CancelledUPI
DeliveredCOD
DeliveredCard
DeliveredNetBanking
DeliveredUPI
PendingCOD
PendingUPI
ReturnedCOD
ReturnedCard
ShippedCard
ShippedUPI

(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 Compiler

Result:

total_ordersunique_customers
2411

(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 Compiler

Result:

productmarginmargin_pct
Redmi Note 13 Pro4499.0018.7
boAt Airdopes 141599.0046.1
Samsung 43" Smart TV5990.0018.7
Men's Cotton Kurta949.0050.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 Compiler

Result:

product_namebrand
Redmi Note 13 ProXiaomi
boAt Airdopes 141boAt
Samsung 43" Smart TVSamsung

(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 Compiler

Error: 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 Compiler

Result:

product_namemargin
Redmi Note 13 Pro4499.00
Samsung 43" Smart TV5990.00
Women's Silk Saree3299.00

(3 rows)

  • Using DISTINCT to hide a bad join. If a join creates duplicates, fix the join. DISTINCT just hides the symptom and can make totals wrong.
  • Putting DISTINCT in the wrong place. It goes right after SELECT, 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 Compiler

Result:

category_idbrand
1Samsung
1Xiaomi
1boAt
2Campus
2Fabindia
2Nalli
3Hawkins
3Milton
3Prestige
4Himalaya
4Kama Ayurveda
5Jaico

(showing 12 of 15 rows)

Key takeaways

  • DISTINCT removes duplicate rows across all selected columns.
  • COUNT(DISTINCT col) counts unique values.
  • AS renames columns and tables in the output only.
  • Column aliases work in ORDER BY but not in WHERE.

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.

Newsletter

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)