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 10

Aggregate Functions

Turning millions of rows into a single meaningful number.

IN THIS CHAPTER

  • COUNT — how many rows (with and without NULLs)
  • SUM, AVG — totals and averages
  • MIN, MAX — extremes
  • Combining multiple aggregates

🌟 Think of it this way: Aggregate functions are like a cashier totalling your bill. You bring 20 items (rows) to the counter; they give you back one number (the total). The individual items are collapsed into a single summary.

10.1 - Pagination with OFFSET

How many employees?

SELECT COUNT(*) AS total_employees FROM employees;

COUNT (*) vs COUNT(column)

SQL

SELECT COUNT(*) AS all_rows,
      COUNT(manager_id) AS has_manager
FROM employees;

10.2 - SUM, AVG, MIN, MAX

Full payroll summary

SELECT
    COUNT(*) AS headcount,
    SUM(salary) AS total_payroll,
    AVG(salary) AS avg_salary,
    MIN(salary) AS min_salary,
    MAX(salary) 
FROM employees;

Exercise 👇

Exercise:

Tasks

1.👉Find the total number of employees.
2.Find the number of employees in IT department.
3.Find the total salary of all employees.
4.Find the total salary of employees in HR department.
5.Find the average salary of all employees.
6.Find the minimum salary among all employees.
7.Find the maximum salary among all employees.
8.Find the average salary of employees in Sales department.
Stuck? Read this task's