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
- Schema Design โ Normalised tables with constraints and indexes
- Analytics Queries โ Revenue reports, LTV, product rankings
- Order Processing โ Transactional procedure with stock checks
- Dashboard Views โ KPIs, inventory alerts, monitoring
- 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
-- 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
-- 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
-- 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
-- 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
-- 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 Path | Typical Role | Core Skills | Salary (USD) |
|---|---|---|---|
| ๐ Data Analyst | Extract insights, build reports | SQL, Excel, Tableau | $65,000 โ $95,000 |
| โ๏ธ Backend Developer | Build APIs with database access | SQL, Python/Node, Git | $85,000 โ $140,000 |
| ๐ง Database Administrator | Manage DB performance & security | SQL, Linux, monitoring | $80,000 โ $130,000 |
| ๐งช Data Engineer | Build ETL pipelines & warehouses | SQL, Spark, Airflow | $100,000 โ $160,000 |
| ๐ค ML Engineer | Feature engineering, model data | SQL, Python, ML frameworks | $110,000 โ $180,000 |
| ๐ผ Freelancer | Database design & optimisation | All 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
๐ 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.