SQL - 04
SQL - 04
SQL Aliases
SQL Aggregate Functions
SQL Aliases
Syntax : SQL Aliases
Example : SQL Aliases
Example : SQL Aliases
SQL Functions
SQL : Aggregate Function
Count Function
Example : Count Function
SUM () Function
AVG () Function
MAX() Function
MIN() Function
Exercise – Aggregate Functions
Let's assume the table is called Sales with the following columns:
SalesID (Primary Key)
Product (VARCHAR)
Category (VARCHAR)
Quantity (INT)
Price (DECIMAL)
SaleDate (DATE)
Exercise – Aggregate Functions
1. What is the total revenue generated?(Use SUM(Quantity * Price))
2. How many products have been sold in total?(Use SUM(Quantity))
3. What is the average price of products?(Use AVG(Price))
4. How many unique product categories exist?(Use COUNT(DISTINCT Category))
5. What is the highest price of a product in the table?(Use MAX(Price))
6. What is the lowest price of a product in the table?(Use MIN(Price))
7. What is the average quantity sold per transaction?(Use AVG(Quantity))
8. How many total sales records are there in the table?(Use COUNT(SalesID) or COUNT(*))
9. What is the total revenue for each product category?(Use SUM(Quantity * Price) grouped by Category)
10. What is the average price of products in each category?(Use AVG(Price) grouped by Category)
11. Which category has the highest total revenue?(Use SUM(Quantity * Price) with GROUP BY and ORDER
BY DESC)
12. What is the total quantity sold for each product?(Use SUM(Quantity) grouped by Product)
13. What is the maximum quantity sold in a single transaction?(Use MAX(Quantity))
14. What is the total revenue for a specific product, e.g., "Laptop"?(Use SUM(Quantity * Price) with a
WHERE Product = 'Laptop')
Thank You