Week 5
Week 5
Lab Manual 5
Learning Objectives
• GROUP BY on a single table
• GROUP BY on two or more tables
• GROUP BY with JOINS
LO1: Understanding GROUP BY clause on a Single
Table
The GROUP BY statement in SQL is used to arrange identical data into
groups based on specified columns. If a particular column has the same
values in multiple rows, the GROUP BY clause will group these rows
together.
Key Points About GROUP BY:
Syntax:
SELECT column1, function_name(column2)
FROM table_name
GROUP BY column1, column2;
suppliers
SupplierID SupplierName Country City
1 Exotic Liquid UK Londona
Database Systems-Manual 5 Page | 5
New Orleans New
2 USA
Cajun Delights Orleans
Grandma
3 Kelly's USA Ann Arbor
Homestead
4 Tokyo Traders Japan Tokyo
Cooperativa de
5 Quesos 'Las Spain Oviedo
Cabras'
UNION ALL
Conclusion:
• GROUP BY is used to organize and summarize data in SQL.
• HAVING filters groups, while WHERE filters rows before
grouping.
• JOIN and GROUP BY together help in analyzing related data.
Tasks:
1. Find the number of customers in each country.
(Country, Total_Customers)
2. Retrieve the total number of products in each category.
(CategoryID, CategoryName, Total_Products)
3. List the total number of orders handled by each employee.
(EmployeeID, LastName, Total_Orders)
4. Find the total quantity ordered for each product.
(ProductID, ProductName, Total_Quantity)
5. Retrieve the total number of orders shipped by each shipper.
(ShipperID, ShipperName, Total_Orders)
6. List customers who have placed more than 5 orders.
(CustomerID, CustomerName, Total_Orders)
7. Get the average quantity per order for each product.
(ProductID, ProductName, Avg_Quantity)
8. List of products ordered along with their total quantity arrange
them in descending order.
Database Systems-Manual 5 Page | 9
(ProductID, ProductName, Total_Quantity)
9. List the number of orders for each month.
(OrderMonth, Total_Orders)
10. List product categories along with their total sales quantity.
(CategoryID, CategoryName, Total_SalesQuantity)
11. Find employees who have handled more than 20 orders.
(EmployeeID, LastName, Total_Orders)
12. Identify the products that have been ordered more than 50 times.
(ProductID, ProductName, Total_Quantity)
13. List the total number of orders for each year.
(OrderYear, Total_Orders)
14. List employees along with the number of orders they handled.
(EmployeeID, LastName, Total_Orders)
15. Find the products that have never been ordered.
(ProductID, ProductName)
16. List the total number of orders placed in each country.
(Country, Total_Orders)
17. List employees along with the number of unique products they
have worked on.
(EmployeeID, LastName, Unique_Products_Count)
18. List shipping methods along with the number of orders shipped.
(ShipperID, ShipperName, Total_Orders)
Hackerrank Task:
1. Average-Population
2. Revising-Aggregations-The-Averagefunction
3. Revising-Aggregations-Sum
4. Revising-Aggregations-The-Countfunction
5. Weather-Observation-Station-6
6. Weather-Observation-Station-7
7. Weather-Observation-Station-8
8. Weather-Observation-Station-9
9. Weather-Observation-Station-10