Advanced Filtering with SQL Functions

This lesson builds upon your existing SQL skills by introducing aggregate functions. You'll learn how to use functions like COUNT, SUM, AVG, MIN, and MAX to analyze data and extract valuable insights from your tables.

Learning Objectives

  • Define and understand the purpose of aggregate functions in SQL.
  • Use the COUNT function to determine the number of rows or values that meet specific criteria.
  • Utilize the SUM, AVG, MIN, and MAX functions to calculate sums, averages, minimums, and maximums within a dataset.
  • Combine aggregate functions with WHERE clauses to perform conditional calculations.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Aggregate Functions

Aggregate functions are powerful tools in SQL that allow you to perform calculations on a set of rows and return a single value. They 'aggregate' multiple rows into a single result. Think of them like summarizing data. Instead of seeing every individual customer record, you might want to know the total number of customers, the average order value, or the highest price of a product. We'll cover five key aggregate functions in this lesson: COUNT, SUM, AVG, MIN, and MAX.

The COUNT Function

The COUNT() function counts the number of rows that match a specified criterion. It's extremely useful for quickly determining the size of a dataset or the number of records that fit a specific condition.

Syntax: SELECT COUNT(column_name) FROM table_name; or SELECT COUNT(*) FROM table_name; (counts all rows)

Example: Suppose you have a table named customers and you want to know how many customers are in the table. You would use: SELECT COUNT(*) FROM customers; This will return a single number representing the total number of customers.

Let's assume you have a table called orders with columns like order_id, customer_id, and order_date. To count the number of orders, you'd use SELECT COUNT(*) FROM orders;. To count the number of unique customers who placed orders, you'd use SELECT COUNT(DISTINCT customer_id) FROM orders;. Using DISTINCT ensures each customer is only counted once, even if they have multiple orders.

The SUM Function

The SUM() function calculates the sum of values in a numeric column. It's perfect for totaling sales, expenses, or any other quantifiable data.

Syntax: SELECT SUM(column_name) FROM table_name;

Example: If you have a table named sales with a sales_amount column, you can calculate the total sales using: SELECT SUM(sales_amount) FROM sales; This will give you the total revenue generated from sales.

The AVG Function

The AVG() function calculates the average (mean) of values in a numeric column. It's often used to determine the average order value, average age of customers, or the average score on a test.

Syntax: SELECT AVG(column_name) FROM table_name;

Example: Using the sales table again, to find the average sale amount, you would use: SELECT AVG(sales_amount) FROM sales;.

The MIN and MAX Functions

The MIN() and MAX() functions are used to find the minimum and maximum values in a numeric column, respectively. They can be used to find the lowest price, the oldest customer, or the highest sales amount.

Syntax: SELECT MIN(column_name) FROM table_name; and SELECT MAX(column_name) FROM table_name;

Example: To find the lowest sale amount in the sales table, use: SELECT MIN(sales_amount) FROM sales; and to find the highest, use: SELECT MAX(sales_amount) FROM sales;.

Combining with WHERE Clause

You can combine aggregate functions with the WHERE clause to filter the data before performing the calculation. This allows you to calculate sums, averages, counts, minimums, and maximums based on specific criteria.

Example: Suppose you want to know the total sales for the month of January in your sales table. Assuming you have a sales_date column, you could use: SELECT SUM(sales_amount) FROM sales WHERE sales_date BETWEEN '2024-01-01' AND '2024-01-31';

Progress
0%