SQL Grouping, Aggregate Functions, and Joins
Dr M AOUDE
January 30, 2025
1 Step 1: Creating Databases
1.1 1. SalesExample Database
1 CREATE DATABASE SalesExample ;
2 GO
3
4 USE SalesExample ;
5 GO
1.2 2. GroupingDemo Database
1 CREATE DATABASE GroupingDemo ;
2 GO
3
4 USE GroupingDemo ;
5 GO
2 Step 2: Creating Tables with Foreign Keys
2.1 1. Categories Table
1 CREATE TABLE Categories (
2 CategoryID INT PRIMARY KEY ,
3 CategoryName VARCHAR (50) NOT NULL
4 );
2.2 2. Products Table (Foreign Key: CategoryID)
1 CREATE TABLE Products (
2 ProductID INT PRIMARY KEY ,
3 ProductName VARCHAR (50) NOT NULL ,
4 CategoryID INT ,
5 UnitPrice DECIMAL (10 ,2) ,
6 CONSTRAINT F K _ P r o d u c t s _ C a t e g o r i e s FOREIGN KEY ( CategoryID )
7 REFERENCES Categories ( CategoryID )
8 );
1
2.3 3. Orders Table (Foreign Key: ProductID)
1 CREATE TABLE Orders (
2 OrderID INT PRIMARY KEY ,
3 OrderDate DATE NOT NULL ,
4 ProductID INT ,
5 Quantity INT NOT NULL ,
6 CONSTRAINT FK _O rd er s_ Pr od uc ts FOREIGN KEY ( ProductID )
7 REFERENCES Products ( ProductID )
8 );
2.4 4. Customers Table (GroupingDemo)
1 CREATE TABLE Customers (
2 CustomerID INT PRIMARY KEY ,
3 CustomerName VARCHAR (50) NOT NULL
4 );
2.5 5. Orders Table (Foreign Key: CustomerID)
1 CREATE TABLE Orders (
2 OrderID INT PRIMARY KEY ,
3 OrderDate DATE NOT NULL ,
4 TotalAmount DECIMAL (10 , 2) NOT NULL ,
5 CustomerID INT ,
6 CONSTRAINT F K _ Or d e rs _ C us t o me r s FOREIGN KEY ( CustomerID )
7 REFERENCES Customers ( CustomerID )
8 ON DELETE CASCADE
9 ON UPDATE CASCADE
10 );
3 Step 3: Inserting Sample Data
1 -- Insert Data into Categories
2 INSERT INTO Categories VALUES
3 (1 , ’ Electronics ’) ,
4 (2 , ’ Books ’) ,
5 (3 , ’ Clothing ’) ;
6
7 -- Insert Data into Products
8 INSERT INTO Products VALUES
9 (1 , ’ Laptop ’ , 1 , 1200.00) ,
10 (2 , ’ Smartphone ’ , 1 , 800.00) ,
11 (3 , ’ Novel ’ , 2 , 20.00) ,
12 (4 , ’T - Shirt ’ , 3 , 25.00) ,
13 (5 , ’ Textbook ’ , 2 , 100.00) ;
14
15 -- Insert Data into Orders
16 INSERT INTO Orders VALUES
17 (1 , ’ 2024 -01 -01 ’ , 1 , 2) ,
18 (2 , ’ 2024 -01 -01 ’ , 2 , 3) ,
2
19 (3 , ’ 2024 -01 -02 ’ , 1, 1) ,
20 (4 , ’ 2024 -01 -02 ’ , 3, 5) ,
21 (5 , ’ 2024 -01 -03 ’ , 4, 10) ,
22 (6 , ’ 2024 -01 -03 ’ , 5, 2) ,
23 (7 , ’ 2024 -01 -03 ’ , 2, 1) ;
4 Step 4: Grouping and Aggregate Functions
4.1 1. Total Sales by Category
1 SELECT
2 c . CategoryName ,
3 COUNT ( o . OrderID ) as TotalOrders ,
4 SUM ( o . Quantity ) as TotalQuantity ,
5 SUM ( o . Quantity * p . UnitPrice ) as TotalRevenue
6 FROM Categories c
7 LEFT JOIN Products p ON c . CategoryID = p . CategoryID
8 LEFT JOIN Orders o ON p . ProductID = o . ProductID
9 GROUP BY c . CategoryName ;
4.2 2. Customers with More Than 1 Order
1 SELECT c . CustomerName , COUNT ( o . OrderID ) AS NumberOfOrders
2 FROM Customers c
3 LEFT JOIN Orders o ON c . CustomerID = o . CustomerID
4 GROUP BY c . CustomerName
5 HAVING COUNT ( o . OrderID ) > 1;
4.3 3. Order Customers by Total Amount Spent
1 SELECT c . CustomerName , SUM ( o . TotalAmount ) AS TotalSpent
2 FROM Customers c
3 LEFT JOIN Orders o ON c . CustomerID = o . CustomerID
4 GROUP BY c . CustomerName
5 ORDER BY TotalSpent DESC ;
4.4 4. GROUP BY with ROLLUP (Subtotal and Grand Total)
1 SELECT c . CategoryName , SUM ( o . Quantity * p . UnitPrice ) AS
TotalRevenue
2 FROM Categories c
3 JOIN Products p ON c . CategoryID = p . CategoryID
4 JOIN Orders o ON p . ProductID = o . ProductID
5 GROUP BY ROLLUP ( c . CategoryName ) ;
3
4.5 5. GROUP BY with CUBE (All Possible Subtotals)
1 SELECT c . CategoryName , SUM ( o . Quantity * p . UnitPrice ) AS
TotalRevenue
2 FROM Categories c
3 JOIN Products p ON c . CategoryID = p . CategoryID
4 JOIN Orders o ON p . ProductID = o . ProductID
5 GROUP BY CUBE ( c . CategoryName ) ;
5 Step 5: Cleanup
1 -- Drop foreign key constraints
2 ALTER TABLE Orders DROP CONSTRAINT F K_O rd er s_ Pr od uc ts ;
3 ALTER TABLE Products DROP CONSTRAINT F K _ P r o d u c t s _ C a t e g o r i e s ;
4 ALTER TABLE Orders DROP CONSTRAINT F K _ Or d e rs _ C us t o me r s ;
5
6 -- Drop tables
7 DROP TABLE Orders ;
8 DROP TABLE Products ;
9 DROP TABLE Categories ;
10 DROP TABLE Customers ;
11
12 -- Drop databases
13 USE master ;
14 GO
15 DROP DATABASE SalesExample ;
16 DROP DATABASE GroupingDemo ;