Lesson 13/100

Tutorials MySQL Tutorial

GROUP BY — Complete Guide

GROUP BY — 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 13 of 100

GROUP BY

BasicsAdvanced

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

What is this?

GROUP BY collapses rows that share the same column values into one summary row. You use it with aggregates like COUNT or SUM to answer “per city” or “per day” questions.

Why should you care?

Swiggy ops needs “orders per restaurant per hour”, not a flat list of ten million order rows.

See it live — copy this example

Run in MySQL Workbench or the mysql CLI.

SELECT city, COUNT(*) AS customer_count
FROM customers
GROUP BY city
ORDER BY customer_count DESC;

What happened?

  • Rows with the same city merge into one group.
  • COUNT(*) counts members per city.
  • ORDER BY sorts cities by popularity.
  • Non-grouped columns cannot appear unless aggregated.

Practice next

  1. Add customers in 4+ cities.
  2. Run GROUP BY city.
  3. Add HAVING customer_count > 1 in the next lesson style.
  4. GROUP BY DATE(created_at) for signups per day.
  5. Use COUNT(DISTINCT city) vs COUNT(*) and compare.

Remember

GROUP BY builds buckets of rows. Every selected non-aggregated column must be in GROUP BY. Pair with COUNT/SUM/AVG for metrics.

DataFlow signup map

Growth team charts new customers per city weekly from one GROUP BY query.

Outcome: Marketing spends where signups actually happen.

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