0% found this document useful (0 votes)
19 views4 pages

DA - Hands On - Week 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

DA - Hands On - Week 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Session 4: SQL Filtering, Sorting,

Aggregation & Basic Joins (Using Oracle


Live SQL)
Objective:
✔ Learn to filter, sort, and aggregate data using SQL.
✔ Perform basic joins to extract business insights.
✔ Execute multiple hands-on queries to strengthen SQL concepts.

1️⃣ Creating the Database Structure


Scenario:
You work for a restaurant chain analyzing customer orders, menu items, and employee
details. You need to store, retrieve, and analyze data using SQL.

Activity: Define the structure of the database.

Table 1: Customers (Stores customer details)

✔ CustomerID → Unique ID for each customer (Primary Key)


✔ Name → Customer’s full name
✔ City → City of residence
✔ JoinDate → When the customer joined the restaurant’s loyalty program

CREATE TABLE Customers (


CustomerID NUMBER PRIMARY KEY,
Name VARCHAR2(100),
City VARCHAR2(50),
JoinDate DATE
);

Table 2: Menu (Stores restaurant menu items)

✔ DishID → Unique ID for each dish (Primary Key)


✔ DishName → Name of the dish
✔ Category → Type of cuisine (Italian, Japanese, etc.)
✔ Price → Cost of the dish

CREATE TABLE Menu (


DishID NUMBER PRIMARY KEY,
DishName VARCHAR2(100),
Category VARCHAR2(50),
Price NUMBER(10,2)
);
Table 3: Orders (Stores customer orders)

✔ OrderID → Unique ID for each order (Primary Key)


✔ CustomerID → Reference to the customer placing the order (Foreign Key)
✔ DishID → Reference to the dish ordered (Foreign Key)
✔ OrderDate → Date of order placement
✔ Quantity → Number of items ordered
✔ TotalPrice → Total cost of the order

CREATE TABLE Orders (


OrderID NUMBER PRIMARY KEY,
CustomerID NUMBER,
DishID NUMBER,
OrderDate DATE,
Quantity NUMBER,
TotalPrice NUMBER(10,2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (DishID) REFERENCES Menu(DishID)
);

Table 4: Employees (Stores employee details)

✔ EmployeeID → Unique ID for each employee (Primary Key)


✔ Name → Employee’s full name
✔ Role → Job position (Chef, Waiter, etc.)
✔ Salary → Employee’s monthly salary

CREATE TABLE Employees (


EmployeeID NUMBER PRIMARY KEY,
Name VARCHAR2(100),
Role VARCHAR2(50),
Salary NUMBER(10,2)
);

2️⃣ Inserting Sample Data (10 Records Each)


Activity: Populate tables with restaurant data for realistic hands-on practice.

Adding 10 Customers
INSERT INTO Customers VALUES (1, 'Alice Johnson', 'New York',
TO_DATE('2023-01-15', 'YYYY-MM-DD'));
INSERT INTO Customers VALUES (2, 'Bob Smith', 'Los Angeles', TO_DATE('2023-
02-10', 'YYYY-MM-DD'));
-- (Add remaining 8 customers similarly)

Adding 10 Menu Items


INSERT INTO Menu VALUES (101, 'Pasta Alfredo', 'Italian', 12.99);
INSERT INTO Menu VALUES (102, 'Sushi Roll', 'Japanese', 15.99);
-- (Add remaining 8 dishes similarly)

Adding 10 Orders
INSERT INTO Orders VALUES (1001, 1, 101, TO_DATE('2023-07-10', 'YYYY-MM-
DD'), 2, 25.98);
INSERT INTO Orders VALUES (1002, 2, 102, TO_DATE('2023-07-12', 'YYYY-MM-
DD'), 1, 15.99);
-- (Add remaining 8 orders similarly)

Adding 10 Employees
INSERT INTO Employees VALUES (1, 'David Miller', 'Chef', 4000.00);
INSERT INTO Employees VALUES (2, 'Sarah Lee', 'Waiter', 2500.00);
-- (Add remaining 8 employees similarly)

3️⃣ Querying & Filtering Data


Objective: Learn how to retrieve specific data using WHERE, ORDER BY, and conditions.

1️⃣ Retrieve Customers from a Specific City


SELECT * FROM Customers WHERE City = 'New York';

Explanation: This query finds all customers who live in New York.

2️⃣ Find Expensive Orders (Above $30)


SELECT * FROM Orders WHERE TotalPrice > 30;

Explanation: Filters orders where total price exceeds $30.

3️⃣ Sort Menu by Price (Highest First)


SELECT * FROM Menu ORDER BY Price DESC;

Explanation: Sorts menu items from highest to lowest price.

4⃣ Find Total Orders Per Customer


SELECT CustomerID, COUNT(*) AS OrderCount FROM Orders GROUP BY CustomerID;

Explanation: Groups orders by customer and counts how many orders each
customer placed.

5️⃣ Find Employees with Salaries Above $4000


SELECT Name, Role FROM Employees WHERE Salary > 4000;
Explanation: Lists all employees earning more than $4000 per month.

4⃣ Basic Joins for Business Insights


Objective: Learn to combine multiple tables using JOIN for meaningful data analysis.

1️⃣ Retrieve Orders with Customer Names


SELECT O.OrderID, C.Name, O.OrderDate, O.TotalPrice
FROM Orders O
JOIN Customers C ON O.CustomerID = C.CustomerID;

Explanation:
✔ Fetches order details along with customer names.
✔ JOIN condition: Matches CustomerID in both tables.

2️⃣ Retrieve Orders with Dish Names & Prices


SELECT O.OrderID, M.DishName, M.Price, O.Quantity, O.TotalPrice
FROM Orders O
JOIN Menu M ON O.DishID = M.DishID;

Explanation:
✔ Fetches order details along with dish names and prices.
✔ JOIN condition: Matches DishID in both tables.

✅ Summary: Hands-on SQL Practice with 10 Sample


Records
✔ Learned: Filtering, Sorting, Aggregation, Basic Joins.
✔ Practiced 20+ Queries with 10 sample records per table.
✔ Outcome: Strong SQL foundation through real-world data analytics.

You might also like