Lesson 50 โ€ข Expert Capstone

    Final Project: Build a Production-Ready Database ๐Ÿ’ผ

    Put everything together โ€” design, build, query, optimise, and secure a complete e-commerce database system.

    What You'll Build in This Project

    • โ€ข A normalised e-commerce schema with constraints and indexes
    • โ€ข Business analytics queries (revenue, LTV, rankings)
    • โ€ข A transactional stored procedure with stock validation
    • โ€ข Dashboard views and inventory alert systems
    • โ€ข Security policies, audit triggers, and materialised views

    ๐ŸŽฏ Project Overview

    You're building the database for an online store. This capstone project tests every concept from the entire course: table design, JOINs, aggregates, window functions, transactions, views, procedures, indexes, security, and performance.

    ๐Ÿ“‹ 5 Challenges โ€” Complete Them All

    1. Schema Design โ€” Normalised tables with constraints and indexes
    2. Analytics Queries โ€” Revenue reports, LTV, product rankings
    3. Order Processing โ€” Transactional procedure with stock checks
    4. Dashboard Views โ€” KPIs, inventory alerts, monitoring
    5. Security & Performance โ€” RLS, audit triggers, materialised views

    ๐Ÿ’ก Pro Tip: Work through each challenge in order. Later challenges build on earlier ones. Test each piece before moving to the next โ€” just like building a real production system!

    Challenge 1: Database Schema Design

    Design and create the complete e-commerce schema with constraints and indexes

    Try it Yourself ยป
    SQL
    -- CHALLENGE 1: Design & Build the Schema
    -- Create a complete e-commerce database from scratch
    
    CREATE TABLE customers (
        customer_id SERIAL PRIMARY KEY,
        email VARCHAR(255) UNIQUE NOT NULL,
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        phone VARCHAR(20),
        created_at TIMESTAMPTZ DEFAULT NOW(),
        is_vip BOOLEAN DEFAULT false
    );
    
    CREATE TABLE categories (
        category_id SERIAL PRIMARY KEY,
        name VARCHAR(100) NOT NULL,
        parent_id INT REFERENCES cate
    ...

    Challenge 2: Business Analytics Queries

    Write revenue reports, customer LTV, and product ranking queries

    Try it Yourself ยป
    SQL
    -- CHALLENGE 2: Business Analytics Queries
    -- Answer real business questions with SQL
    
    -- 1. Monthly revenue report with month-over-month growth
    WITH monthly AS (
        SELECT
            DATE_TRUNC('month', created_at) AS month,
            COUNT(DISTINCT order_id) AS total_orders,
            COUNT(DISTINCT customer_id) AS unique_customers,
            ROUND(SUM(total), 2) AS revenue
        FROM orders
        WHERE status != 'cancelled'
        GROUP BY DATE_TRUNC('month', created_at)
    )
    SELECT
        month,
        total_orders
    ...

    Challenge 3: Order Processing Procedure

    Build a transactional stored procedure with stock validation and audit logging

    Try it Yourself ยป
    SQL
    -- CHALLENGE 3: Stored Procedure with Transaction Safety
    -- Process an order with stock validation and audit logging
    
    CREATE TABLE order_audit (
        id SERIAL PRIMARY KEY,
        order_id INT,
        action VARCHAR(50) NOT NULL,
        details JSONB,
        performed_by TEXT DEFAULT current_user,
        performed_at TIMESTAMPTZ DEFAULT NOW()
    );
    
    CREATE OR REPLACE FUNCTION place_order(
        p_customer_id INT,
        p_items JSONB  -- [{"product_id": 1, "quantity": 2}, ...]
    )
    RETURNS INT AS $$
    DECLARE
        v_order_id
    ...

    Challenge 4: Dashboard Views & Alerts

    Create KPI dashboards, inventory alerts, and monitoring views

    Try it Yourself ยป
    SQL
    -- CHALLENGE 4: Dashboard Views & Inventory Alerts
    
    -- Revenue dashboard with KPIs
    CREATE VIEW dashboard_kpis AS
    WITH current_month AS (
        SELECT SUM(total) AS revenue, COUNT(*) AS orders
        FROM orders
        WHERE created_at >= DATE_TRUNC('month', NOW())
          AND status != 'cancelled'
    ),
    prev_month AS (
        SELECT SUM(total) AS revenue, COUNT(*) AS orders
        FROM orders
        WHERE created_at >= DATE_TRUNC('month', NOW()) - INTERVAL '1 month'
          AND created_at < DATE_TRUNC('month', NOW())
     
    ...

    Challenge 5: Security & Performance

    Add materialised views, RLS policies, and audit triggers

    Try it Yourself ยป
    SQL
    -- CHALLENGE 5: Performance Optimisation & Security
    
    -- 1. Create a materialised view for slow reports
    CREATE MATERIALIZED VIEW mv_daily_sales AS
    SELECT
        created_at::DATE AS sale_date,
        COUNT(*) AS orders,
        ROUND(SUM(total), 2) AS revenue,
        COUNT(DISTINCT customer_id) AS unique_customers
    FROM orders
    WHERE status != 'cancelled'
    GROUP BY created_at::DATE;
    
    CREATE UNIQUE INDEX idx_mv_daily_date ON mv_daily_sales(sale_date);
    -- Refresh: REFRESH MATERIALIZED VIEW CONCURRENTLY mv_daily_sal
    ...

    ๐Ÿš€ What's Next? Career Paths

    Career PathTypical RoleCore SkillsSalary (USD)
    ๐Ÿ“Š Data AnalystExtract insights, build reportsSQL, Excel, Tableau$65,000 โ€“ $95,000
    โš™๏ธ Backend DeveloperBuild APIs with database accessSQL, Python/Node, Git$85,000 โ€“ $140,000
    ๐Ÿ”ง Database AdministratorManage DB performance & securitySQL, Linux, monitoring$80,000 โ€“ $130,000
    ๐Ÿงช Data EngineerBuild ETL pipelines & warehousesSQL, Spark, Airflow$100,000 โ€“ $160,000
    ๐Ÿค– ML EngineerFeature engineering, model dataSQL, Python, ML frameworks$110,000 โ€“ $180,000
    ๐Ÿ’ผ FreelancerDatabase design & optimisationAll of the above$50 โ€“ $150 / hour

    ๐Ÿช™ Business Opportunities You Can Build

    • โ€ข SaaS analytics dashboards for small businesses
    • โ€ข Database consulting โ€” performance audits and schema reviews
    • โ€ข ETL pipeline services โ€” help companies migrate and unify data
    • โ€ข Data API products โ€” curated datasets via REST APIs
    • โ€ข Educational content โ€” courses, tutorials, and SQL challenges

    ๐ŸŽฏ Skill Level Assessment

    ๐ŸŸข Beginner (Lessons 1โ€“5)

    SELECT, WHERE, ORDER BY, basic filtering. You can query any single table.

    ๐Ÿ”ต Intermediate (Lessons 6โ€“10)

    JOINs, aggregates, GROUP BY, subqueries, data modification. You can work with multiple tables.

    ๐ŸŸฃ Advanced (Lessons 11โ€“15)

    Indexes, views, procedures, transactions, window functions. You can design and optimise databases.

    ๐ŸŸก Expert (Lessons 16โ€“50)

    Internals, replication, sharding, warehousing, security, big data SQL. You can architect production systems.

    โœ… Complete Course Skills Checklist

    CREATE TABLE with constraints
    SELECT, WHERE, ORDER BY, LIMIT
    INNER, LEFT, RIGHT, FULL JOINs
    Aggregate functions (SUM, AVG, COUNT)
    GROUP BY & HAVING
    Subqueries & EXISTS
    INSERT, UPDATE, DELETE safely
    Indexes & EXPLAIN plans
    Views & Stored Procedures
    Transactions & ACID
    Window functions & CTEs
    Normalisation (1NF to 5NF)
    Query optimisation
    Partitioning & Sharding
    Replication & Failover
    Row-Level Security
    Triggers & Audit trails
    Data warehousing (Star schemas)
    Big Data SQL (Hive, Spark, DuckDB)
    Performance benchmarking

    ๐Ÿ“š Free Resources to Keep Learning

    ๐Ÿ˜ PostgreSQL Documentation

    The gold standard for SQL reference โ€” comprehensive and free.

    ๐Ÿ“Š Mode Analytics SQL Tutorial

    Practice SQL with real datasets and interactive exercises.

    ๐Ÿ† LeetCode / HackerRank SQL

    SQL coding challenges used in job interviews at top companies.

    ๐Ÿฆ† DuckDB Playground

    Practice modern SQL locally with zero setup โ€” just download and query.

    ๐ŸŽ‰ Congratulations โ€” Course Complete!

    You've completed all 50 lessons of the SQL course! You now have the skills to design databases, write complex analytical queries, optimise performance, secure data with RLS, and build production-ready database systems.

    Keep practising with real datasets, contribute to open-source projects, and explore the career paths above. Your SQL skills are in high demand โ€” every company needs someone who can talk to their data!

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service