0% found this document useful (0 votes)
1 views3 pages

Worksheet4 Aggregation

The document provides an overview of SQL aggregation functions, including COUNT(), MAX(), MIN(), SUM(), and AVG(), along with their basic syntax and usage in SELECT statements. It explains how to group data using the GROUP BY clause and filter results with the HAVING clause, providing example queries for various scenarios. Additionally, it includes practice problems to reinforce the concepts discussed.

Uploaded by

chaimaalabyad647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Worksheet4 Aggregation

The document provides an overview of SQL aggregation functions, including COUNT(), MAX(), MIN(), SUM(), and AVG(), along with their basic syntax and usage in SELECT statements. It explains how to group data using the GROUP BY clause and filter results with the HAVING clause, providing example queries for various scenarios. Additionally, it includes practice problems to reinforce the concepts discussed.

Uploaded by

chaimaalabyad647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Inspire, Transform and Impact

School of Science & Engineering

CSC 3326 DATABASE SYSTEMS Summer 2025

SQL Aggregation

1) Functions

SQL provides some built-in functions for performing data aggregation and grouping. The main ones are:

COUNT() – the number of rows


MAX() – the largest value
MIN() – the smallest value
SUM() – the sum
AVG() – the average value

Aggregate functions can be used as expressions only in the following:

- The select list of a SELECT statement

- A HAVING clause.

Basic syntax:

SELECT aggregate_function([DISTINCT] expression), ... FROM tbl_name,... [WHERE


where_condition];

where expression is a column or an expression involving columns (like an arithmetic expression).


expression cannot be however an aggregate function.

• Try the following query to obtain the lowest price:

SELECT MIN(price) AS "Lowest price" FROM Product ;

1
• Try the following query to obtain various statistics on the product prices:

SELECT MIN(price) AS "Lowest price", AVG(price) AS Average, MAX(price) AS Highest, SUM(price)


AS "Total sum" FROM Product;

• We can better format all those prices using the CAST() conversion function:

SELECT CAST(MIN(price) AS decimal (6, 2)) AS "Lowest price ", CAST(AVG(price) AS decimal (6,
2)) AS Average, CAST(MAX(price) AS decimal (6, 2)) AS Highest, CAST(SUM(price) AS decimal (7,
2)) AS "Total sum " FROM Product;

• Try the following query to obtain how many products cost at least 10 Dhs:

SELECT COUNT(*) AS "Product count"


FROM Product
WHERE price >= 10.00;

• Try the following query to obtain the total value of all products in inventory.

SELECT SUM(price * quantityOnHand) AS "Total value" FROM Product;

Practice problems

• Write the SQL statement to count the number of products that have no discount
• Write the SQL query to obtain the number of vendors living in UK and supplying some products that
cost at least 10 DHs.

2) Grouping data (GROUP BY clause)

GROUP BY is used in conjunction with the aggregate functions to group a selected set of rows into a set
of summary rows by the values of one or more columns or expressions. One row is returned for each
group.

Basic syntax:

SELECT group_by_expression, aggregate_function([DISTINCT] expression), ... FROM


tbl_name,...
[WHERE where_condition]
GROUP BY {col_name | expr }... [ORDER BY {col_name | expr} [ASC | DESC],...];

We can use the following query to display, for every vendor, their products’ average price:

SELECT vendorCode, AVG(price) AS "Average price" FROM Product GROUP BY vendorCode;

2
Practice problems

• For each invoice, display the invoice number and the total amount of that invoice.
• For each customer, display the customer code and the total number of invoices that were issued for that
customer (also consider the customers that have no invoices).
• Display the vendor code, vendor name and the total product value for all the products that have no discount
supplied by each vendor.

3) Filtering the results based on the grouping results (HAVING clause)

The HAVING clause is used to restrict the aggregated results returned based on a post- aggregation
condition; it is used in conjunction with aggregate functions to select the result- set by some condition.

SELECT group_by_expression, aggregate_function([DISTINCT] expression),... FROM


tbl_name,...
[WHERE where_condition]
GROUP BY {col_name | expr } HAVING aggregate_condition
[ORDER BY {col_name | expr} [ASC | DESC],...];

where aggregate_condition in the HAVING clause is a logical expression constraining on some condition
on the aggregated results.

We can use the following query to display information about every vendor whose product price average is
more than 10 dirhams. The information displayed includes the vendor code, the number of products and
the average price of the products supplied by each vendor:

SELECT vendorCode, COUNT(*) AS "Product Count", AVG(price) AS "Avg price" FROM Product
GROUP BY vendorCode
HAVING AVG(price) > 10;

Practice Problems

Practice Problems

• Display the names of customers having generated more than 5 invoices.


• Display the code and description of products with a total quantity sold higher than 10.
• Display the invoice number, the invoice date and the total amount for every invoice with an invoice
number above 1005 and a total amount greater than 100 DHs.

You might also like