Lesson 14/100

Tutorials MySQL Tutorial

HAVING — Complete Guide

HAVING — 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 14 of 100

HAVING

BasicsAdvanced

Basics · 1 — SQL · ~6 min · MySQL — Queries & Clauses

What is this?

HAVING filters groups after GROUP BY, like WHERE but for aggregated results. Use it when the condition involves COUNT, SUM, or AVG.

Why should you care?

“Show cities with more than 100 active customers” needs HAVING — WHERE cannot see COUNT because grouping has not happened yet.

See it live — copy this example

Run in MySQL Workbench or the mysql CLI.

SELECT city, COUNT(*) AS cnt
FROM customers
GROUP BY city
HAVING cnt >= 2
ORDER BY cnt DESC;

What happened?

  • GROUP BY forms city buckets first.
  • HAVING drops buckets where cnt < 2.
  • Cities with one lonely customer disappear from the report.

Practice next

  1. Skew data: put 3 customers in Mumbai, 1 in Kochi.
  2. Run query; only Mumbai should show if threshold is 2.
  3. Change HAVING to cnt >= 3 and watch Mumbai vanish.
  4. HAVING AVG(...) style: add orders JOIN and HAVING SUM(total_inr) > 1000.
  5. Combine WHERE city IS NOT NULL AND HAVING cnt > 1.

Remember

WHERE filters rows; HAVING filters groups. HAVING can reference aggregate aliases in MySQL. Use for threshold reports on summaries.

Flipkart seller tier report

Only sellers with 50+ SKUs in stock appear in a premium listing — HAVING COUNT(sku) >= 50.

Outcome: List stays high quality without manual spreadsheets.

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