Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >