0% found this document useful (0 votes)
14 views10 pages

4 IM Aggregation and Grouping

Uploaded by

davethegreat343
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)
14 views10 pages

4 IM Aggregation and Grouping

Uploaded by

davethegreat343
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
You are on page 1/ 10

LAB 4: AGGREGATION AND GROUPING

Objectives:
 Learn how to perform aggregation function on data
 Understand GROUP BY and HAVING clauses
Lab Activities:
1. Writing queries to calculate SUM, AVG, COUNT, etc.
2. Using GROUP BY to group data based on the specific columns.
3. Applying HAVING clause to filter grouped data.

LAB 4.1 - WRITING QUERIES TO CALCULATE SUM, AVG, COUNT, ETC.


To write queries to calculate SUM, AVG, COUNT, and other aggregate functions using SQL Server
Management Studio (SSMS), you'll need to use the SQL language. Here are the basic steps to write
these types of queries:

1. Open SQL Server Management Studio (SSMS): Launch SSMS and connect to your SQL Server
instance.
2. Select a Database: In the Object Explorer, expand the server node, then expand the Databases
node, and choose the database you want to work with by right-clicking and selecting "New
Query" or by selecting it from the "Available Databases" dropdown in the query window.
3. Write the SQL Query: In the query window, you can start writing your SQL query. Here are
some examples for common aggregate functions:

a. SUM:
To calculate the sum of a column, you can use the SUM() function:

SELECT SUM(column_name) AS total_sum


FROM table_name;

Replace column_name with the name of the column you want to sum, and table_name with the
name of the table.

b. AVG:
To calculate the average of a column, you can use the AVG() function:

SELECT AVG(column_name) AS average_value


FROM table_name;

c. COUNT:
To count the number of rows in a table, you can use the COUNT() function:
SELECT COUNT(*) AS row_count
FROM table_name;
To count the number of non-null values in a specific column:

SELECT COUNT(column_name) AS non_null_count


FROM table_name;

d. Other Aggregate Functions:


SQL Server also supports other aggregate functions like MIN(), MAX(), etc. You can use them
similarly to SUM, AVG, and COUNT.

4. Execute the Query: After writing your query, you can execute it by clicking the "Execute" button
(or press F5). The results will be displayed in the "Results" pane.

5. View the Results: The result set will display the calculated value(s) based on your query.

6. (Optional) Save the Query: You can save the query for future use by clicking "File" -> "Save" or
using the shortcut Ctrl + S.

Remember to replace column_name and table_name with your actual column and table names.
SQL Server Management Studio provides an intuitive interface for writing and executing SQL
queries, making it easy to work with aggregate functions and retrieve the desired results.

Use Northwind, w3schools.com mfhasan.com/sql

Exercise 4.1a: Calculate the Total Sales Amount


Calculate the total sales amount from the "Sales.SalesOrderHeader" table.

SELECT SUM(TotalDue) AS TotalSalesAmount


FROM Sales.SalesOrderHeader;

Sample Output:

Exercise 4.1b: Calculate the Average Product Price


Calculate the average product price from the "Production.Product" table.

SELECT AVG(ListPrice) AS AverageProductPrice


FROM Production.Product;

Sample Output:

Exercise 4.1c: Count the Number of Customers


Count the number of customers in the "Sales.Customer" table.

SELECT COUNT(*) AS NumberOfCustomers


FROM Sales.Customer;

Sample Output:

Exercise 4.1d: Count the Number of Orders for a Specific Customer


Count the number of orders for a specific customer (replace <CustomerID> with the actual customer
ID).

SELECT COUNT(*) AS NumberOfOrders


FROM Sales.SalesOrderHeader
WHERE CustomerID = <CustomerID>;

Sample Output:

Exercise 4.1e: Find the Minimum and Maximum Order Total


Find the minimum and maximum order total from the "Sales.SalesOrderHeader" table.

SELECT MIN(TotalDue) AS MinimumOrderTotal,


MAX(TotalDue) AS MaximumOrderTotal
FROM Sales.SalesOrderHeader;

Sample Output:

Exercise 4.1f: Calculate the Average Product Rating


Calculate the average product rating from the "Production.ProductReview" table.

SELECT AVG(Rating) AS AverageProductRating


FROM Production.ProductReview;

Sample Output:

Exercise 4.1g: Calculate the Total Quantity Sold for a Specific Product
Calculate the total quantity sold for a specific product (replace <ProductID> with the actual product ID).

