DISTINCT — Complete Guide
DISTINCT — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 17 of 100
DISTINCT
Basics → Advanced
Basics · 1 — SQL · ~6 min · MySQL — Queries & Clauses
What is this?
DISTINCT removes duplicate rows from the result set based on all selected columns. It is slower than GROUP BY for large data but reads clearly for “unique values”.
Why should you care?
A dropdown of “cities we ship to” should list each city once — DISTINCT on city from customers.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
SELECT DISTINCT city
FROM customers
WHERE city IS NOT NULL
ORDER BY city;
What happened?
- If forty customers share Mumbai, one Mumbai row appears.
- NULL cities are excluded by WHERE.
- Sorted list feeds a UI select box.
Practice next
- Duplicate city values in customers.
- Compare SELECT city vs SELECT DISTINCT city row counts.
- Try DISTINCT city, full_name — duplicates return because pairs differ.
- SELECT COUNT(DISTINCT customer_id) FROM orders;
- DISTINCT on expression: SELECT DISTINCT LEFT(city,1) AS first_letter FROM customers;
Remember
DISTINCT dedupes full selected row shape. COUNT(DISTINCT col) counts unique values. Fix JOIN duplication at source when possible.
DataFlow shipping zones
Logistics builds city list from DISTINCT customer cities for courier SLA rules.
Outcome: No duplicate Mumbai entries in config UI.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!