Aggregation
COUNT, SUM, AVG, MIN, and MAX
Summarize a complete set of product prices with aggregate functions.
Lesson 26 of 84
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Intermediate
On this page4 sections
This lesson uses the unmodified DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Summarize a complete set of product prices with aggregate functions.
Run the example
SELECT COUNT(*) AS ProductCount,
SUM(ListPrice) AS PriceSum,
AVG(ListPrice) AS AveragePrice,
MIN(ListPrice) AS LowestPrice,
MAX(ListPrice) AS HighestPrice
FROM dbo.Products;Read it step by step
- FROM supplies all 15 products in the unmodified v1.0.0 dataset.
- COUNT(*) counts rows; the other functions summarize non-NULL ListPrice values.
- With no GROUP BY, the query produces one summary row.
Check the result
| ProductCount | PriceSum | AveragePrice | LowestPrice | HighestPrice |
|---|---|---|---|---|
| 15 | 1115.50 | 74.366666 | 8.50 | 189.00 |
ListPrice is NOT NULL, so all 15 prices contribute. AVG on this decimal column returns 74.366666 at six fractional digits. That value is truncated; rounding the exact average to six places would give 74.366667.
Practice
Count active products and find their lowest and highest ListPrice.
Check your answer and expected result
Reveal answer
SELECT COUNT(*) AS ActiveProducts, MIN(ListPrice) AS LowestPrice,
MAX(ListPrice) AS HighestPrice
FROM dbo.Products
WHERE IsActive = 1;| ActiveProducts | LowestPrice | HighestPrice |
|---|---|---|
| 14 | 8.50 | 189.00 |
The row shows the count and price range among active products only.
Your progress
Reveal the check answer or reach the end to complete this lesson.