SELECT SUM(OrderQty) AS TotalQuantitySold


FROM Sales.SalesOrderDetail
WHERE ProductID = <ProductID>;

Sample Output:

Exercise 4.1h: Calculate the Average Age of Employees


Calculate the average age of employees in the "HumanResources.Employee" table.

SELECT AVG(DATEDIFF(YEAR, BirthDate, GETDATE())) AS AverageEmployeeAge


FROM HumanResources.Employee;

Sample Output:

Answer what is available database in the internet.

Problem 1: Calculate Total Revenue


Calculate the total revenue generated by all orders in the "Sales.SalesOrderHeader" table.

Problem 2: Calculate Average Salary


Calculate the average salary of employees in the "HumanResources.Employee" table.

Problem 3: Count Orders by Status


Count the number of orders in the "Sales.SalesOrderHeader" table for each distinct order status (e.g.,
"Shipped," "Cancelled").

Problem 4: Find Minimum and Maximum Product Prices


Find the minimum and maximum product prices from the "Production.Product" table.

Problem 5: Calculate Average Product Rating


Calculate the average product rating from the "Production.ProductReview" table.

Problem 6: Count Orders for Each Customer


Count the number of orders placed by each customer in the "Sales.Customer" table.

Problem 7: Calculate Total Quantity Sold


Calculate the total quantity sold for each product in the "Sales.SalesOrderDetail" table.

Problem 8: Calculate Average Age of Employees


Calculate the average age of employees in the "HumanResources.Employee" table.
Problem 9: Count Orders by Month and Year
Count the number of orders placed in each month and year in the "Sales.SalesOrderHeader" table.

Problem 10: Calculate Total Sales by Product Category


Calculate the total sales amount for each product category in the "Sales.SalesOrderDetail" table,
joining with the "Production.Product" table to get the product category.

Problem 11: Calculate Total Sales by Region


Calculate the total sales amount for each sales region in the "Sales.SalesOrderHeader" table, joining
with the "Sales.SalesTerritory" table to get the region information.

Problem 12: Calculate Total Sales by Employee


Calculate the total sales amount generated by each salesperson (employee) in the
"Sales.SalesOrderHeader" table, joining with the "HumanResources.Employee" table to get the
employee information.

4.2 - USING GROUP BY TO GROUP DATA BASED ON THE SPECIFIC COLUMNS

Using the GROUP BY clause in SQL Server Management Studio (SSMS) allows you to group data
based on specific columns and perform aggregate functions on those groups. Here are the steps to use
GROUP BY:

1. Open SSMS and Connect:


 Launch SQL Server Management Studio and connect to your SQL Server instance.
2. Select a Database:
 In the Object Explorer, expand the server node, then expand the Databases node.
 Choose the database you want to work with, right-click on it, and select "New Query" or
simply double-click it to open a new query window.
3. Write the SQL Query:
 In the query window, you can write a SQL query using the GROUP BY clause. The basic
syntax is as follows:
SELECT column1, column2, ..., aggregate_function(column)
FROM table
GROUP BY column1, column2, ...;
 Replace column1, column2, etc. with the columns you want to group by.
 Replace aggregate_function(column) with the aggregate function (e.g., SUM(column),
AVG(column), COUNT(column)) that you want to apply to the grouped data.
 Replace table with the name of the table you are querying.
4. Execute the Query:
 After writing your query, you can execute it by clicking the "Execute" button (or press F5).
The results will be displayed in the "Results" pane.
5. View the Results:
 The result set will display the aggregated data grouped by the specified columns.

Here's a practical example to illustrate how to use GROUP BY:

Let's say you have a table named "Orders" with columns "CustomerID," "ProductID," and "Quantity."
You want to find the total quantity of products purchased by each customer. You can write the following
SQL query:
SELECT CustomerID, SUM(Quantity) AS TotalQuantityPurchased
FROM Orders
GROUP BY CustomerID;

In this query:

 We are grouping the data by the "CustomerID" column.


 We are using the SUM function to calculate the total quantity purchased for each
customer.
 The result will display the "CustomerID" and the corresponding total quantity purchased
for each customer.

Using the GROUP BY clause is a powerful way to perform aggregations on specific columns and
summarize data in SQL Server.

Use northwind, w3schools.com mfhasan.com/sql

Exercise 1: Group Sales by Product


Group sales data from the "Sales.SalesOrderDetail" table by product. Calculate the total quantity sold
and total sales amount for each product.

