DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 11

GROUP BY

Aggregating by category — the analytics workhorse.

IN THIS CHAPTER

  • How GROUP BY splits data into buckets before aggregating
  • Grouping by a single column
  • Grouping by multiple columns
  • Combining GROUP BY with WHERE

🌟 Think of it this way: GROUP BY is like sorting coins into labelled bags — all 1-rupee coins into one bag, all 5-rupee into another — and then counting each bag separately. Each bag becomes one row in your result.

11.1 - Employees Department

SQL

SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC;

Exercise 👇

Exercise:

Tasks

1.👉Find the number of employees in each department.
2.Find the total salary of each department.
3.Find the average salary of each department.
4.Find the minimum and maximum salary in each department.
Stuck? Read this task's