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

Leveling Up With Advanced SQL

This document covers advanced SQL topics including CASE statements, VIEWS, and advanced SQL queries utilizing Window Functions and Common Table Expressions (CTEs). It provides examples of SQL queries for categorizing orders, creating and modifying views, and performing complex queries for sales analysis. The document serves as a guide for implementing these advanced SQL techniques in database management.

Uploaded by

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

Leveling Up With Advanced SQL

This document covers advanced SQL topics including CASE statements, VIEWS, and advanced SQL queries utilizing Window Functions and Common Table Expressions (CTEs). It provides examples of SQL queries for categorizing orders, creating and modifying views, and performing complex queries for sales analysis. The document serves as a guide for implementing these advanced SQL techniques in database management.

Uploaded by

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

TDI WEEK 6

This week, we are focusing on advanced SQL topics, specifically on CASE statements,
VIEWS (creating, modifying, and dropping), and advanced SQL queries involving Window
Functions and Common Table Expressions (CTEs).

QUESTIONS
CASE STATEMENTS
1. Create a query to categorize orders based on the `Profit` column:
- 'Loss' for profits less than 0
- 'Low Profit' for profits between 0 and 100
- 'High Profit' for profits greater than 100
SELECT
Order_Date, Order_ID, Product_ID, Category,
CASE
WHEN Profit < 0 THEN 'Loss'
WHEN Profit BETWEEN 0 AND 100 THEN 'Low Profit'
WHEN Profit > 100 THEN 'High Profit'
END AS Profit_Category
FROM [dbo].[Sales_wk6]

2. Write a query to display the `Customer Name` along with a flag indicating if their order
quantity is 'High' (Quantity > 10) or 'Low' (Quantity <= 10).

SELECT
Customer_Name, Quantity,
CASE
WHEN Quantity <= 10 THEN 'Low'
ELSE 'High'
END AS Quantity_Flag
FROM [dbo].[Sales_wk6]

3. Create a query to classify orders based on the `Ship Mode`:


- 'Fast' for 'First Class'
- 'Standard' for 'Standard Class'
SELECT
Order_ID,
Category,
Ship_Mode,
CASE
WHEN Ship_Mode = 'First Class' THEN 'Fast'
WHEN Ship_Mode = 'Standard Class' THEN 'Standard'
ELSE 'Other'
END AS Ship_Mode_Category
FROM [dbo].[Sales_wk6]
4. Write a query to show the `Sales` along with a message indicating if the sales are
'Above Average' or 'Below Average', considering the average sales of all orders.
SELECT Order_ID, Category, Sales,
CASE
WHEN Sales > (SELECT AVG(Sales) FROM [dbo].[Sales_wk6]) THEN 'Above Average'
ELSE 'Below Average'
END AS Sales_Category
FROM [dbo].[Sales_wk6]
GROUP BY Order_ID, Category, Sales

VIEWS
1. Create a view named ‘sales_summary’ that shows the total sales, quantity, and profit
for each region.

CREATE VIEW Sales_summary AS


SELECT
Region, SUM(Sales) AS Total_Sales, SUM(Quantity) AS Total_Quantity, SUM(Profit) AS
Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Region

2. Modify the ‘sales_summary’ view to include the category and subcategory columns.
ALTER VIEW Sales_summary AS
SELECT
Region,
Category,
Sub_Category,
SUM(Sales) AS Total_Sales,
SUM(Quantity) AS Total_Quantity,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Region, Category, Sub_Category

3. Drop the ‘sales_summary’ view.


DROP VIEW Sales_summary

4. Create a view named ‘customer sales’ that shows the total sales and profit for each
customer.
CREATE VIEW Customer_sales AS
SELECT
Customer_ID,
Customer_Name,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Customer_ID, Customer_Name
5. Modify the ‘customer sales’ view to include the city and state columns.
ALTER VIEW Customer_sales AS
SELECT
Customer_ID,
Customer_Name,
City,
State,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Customer_ID, Customer_Name, City, State

6. Create a view named ‘product sales’ that shows the total sales and profit for each
product category and subcategory.
CREATE VIEW Product_sales AS
SELECT
Product_ID,
Category,
Sub_Category,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Product_ID, Category, Sub_Category

ADVANCED SQL QUERIES


1. Write a query to find the top 3 customers with the highest total sales.
SELECT TOP(3)
Customer_Name,
SUM(Sales) AS Total_Sales
FROM [dbo].[Sales_wk6]
GROUP BY Customer_Name
ORDER BY Total_Sales DESC

2. Write a query to find the total sales and profit for each region, including only orders with
a specific ship mode.
SELECT
Region,
Ship_Mode,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Region, Ship_Mode
HAVING Ship_Mode = 'First Class'

