0% found this document useful (0 votes)
15 views

SQL_Aggregations & Grouping

The document provides a series of SQL queries for data aggregation and grouping across various datasets, including orders, employees, products, and reviews. Key operations include calculating totals, averages, counts, and finding maximum and minimum values for different attributes. Each query is structured to retrieve specific insights, such as total sales per month and average ratings per product category.

Uploaded by

mistrijuthika50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL_Aggregations & Grouping

The document provides a series of SQL queries for data aggregation and grouping across various datasets, including orders, employees, products, and reviews. Key operations include calculating totals, averages, counts, and finding maximum and minimum values for different attributes. Each query is structured to retrieve specific insights, such as total sales per month and average ratings per product category.

Uploaded by

mistrijuthika50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Aggregations and grouping

1. Calculate the total quantity and total amount for each order.

Dataset: Orders (order_id, order_date), Order_Items (order_id, product_id, quantity, amount)

SELECT
o.order_id,
SUM(oi.quantity) AS total_quantity,
SUM(oi.amount) AS total_amount
FROM
Orders o
LEFT JOIN
Order_Items oi ON o.order_id = oi.order_id
GROUP BY
o.order_id;

2. Find the average age and the number of employees for each job title.

Dataset: Employees (employee_id, employee_name, age, job_title)

SELECT
job_title,
AVG(age) AS average_age,
COUNT(*) AS number_of_employees
FROM
Employees
GROUP BY
job_title;

3. Get the total number of products in each category.

Dataset: Products (product_id, product_name, category_id), Categories (category_id,


category_name)

SELECT
c.category_name,
COUNT(p.product_id) AS total_products
FROM
Categories c
LEFT JOIN
Products p ON c.category_id = p.category_id
GROUP BY
c.category_name;

Sinduja Anthannagari pg. 1


4. Calculate the average rating and the number of reviews for each product.

Dataset: Products (product_id, product_name), Reviews (product_id, rating)

SELECT
p.product_name,
AVG(r.rating) AS average_rating,
COUNT(r.review_id) AS number_of_reviews
FROM
Products p
LEFT JOIN
Reviews r ON p.product_id = r.product_id
GROUP BY
p.product_id, p.product_name;

5. Find the customers with the highest and lowest total order amounts.

Dataset: Customers (customer_id, customer_name), Orders (order_id, customer_id, order_amount)

WITH CustomerTotals AS (
SELECT
c.customer_id,
c.customer_name,
SUM(o.order_amount) AS total_order_amount,
RANK() OVER (ORDER BY SUM(o.order_amount) DESC) AS rank_desc,
RANK() OVER (ORDER BY SUM(o.order_amount) ASC) AS rank_asc
FROM
Customers c
LEFT JOIN
Orders o ON c.customer_id = o.customer_id
GROUP BY
c.customer_id, c.customer_name
)
SELECT
customer_id,
customer_name,
total_order_amount
FROM
CustomerTotals
WHERE
rank_desc = 1
OR
rank_asc = 1;

___________________________________________________________________________________

WITH CustomerTotals AS (
SELECT
c.customer_id,
c.customer_name,
SUM(o.order_amount) AS total_order_amount
FROM
Customers c
JOIN
Orders o ON c.customer_id = o.customer_id

Sinduja Anthannagari pg. 2


GROUP BY
c.customer_id, c.customer_name
)
SELECT
customer_id,
customer_name,
total_order_amount
FROM
CustomerTotals
WHERE
total_order_amount = (SELECT MAX(total_order_amount) FROM CustomerTotals)
OR
total_order_amount = (SELECT MIN(total_order_amount) FROM CustomerTotals);

6. Get the maximum and minimum ages for each department.

Dataset: Employees (employee_id, employee_name, age,department)

SELECT
department,
MAX(age) AS max_age,
MIN(age) AS min_age
FROM
Employees
GROUP BY
department;

7. Calculate the total sales amount and the number of orders for each month.

Dataset: Orders (order_id, order_date, order_amount)

SELECT
YEAR(order_date) AS order_year,
MONTH(order_date) AS order_month,
SUM(order_amount) AS total_sales_amount,
COUNT(*) AS number_of_orders
FROM
Orders
GROUP BY
YEAR(order_date), MONTH(order_date)
ORDER BY
order_year, order_month;

Sinduja Anthannagari pg. 3


8. Find the average price and the number of products for each supplier.

Dataset: Products (product_id, product_name, price,supplier_id),

Suppliers (supplier_id, supplier_name)

SELECT
s.supplier_id,
s.supplier_name,
AVG(p.price) AS average_price,
COUNT(*) AS number_of_products
FROM
Suppliers s
LEFT JOIN
Products p ON s.supplier_id = p.supplier_id
GROUP BY
s.supplier_id, s.supplier_name
ORDER BY
s.supplier_id;

9. Get the maximum and minimum prices for each product category.

Dataset: Products (product_id, product_name, category_id), Categories (category_id,


category_name)

SELECT
c.category_id,
c.category_name,
MAX(p.price) AS max_price,
MIN(p.price) AS min_price
FROM
Categories c
LEFT JOIN
Products p ON c.category_id = p.category_id
GROUP BY
c.category_id, c.category_name
ORDER BY
c.category_id;

10. Calculate the average rating and the number of reviews for each product category.

Dataset: Products (product_id, product_name, category_id), Reviews (product_id, rating)


SELECT
p.category_id,
c.category_name,
AVG(r.rating) AS average_rating,
COUNT(r.rating) AS number_of_reviews
FROM
Products p
LEFT JOIN Reviews r ON p.product_id = r.product_id
LEFT JOIN Categories c ON p.category_id = c.category_id
GROUP BY p.category_id, c.category_name
ORDER BY p.category_id;

Sinduja Anthannagari pg. 4


Sinduja Anthannagari pg. 5

You might also like