Lesson 17/100

Tutorials MySQL Tutorial

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

BasicsAdvanced

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

  1. Duplicate city values in customers.
  2. Compare SELECT city vs SELECT DISTINCT city row counts.
  3. Try DISTINCT city, full_name — duplicates return because pairs differ.
  4. SELECT COUNT(DISTINCT customer_id) FROM orders;
  5. 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.

Junior Detailed
Explain SQL queries in the context of MySQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MySQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MySQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MySQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MySQL Tutorial
Course syllabus

MySQL Tutorial

MySQL — Foundations
MySQL — Queries & Clauses
MySQL — Joins & Relationships
MySQL — Functions & Window Functions
MySQL — Transactions & Concurrency
MySQL — Stored Procedures & Triggers
MySQL — Indexing & Performance
MySQL — Advanced MySQL
MySQL — Security & Cloud MySQL
MySQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details