Class 56 of 58 • Practice & Exam Prep

SQL Real-World Practice: Schemas, Queries & Lab Challenges

True mastery in SQL is gained by writing real queries against authentic, multi-table database schemas. In this comprehensive laboratory session, we construct a complete E-Commerce Order Management Schema, populate it with realistic dataset records, and solve 5 essential real-world business intelligence challenges.


The Complete Practice Schema Script

Copy and paste this SQL script into MySQL Workbench, pgAdmin, or DB-Fiddle to create the practice environment:

-- 1. Create Customers Table
CREATE TABLE customers (
    customer_id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    city VARCHAR(50) DEFAULT 'Delhi'
);

-- 2. Create Products Table
CREATE TABLE products (
    product_id INT PRIMARY KEY AUTO_INCREMENT,
    product_name VARCHAR(100) NOT NULL,
    category VARCHAR(50) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

-- 3. Create Orders Table
CREATE TABLE orders (
    order_id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT,
    order_date DATE NOT NULL,
    total_amount DECIMAL(10, 2) NOT NULL,
    status VARCHAR(20) DEFAULT 'Completed',
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

-- Populate Sample Data
INSERT INTO customers (first_name, email, city) VALUES
('Aman Verma', 'aman@test.com', 'Delhi'),
('Pooja Patel', 'pooja@test.com', 'Mumbai'),
('Rohan Singh', 'rohan@test.com', 'Bangalore'),
('Neha Gupta', 'neha@test.com', 'Delhi'),
('Vikram Rao', 'vikram@test.com', 'Hyderabad');

INSERT INTO products (product_name, category, price) VALUES
('Laptop Pro', 'Electronics', 65000.00),
('Wireless Mouse', 'Electronics', 1200.00),
('SQL Master Book', 'Books', 850.00),
('Office Chair', 'Furniture', 7500.00);

INSERT INTO orders (customer_id, order_date, total_amount, status) VALUES
(1, '2026-10-15', 66200.00, 'Completed'),
(2, '2026-10-18', 850.00, 'Completed'),
(1, '2026-11-01', 7500.00, 'Completed'),
(3, '2026-11-04', 1200.00, 'Completed');
-- Note: Customer 4 (Neha) and Customer 5 (Vikram) have placed NO orders yet!

Challenge 1: Customer Lifetime Value (CLV Aggregation)

Task: Find total money spent by each customer, displaying their name, city, and total expenditure. Sort by highest spenders first.

SELECT 
    c.first_name,
    c.city,
    COUNT(o.order_id) AS total_orders_placed,
    SUM(o.total_amount) AS lifetime_value
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.city
ORDER BY lifetime_value DESC;

Challenge 2: Identifying Inactive Customers (LEFT JOIN Anti-Pattern)

Task: Marketing wants to send discount coupons to registered users who have never placed an order.

SELECT c.customer_id, c.first_name, c.email
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;

Challenge 3: Ranking Spenders within Each City (Window Function)

Task: Assign a spending rank to customers partitioned by their city.

SELECT 
    c.city,
    c.first_name,
    SUM(o.total_amount) AS total_spent,
    DENSE_RANK() OVER (
        PARTITION BY c.city 
        ORDER BY SUM(o.total_amount) DESC
    ) AS city_spending_rank
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.city, c.customer_id, c.first_name;

Next, test your theoretical and practical knowledge with Top 100+ SQL Interview & Exam Q&A!


Frequently Asked Questions (FAQ)

You can copy and paste the complete schema script below directly into free online SQL playgrounds such as DB-Fiddle.com (MySQL 8.0/PostgreSQL), SQLiteOnline.com, or SQLFiddle.com.

This lab includes customer lifetime value aggregation, detecting unfulfilled orders via LEFT JOIN, ranking best-selling products with window functions, and finding top departmental earners.

Yes! These scenarios are specifically aligned with BCA, BSc Computer Science, and MCA laboratory practical curriculum requirements.

Next Step in Your SQL Database Learning Path:

Proceed to the next class in sequence to build production mastery of relational database architecture.

Continue to Class 57: 100+ Interview Q&A → Previous: ← Full-Text Search | Full Syllabus


Related SQL Masterclass Lessons & Recommended Next Topics

SQL Master Syllabus →

Review the complete 58-class step-by-step curriculum.

100+ SQL Interview Q&A →

Test your knowledge against real university and FAANG questions.

Mastering Relational Joins →

Review inner, left, and outer joins used in practice exercises.

Window Functions Practice →

Review running totals, rankings, and lead/lag calculations.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!