SQL: Aggregate Functions and Grouping

This lesson introduces powerful SQL tools for summarizing and analyzing data: aggregate functions and the `GROUP BY` clause. You'll learn how to calculate sums, averages, counts, and other key statistics to extract meaningful insights from your datasets.

Learning Objectives

  • Define and explain the purpose of aggregate functions (COUNT, SUM, AVG, MIN, MAX).
  • Apply aggregate functions to calculate summary statistics from a single table.
  • Utilize the `GROUP BY` clause to group results based on specified columns.
  • Combine aggregate functions with `GROUP BY` to analyze data by categories or groups.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Aggregate Functions

Aggregate functions perform calculations on a set of rows and return a single value. They are essential for summarizing data. Here's a breakdown of the most common ones:

  • COUNT(): Counts the number of rows that match a specified criterion (or all rows if no criterion is given).
  • SUM(): Calculates the sum of a numeric column.
  • AVG(): Calculates the average of a numeric column.
  • MIN(): Finds the minimum value in a column.
  • MAX(): Finds the maximum value in a column.

Example Table: products

product_id product_name category price 1 Laptop Electronics 1200 2 Mouse Electronics 25 3 T-shirt Clothing 20 4 Jeans Clothing 50 5 Tablet Electronics 300

Examples using Aggregate Functions:

  • SELECT COUNT(*) FROM products; (Counts all rows - Result: 5)
  • SELECT SUM(price) FROM products; (Calculates the sum of all prices - Result: 1595)
  • SELECT AVG(price) FROM products; (Calculates the average price - Result: 319)
  • SELECT MIN(price) FROM products; (Finds the minimum price - Result: 20)
  • SELECT MAX(price) FROM products; (Finds the maximum price - Result: 1200)

The `GROUP BY` Clause

The GROUP BY clause divides the result set into groups based on the values in one or more columns. It's often used with aggregate functions to perform calculations on each group.

Syntax: SELECT column1, aggregate_function(column2) FROM table_name WHERE condition GROUP BY column1;

Example:

SELECT category, COUNT(*) FROM products GROUP BY category;

This query groups the products table by category and counts the number of products in each category. The output would be:

category count(*) Clothing 2 Electronics 3

Combining Aggregate Functions and `GROUP BY`

This is where the real power lies! You can combine aggregate functions and GROUP BY to perform detailed data analysis.

Example:

SELECT category, AVG(price) FROM products GROUP BY category;

This query groups the products table by category and calculates the average price of products in each category. The output would be:

category avg(price) Clothing 35.00 Electronics 508.33

Using `HAVING` Clause

The HAVING clause is used to filter grouped results. It's similar to the WHERE clause, but it filters groups instead of individual rows. The HAVING clause is used after the GROUP BY clause.

Syntax: SELECT column1, aggregate_function(column2) FROM table_name WHERE condition GROUP BY column1 HAVING condition;

Example:
SELECT category, AVG(price) FROM products GROUP BY category HAVING AVG(price) > 100;

This query groups the products table by category and calculates the average price of each category but only displays the categories where the average price is greater than 100. The output would be:

category avg(price) Electronics 508.33
Progress
0%