3. Write a query to find the average sales and profit for each category and subcategory.
SELECT
Category,
Sub_Category,
CAST(AVG(Sales) as numeric (8,2)) AS Avg_Sales,
CAST(AVG(Profit) as numeric (8,2)) AS Avg_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Category, Sub_Category

4. Write a query to find the top 3 products with the highest total sales and profit.
SELECT TOP(3)
Product_ID,
Category,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Product_ID, Category
ORDER BY Total_Sales DESC, Total_Profit
WINDOW FUNCTIONS
1. Write a query to find the running total of sales for each order
SELECT
Order_ID,
Customer_Name,
Category,
Sales,
SUM(Sales) OVER (ORDER BY Order_ID) AS Running_Total_Sales
FROM [dbo].[Sales_wk6]
GROUP BY Order_ID, Customer_Name, Category, Sales

2. Write a query to find the average sales and profit for each region including a running
total.
WITH Regional_summary AS (
SELECT
Region,
CAST(AVG(Sales) as numeric(8, 2)) AS Avg_Sales,
CAST(AVG(Profit) as numeric(8, 2)) AS Avg_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Region
)
SELECT
Region,
Avg_Sales,
Avg_Profit,
SUM(Avg_Sales) OVER (ORDER BY Region) AS Running_Total_Sales,
SUM(Avg_Profit) OVER (ORDER BY Region) AS Running_Total_Profit
FROM Regional_summary

3. Write a query to find the total sales and profit for each product category and sub
category including a running total.
WITH Product_summary AS (
SELECT
Category,
Sub_Category,
CAST(SUM(Sales) as numeric (8, 2)) AS Total_Sales,
CAST(SUM(Profit) as numeric (8, 2)) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Category, Sub_Category
)
SELECT
Category,
Sub_Category,
Total_Sales,
Total_Profit,
SUM(Total_Sales) OVER (ORDER BY Category) AS Running_Total_Sales,
SUM(Total_Profit) OVER (ORDER BY Category) AS Running_Total_Profit
FROM Product_summary
COMMON TABLE EXPRESSIONS (CTEs)
1. Write a query to find the total sales and profit for each region using a CTE to calculate
the running total.
WITH SalesProfit AS (
SELECT
Region,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Region
)
SELECT
Region,
Total_Sales,
Total_Profit,
SUM(Total_Sales) OVER (ORDER BY Region) AS Running_Total_Sales,
SUM(Total_Profit) OVER (ORDER BY Region) AS Running_Total_Profit
FROM SalesProfit

2. Write a query to find the top 3 customers with the highest total sales, using a CTE to
calculate the ranking.
WITH Customer_Sales_Rank AS (
SELECT
Customer_Name,
SUM(Sales) AS Total_Sales,
RANK() OVER (ORDER BY SUM(Sales) DESC) AS Sales_Rank
FROM [dbo].[Sales_wk6]
GROUP BY Customer_Name
)
SELECT
Customer_Name,
Total_Sales,
Sales_Rank
FROM Customer_Sales_Rank
WHERE Sales_Rank <= 3
ORDER BY Sales_Rank;

3. Write a query to find the average sales for each category and subcategory using a CTE
to calculate the running average.
WITH Product_summary AS (
SELECT
Category,
Sub_Category,
CAST(AVG(Sales) as numeric (8,2)) AS Avg_Sales
FROM [dbo].[Sales_wk6]
GROUP BY Category, Sub_Category
)
SELECT
Category,
Sub_Category,
Avg_Sales,
CAST(AVG(Avg_Sales) OVER (ORDER BY Category) as numeric (8,2)) AS Running_Avg_Sales
FROM Product_summary

4. Write a query to find the total sales and profit for each customer using a CTE to
calculate the running total.

WITH Customer_SalesProfit AS (
SELECT
Customer_ID,
Customer_Name,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
GROUP BY Customer_ID, Customer_Name
)
SELECT
Customer_ID,
Customer_Name,
Total_Sales,
Total_Profit,
SUM(Total_Sales) OVER (ORDER BY Customer_ID) AS Running_Total_Sales,
SUM(Total_Profit) OVER (ORDER BY Customer_ID) AS Running_Total_Profit
FROM Customer_SalesProfit

5. Write a query to find the total sales and profit for each region using a CTE to calculate
the running total and including only orders within a specific year and month.
WITH SalesProfit AS (
SELECT
Order_Date,
Region,
SUM(Sales) AS Total_Sales,
SUM(Profit) AS Total_Profit
FROM [dbo].[Sales_wk6]
WHERE YEAR(Order_Date) = 2011 AND MONTH(Order_Date) = 6
GROUP BY Order_Date, Region
)
SELECT
Order_Date,
Region,
Total_Sales,
Total_Profit,
SUM(Total_Sales) OVER (ORDER BY Region) AS Running_Total_Sales,
SUM(Total_Profit) OVER (ORDER BY Region) AS Running_Total_Profit
FROM SalesProfit

You might also like