A comprehensive SQL learning resource built around a realistic e-commerce database with 15+ interconnected tables. This repository demonstrates SQL proficiency from foundational queries to advanced database optimization techniques used in production environments.
What This Is:
A self-designed PostgreSQL database and 105 progressively challenging SQL problems that mirror real-world business scenarios. Each problem includes detailed solutions with explanatory comments.
Built For:
- Data Analysts preparing for technical interviews
- SQL learners progressing from basics to advanced concepts
- Developers wanting to understand production database patterns
- Anyone needing a practical SQL reference with business context
The database models a complete e-commerce operation with:
- Customer Management: Customers, Reviews, Events (JSONB user actions)
- Product Catalog: Products (with JSONB specs), Categories, Suppliers
- Order Lifecycle: Orders, Order_Items, Payments (split payments), Shipments
- Inventory: Warehouses, Inventory tracking with restock timestamps
- Organizational: Departments, Employees (self-referential manager hierarchy)
- Auditing: Audit_Logs (JSONB change tracking), Product_Price_History
Total Tables: 15 with realistic constraints, foreign keys, and indexes
Complete schema with all relationships, constraints, and data types
π View Detailed Schema Documentation
Core SQL operations every analyst needs:
- SELECT, WHERE, filtering and sorting
- Aggregations (COUNT, SUM, AVG, MIN, MAX)
- JOINs (INNER, LEFT, identifying orphaned records)
- GROUP BY with HAVING
- Subqueries and CASE statements
- Date/time operations and COALESCE
- Basic data manipulation (INSERT, UPDATE, DELETE)
Example Problems:
- B15: Find customers who never placed an order (LEFT JOIN + NULL filtering)
- B23: Products priced above average (subquery pattern)
- B26: Orders in last 90 days (interval arithmetic)
Real-world analytical queries and optimizations:
- CTEs (Common Table Expressions) for complex logic
- Window functions (LAG, LEAD, RANK, RUNNING totals)
- JSONB operations and indexing strategies
- Advanced aggregations (FILTER, STRING_AGG, ARRAY_AGG)
- Index creation and query performance (EXPLAIN ANALYZE)
- Transactions and UPSERT patterns
- Materialized views for reporting
Example Problems:
- I11: Month-over-month revenue growth with percentage change
- I16: Safe transaction with rollback on error
- I24: Cumulative payment tracking per order (window aggregates)
- I33: Safely delete duplicates using ROW_NUMBER()
Enterprise-level database engineering:
- Recursive CTEs (organizational hierarchies, sessionization)
- Advanced indexing (BRIN, GIN, partial, covering indexes)
- Query optimization and parallel execution
- PL/pgSQL stored procedures and triggers
- Row-level security and audit logging
- Partitioning strategies and reindexing
- Deadlock prevention and concurrency control
- Multi-master conflict resolution concepts
Example Problems:
- A2: Recursive employee reporting chain (manager hierarchies)
- A7: User sessionization with 30-minute gap detection
- A18: Inventory reduction trigger with stock validation
- A25: Audit trigger capturing row-level changes to JSONB
- A34: Zero-downtime column migration pattern
These problems solve real business questions across multiple domains:
- Lifetime value (LTV) calculation
- Cohort analysis and retention
- Customer segmentation by spending
- Churn identification and prevention
- Stock level monitoring across warehouses
- Restock scheduling based on sales velocity
- Supplier performance analysis
- Shipment tracking and delivery SLAs
- Monthly/quarterly revenue trends
- Product performance ranking
- Payment reconciliation and refund tracking
- Revenue forecasting with rolling averages
- Index strategy for faster queries
- Query execution plan analysis
- Materialized view refresh patterns
- Safe bulk operations on large tables
π See Detailed Business Use Cases
- Start with
/01-Basics/and work through sequentially - Try solving each problem before viewing solutions
- Run queries against the provided schema
- Read inline comments explaining the "why" behind each solution
Example: Customer Spending Analysis
SELECT
c.customer_name,
COUNT(o.order_id) AS total_orders,
SUM(o.total_amount) AS total_spent
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC NULLS LAST
LIMIT 5;Top 5 customers by spending - actual output from the database c:\Users\Success\Pictures\Screenshots\Screenshot 2025-11-02 084330.png
- Review problem categories matching your target role
- Practice explaining solutions out loud
- Focus on
/03-Advanced/for senior positions - Use business applications section for behavioral interview stories
- Reference specific advanced problems in interviews ("I built a recursive CTE for employee hierarchies")
- Demonstrate understanding of production concepts (indexing, triggers, transactions)
- Show progression from basics to complex real-world patterns
PostgreSQL-Specific Features:
DISTINCT ONfor efficient deduplicationFILTERclause in aggregationsLATERALjoins for correlated subqueriesJSONBoperations with GIN indexingARRAYdata types and operations- Recursive CTEs with cycle detection
- Trigram fuzzy matching (
pg_trgm)
Best Practices Demonstrated:
- Idempotent UPSERT patterns (
ON CONFLICT) - Safe concurrent index creation
- Transaction isolation and rollback handling
- Optimistic concurrency control
- Query performance analysis with
EXPLAIN ANALYZE - Keyset pagination (seek method)
- Data type migrations with minimal downtime
- PostgreSQL 15+ installed
- Basic SQL knowledge (for Basics section)
- psql or any PostgreSQL client
# Clone the repository
git clone https://github.com/LennySHumphrey/SQL-Portfolio-105-Problems.git
cd SQL-Portfolio-105-Problems
# Create your database and load schema
psql -U your_username -d postgres
CREATE DATABASE ecommerce_practice;
\c ecommerce_practice
\i 00-Database-Schema/schema.sql
# Start with basics
\i 01-Basics/basics-problems.sqlWeek 1-2: Foundations
Complete all 35 Basics problems. Focus on understanding JOIN logic and aggregations.
Week 3-4: Analytical Queries
Tackle Intermediate section. Master CTEs and window functions.
Week 5-6: Advanced Concepts
Work through Advanced problems. Study each solution's comments on why certain patterns are used.
Week 7: Portfolio Integration
Map problems to business applications. Practice explaining solutions in plain English.
Created as a comprehensive SQL learning resource and technical portfolio piece. The database schema and all 105 problems were designed from scratch to provide:
- Realistic complexity: Multiple foreign keys, JSONB fields, timestamp tracking
- Progressive difficulty: Each section builds on previous concepts
- Business relevance: Every query solves actual analytical questions
- Production patterns: Includes triggers, procedures, indexing strategies used in real systems
Lenny Success Humphrey
π§ [email protected]
πΌ LinkedIn
π Portfolio
This project is open source and available for educational purposes. Feel free to use it for learning, but please provide attribution if you reference or fork this work.
β If you find this helpful, please star the repository!
