SQL Lesson 10 - Queries with aggregates (Pt. 1)

In addition to the simple expressions that we introduced last lesson, SQL also supports the use of aggregate expressions (or functions) that allow you to summarize information about a group of rows of data. With the Pixar database that you've been using, aggregate functions can be used to answer questions like, "How many movies has Pixar produced?", or "What is the highest grossing Pixar film each year?".

Select query with aggregate functions over all rows

**SELECT AGG_FUNC(_column_or_expression_) AS aggregate_description**, … FROM mytable WHERE _constraint_expression_;

Without a specified grouping, each aggregate function is going to run on the whole set of result rows and return a single value. And like normal expressions, giving your aggregate functions an alias ensures that the results will be easier to read and process.

Common aggregate functions

Here are some common aggregate functions that we are going to use in our examples:

Function Description
COUNT(*), COUNT(column) A common function used to counts the number of rows in the group if no column name is specified. Otherwise, count the number of rows in the group with non-NULL values in the specified column.
MIN(column) Finds the smallest numerical value in the specified column for all rows in the group.
MAX(column) Finds the largest numerical value in the specified column for all rows in the group.
**AVG(**column) Finds the average numerical value in the specified column for all rows in the group.
SUM(column) Finds the sum of all numerical values in the specified column for the rows in the group.
Docs: MySQL, Postgres, SQLite, Microsoft SQL Server

Grouped aggregate functions

In addition to aggregating across all the rows, you can instead apply the aggregate functions to individual groups of data within that group (ie. box office sales for Comedies vs Action movies). This would then create as many results as there are unique groups defined as by the GROUP BY clause.

Select query with aggregate functions over groups

SELECT AGG_FUNC(_column_or_expression_) AS aggregate_description, … FROM mytable WHERE _constraint_expression_ **GROUP BY column**;

The GROUP BY clause works by grouping rows that have the same value in the column specified.

Exercise

For this exercise, we are going to work with our Employees table. Notice how the rows in this table have shared data, which will give us an opportunity to use aggregate functions to summarize some high-level metrics about the teams. Go ahead and give it a shot.

1. Find the longest time that an employee has been at the studio
	1. SELECT SUM(years_employed) FROM employees GROUP BY name ORDER BY 1 DESC LIMIT 1;
	2. SELECT MAX(total_years) FROM (SELECT SUM(years_employed) AS total_years FROM employees GROUP BY name) AS sub;
2. For each role, find the average number of years employed by employees in that role
	1. SELECT Role, AVG(Years_employed) FROM employees GROUP BY Role;
3. Find the total number of employee years worked in each building
	1. SELECT Building, Sum(Years_employed) FROM employees GROUP BY Building;

Exercise 10 — Tasks

1. Find the longest time that an employee has been at the studio ✓
2. For each role, find the average number of years employed by employees in that role ✓
3. Find the total number of employee years worked in each building ✓

This page is SQLBolt Lesson 10: "Queries with aggregates (Pt. 1)." It explains how to use SQL aggregate functions to summarize data in a table. The lesson covers:

The exercises ask you to:

The content is interactive and designed to help learners practice SQL aggregate queries directly on example data.[1]

1


Here are alternative ways to write the SQL query:

Original: ✅

SELECT SUM(years_employed) FROM employees GROUP BY name ORDER BY 1 DESC LIMIT 1;

Other equivalent versions:

All of these achieve the goal of finding the longest time that an employee has been at the studio (i.e., the maximum total years employed by any single person). The subquery and window function versions are especially useful in more complex queries or different SQL dialects.

1


Here are a few alternative ways to write the following SQL query:

Original:

SELECT Role, AVG(Years_employed) FROM employees GROUP BY Role;

Other valid versions:

All of these alternatives give you the average years employed by employees in each role. The choice depends on your SQL dialect and whether you want formatting or extra detail.

1


Here are some alternative ways to write your SQL query:

Original:

SELECT Building, SUM(Years_employed) FROM employees GROUP BY Building;

Other valid versions:

All of these versions calculate the total number of employee years worked in each building. The alias and ordering can help with readability and sorting.

1