114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
60% Percent Complete
By Kevin McAleer, 5 Minutes
In this lesson, we’ll dive into grouping and aggregating data in SQL. These techniques allow us to summarize and analyze data across multiple rows, making it easier to extract valuable insights.
Aggregate functions are SQL functions that operate on multiple rows of data to return a single result. Common aggregate functions include:
Suppose we have a sales table with the following data:
sales
| sale_id | customer_id | amount | |---------|-------------|--------| | 1 | 1 | 50 | | 2 | 2 | 30 | | 3 | 1 | 20 | | 4 | 3 | 40 |
Example: Find the total amount of all sales.
SELECT SUM(amount) AS total_sales FROM sales;
Example: Find the average sale amount.
SELECT AVG(amount) AS average_sale FROM sales;
Example: Count the number of sales.
SELECT COUNT(sale_id) AS sale_count FROM sales;
GROUP BY
The GROUP BY clause groups rows that have the same values in specified columns, allowing us to perform aggregate functions on each group.
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
Example: Find the total amount spent by each customer.
SELECT customer_id, SUM(amount) AS total_spent FROM sales GROUP BY customer_id;
Result:
| customer_id | total_spent | |-------------|-------------| | 1 | 70 | | 2 | 30 | | 3 | 40 |
Each customer’s total spending is calculated separately.
HAVING
The HAVING clause allows you to filter groups based on aggregate values. Unlike WHERE, which filters rows, HAVING filters groups created by GROUP BY.
WHERE
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1 HAVING condition;
Example: Find customers who spent more than $50.
SELECT customer_id, SUM(amount) AS total_spent FROM sales GROUP BY customer_id HAVING total_spent > 50;
| customer_id | total_spent | |-------------|-------------| | 1 | 70 |
Only customers with a total spending of over $50 are included in the result.
You can group data by multiple columns to create more specific groupings.
Suppose we add a product column to the sales table:
product
| sale_id | customer_id | product | amount | |---------|-------------|---------|--------| | 1 | 1 | Laptop | 50 | | 2 | 2 | Phone | 30 | | 3 | 1 | Phone | 20 | | 4 | 3 | Laptop | 40 |
Example: Find the total amount spent by each customer on each product.
SELECT customer_id, product, SUM(amount) AS total_spent FROM sales GROUP BY customer_id, product;
| customer_id | product | total_spent | |-------------|---------|-------------| | 1 | Laptop | 50 | | 1 | Phone | 20 | | 2 | Phone | 30 | | 3 | Laptop | 40 |
This query calculates the total amount spent on each product by each customer.
Here’s a quick overview of some of the most useful aggregate functions:
COUNT
COUNT(sale_id)
SUM
SUM(amount)
AVG
AVG(amount)
MIN
MIN(amount)
MAX
MAX(amount)
Total Sales: Find the total sales amount.
Total Sales by Customer: Find the total amount each customer has spent.
Filter Groups with HAVING: Retrieve only customers who have spent more than $50.
Count of Sales per Product: Count the number of sales transactions for each product.
SELECT product, COUNT(sale_id) AS sale_count FROM sales GROUP BY product;
Here’s a summary of the key concepts and commands covered in this lesson:
GROUP BY customer_id
HAVING SUM(amount) > 50
Using grouping and aggregation, you can generate meaningful summaries and insights from your data. In the next lesson, we’ll explore database normalization to help you organize your data effectively.
< Previous Next >