0% found this document useful (0 votes)
16 views2 pages

SQL Aggregate Functions Explained

The document presents SQL queries using aggregate functions on a Sales table. It includes examples for COUNT, SUM, AVG, MAX, and MIN functions, along with a GROUP BY query that summarizes total quantities sold for each product. The outputs for each query are also provided, showcasing the results of the calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

SQL Aggregate Functions Explained

The document presents SQL queries using aggregate functions on a Sales table. It includes examples for COUNT, SUM, AVG, MAX, and MIN functions, along with a GROUP BY query that summarizes total quantities sold for each product. The outputs for each query are also provided, showcasing the results of the calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL Queries using Aggregate Functions

Table: Sales
SaleID Product Quantity Price Region
1 Pen 10 5 North
2 Pencil 20 3 South
3 Notebook 15 20 North
4 Eraser 25 2 East
5 Pen 30 5 South
6 Notebook 12 20 West
7 Pencil 18 3 East

1. COUNT()
Query: SELECT COUNT(*) AS TotalSales FROM Sales;

Output:

TotalSales
7

2. SUM()
Query: SELECT SUM(Quantity) AS TotalQuantity FROM Sales;

Output:

TotalQuantity
130

3. AVG()
Query: SELECT AVG(Price) AS AveragePrice FROM Sales;

Output:

AveragePrice
8.29

4. MAX()
Query: SELECT MAX(Quantity) AS MaxQuantity FROM Sales;

Output:
MaxQuantity
30

5. MIN()
Query: SELECT MIN(Price) AS MinPrice FROM Sales;

Output:

MinPrice
2

6. GROUP BY with SUM()


Query: SELECT Product, SUM(Quantity) AS TotalSold FROM Sales GROUP BY Product;

Output:

Product TotalSold
Pen 40
Pencil 38
Notebook 27
Eraser 25

You might also like