SELECT ProductID,
SUM(OrderQty) AS TotalQuantitySold,
SUM(LineTotal) AS TotalSalesAmount
FROM Sales.SalesOrderDetail
GROUP BY ProductID;

Exercise 2: Group Orders by Customer


Group order data from the "Sales.SalesOrderHeader" table by customer. Count the number of orders
placed by each customer.

SELECT CustomerID,
COUNT(*) AS NumberOfOrders
FROM Sales.SalesOrderHeader
GROUP BY CustomerID;

Exercise 3: Group Employees by Department


Group employee data from the "HumanResources.Employee" table by department. Calculate the
average salary for employees in each department.

SELECT DepartmentID,
AVG(Salary) AS AverageSalary
FROM HumanResources.Employee
GROUP BY DepartmentID;

Exercise 4: Group Products by Category


Group product data from the "Production.Product" table by category. Calculate the average list price for
products in each category.

SELECT CategoryID,
AVG(ListPrice) AS AverageListPrice
FROM Production.Product
GROUP BY CategoryID;

Exercise 5: Group Orders by Year and Month


Group order data from the "Sales.SalesOrderHeader" table by year and month. Calculate the total
number of orders placed in each month and year.

SELECT YEAR(OrderDate) AS OrderYear,


MONTH(OrderDate) AS OrderMonth,
COUNT(*) AS NumberOfOrders
FROM Sales.SalesOrderHeader
GROUP BY YEAR(OrderDate), MONTH(OrderDate)
ORDER BY OrderYear, OrderMonth;

Exercise 6: Group Sales by Region


Group sales data from the "Sales.SalesOrderHeader" table by sales region. Calculate the total sales
amount for each sales region.

SELECT TerritoryID,
SUM(TotalDue) AS TotalSalesAmount
FROM Sales.SalesOrderHeader
GROUP BY TerritoryID;

Exercise 7: Group Orders by Status


Group order data from the "Sales.SalesOrderHeader" table by order status. Count the number of orders
for each order status.

SELECT Status,
COUNT(*) AS NumberOfOrders
FROM Sales.SalesOrderHeader
GROUP BY Status;

Answer what is available database in the internet.

Problem 1: Group Sales by Product Category


Group sales data from the "Sales.SalesOrderDetail" table by product category. Calculate the total
quantity sold and total sales amount for each product category.

Problem 2: Group Orders by Customer and Year


Group order data from the "Sales.SalesOrderHeader" table by customer and year. Calculate the total
number of orders placed by each customer for each year.

Problem 3: Group Employees by Department and Calculate Total Salaries


Group employee data from the "HumanResources.Employee" table by department. Calculate the total
salaries for employees in each department.

Problem 4: Group Products by Supplier


Group product data from the "Production.Product" table by supplier. Calculate the average list price for
products from each supplier.

Problem 5: Group Orders by Month and Calculate Total Sales Amount


Group order data from the "Sales.SalesOrderHeader" table by month. Calculate the total sales amount
for each month.

Problem 6: Group Sales by Sales Region and Calculate Average Discount


Group sales data from the "Sales.SalesOrderHeader" table by sales region. Calculate the average
discount percentage for each sales region.

Problem 7: Group Orders by Payment Method and Calculate Total Orders


Group order data from the "Sales.SalesOrderHeader" table by payment method. Calculate the total
number of orders placed using each payment method.

Problem 8: Group Products by Category and Calculate Maximum Price


Group product data from the "Production.Product" table by product category. Find the product with the
highest list price in each category.

Problem 9: Group Customers by Country and Calculate Average Order Amount


Group customer data from the "Sales.Customer" table by country. Calculate the average order amount
for customers in each country.

Problem 10: Group Sales by Year and Calculate Total Sales Amount
Group sales data from the "Sales.SalesOrderHeader" table by year. Calculate the total sales amount
for each year.

Problem 11: Group Products by Subcategory and Calculate Total Inventory


Group product data from the "Production.Product" table by subcategory. Calculate the total inventory
(the sum of all products' stock quantities) for each subcategory.

Problem 12: Group Orders by Ship Date and Calculate Total Orders
Group order data from the "Sales.SalesOrderHeader" table by ship date. Calculate the total number of
orders shipped on each date.

4.3 - APPLYING HAVING CLAUSE TO FILTER GROUPED DATA

Using the HAVING clause in SQL Server Management Studio (SSMS) allows you to filter the results of
a GROUP BY query based on aggregate conditions. It is typically used to filter groups of rows produced
by the GROUP BY clause. Here are the steps to apply the HAVING clause:

1. Open SSMS and Connect:


 Launch SQL Server Management Studio and connect to your SQL Server instance.
2. Select a Database:
 In the Object Explorer, expand the server node, then expand the Databases node.
 Choose the database you want to work with, right-click on it, and select "New Query" or
simply double-click it to open a new query window.
3. Write the SQL Query:
 In the query window, write a SQL query using both the GROUP BY and HAVING clauses.
The basic syntax is as follows:

SELECT column1, column2, ..., aggregate_function(column)


FROM table
GROUP BY column1, column2, ...
HAVING aggregate_function(column) condition;r

 Replace column1, column2, etc. with the columns you want to group by.
 Replace aggregate_function(column) with the aggregate function (e.g., SUM(column),
AVG(column), COUNT(column)) you want to apply to the grouped data.
 Replace table with the name of the table you are querying.
 Replace condition with the condition you want to apply to the aggregated results.

4. Execute the Query:


 After writing your query, you can execute it by clicking the "Execute" button (or press F5).
The results will be displayed in the "Results" pane.
5. View the Results:
 The result set will display the rows that meet the condition specified in the HAVING clause.

Here's an example to illustrate how to use the HAVING clause:

Let's say you have a table named "Orders" with columns "CustomerID" and "TotalAmount." You want to
find customers who have placed orders with a total amount greater than $1,000. You can write the
following SQL query:

SELECT CustomerID, SUM(TotalAmount) AS TotalOrderAmount


FROM Orders
GROUP BY CustomerID
HAVING SUM(TotalAmount) > 1000;

In this query:

 We are grouping the data by the "CustomerID" column.


 We are using the SUM function to calculate the total order amount for each customer.
 We are using the HAVING clause to filter the results and only include customers whose total
order amount is greater than $1,000.

The result will display the "CustomerID" and the corresponding total order amount for customers who
meet the condition specified in the HAVING clause.

Using the HAVING clause is essential when you need to filter grouped data based on aggregate
conditions, allowing you to extract meaningful insights from your data in SQL Server.

Use AdventureWorks , Northwind, w3schools.com mfhasan.com/sql

Exercise 1: Find High-Spending Customers


Find customers who have made total purchases exceeding $2,000. Group orders by the "CustomerID"
and calculate the total purchase amount for each customer. Use the HAVING clause to filter customers
who meet the criteria.

Exercise 2: Identify Products with High Demand


Identify products that have been ordered more than 50 times. Group order data by "ProductID" and
calculate the total quantity ordered for each product. Use the HAVING clause to filter products with high
demand.
Exercise 3: Find Departments with Top Sales
Find departments in a retail store that have total sales exceeding $10,000. Group sales data by
department and calculate the total sales amount for each department. Use the HAVING clause to filter
departments with top sales.

Exercise 4: Detect Frequent Shippers


Identify shipping companies that have handled more than 100 shipments. Group shipment data by the
"ShipperID" and count the number of shipments for each shipper. Use the HAVING clause to filter
frequent shippers.

Exercise 5: Find Active Customers


Identify customers who have placed orders on more than 5 distinct dates. Group orders by
"CustomerID" and count the number of distinct order dates for each customer. Use the HAVING clause
to filter active customers.

Exercise 6: Detect High-Rated Products


Find products with an average rating greater than 4.5. Group product reviews by "ProductID" and
calculate the average product rating. Use the HAVING clause to filter high-rated products.

Exercise 7: Identify Top-Selling Categories


Identify product categories with total sales exceeding $50,000. Group sales data by product category
and calculate the total sales amount for each category. Use the HAVING clause to filter top-selling
categories.

Exercise 8: Find Busy Employees


Identify employees who have processed more than 50 orders. Group order data by the "EmployeeID"
and count the number of orders processed by each employee. Use the HAVING clause to filter busy
employees.

Exercise 9: Detect High-Value Orders


Identify orders with a total amount exceeding $500. Group orders by "OrderID" and calculate the total
order amount for each order. Use the HAVING clause to filter high-value orders.

Exercise 10: Find Active Users


Identify users with more than 20 login attempts. Group login data by user and count the number of login
attempts for each user. Use the HAVING clause to filter active users.

These exercises will help you practice using the HAVING clause to filter grouped data in SQL Server.

You might also like