Python is fantastic for modeling, but SQL (Structured Query Language) is where real enterprise data lives. In this class, we master robust relational query techniques, dissect the internal SQL execution pipeline, and review join patterns essential for data science.
Writing performant SQL requires understanding how the database query planner evaluates clauses internally:
1. FROM & JOIN → 2. WHERE → 3. GROUP BY → 4. HAVING → 5. SELECT → 6. DISTINCT → 7. ORDER BY → 8. LIMIT
Notice that column aliases defined in SELECT cannot be referenced in WHERE because WHERE executes before SELECT!
| Join Type | Output Behavior | Data Science Use Case |
|---|---|---|
| INNER JOIN | Returns records with matching keys in BOTH tables. | Analyze users who have made at least one verified transaction. |
| LEFT JOIN | All rows from Left table, matched rows from Right table (NULLs if missing). | Audit user churn (find registered users where order_id IS NULL). |
| FULL OUTER JOIN | All rows from both tables, filling NULLs wherever matches do not exist. | Reconcile inventory catalogs between two newly merged company databases. |
| CROSS JOIN | Cartesian product (every row in Table A paired with every row in Table B). | Generate grid search combinations or complete date/store matrices. |
-- Identify high-value customers with 5+ orders who spent over $1,000 in 2025
SELECT
u.user_id,
u.first_name,
u.country,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(o.total_amount), 2) AS total_revenue,
ROUND(AVG(o.total_amount), 2) AS avg_order_value,
CASE
WHEN SUM(o.total_amount) >= 5000 THEN 'VIP Platinum'
WHEN SUM(o.total_amount) >= 2500 THEN 'Gold Tier'
ELSE 'Silver Tier'
END AS customer_tier
FROM users u
INNER JOIN orders o ON u.user_id = o.user_id
WHERE o.order_status = 'COMPLETED'
AND o.created_at >= '2025-01-01'
GROUP BY u.user_id, u.first_name, u.country
HAVING COUNT(o.order_id) >= 5
AND SUM(o.total_amount) >= 1000.00
ORDER BY total_revenue DESC
LIMIT 50;
What is the output difference between COUNT(*) and COUNT(column_name)? (Answer: COUNT(*) counts all rows including those with NULLs; COUNT(column_name) counts only rows where the specified column is non-NULL).
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Elevate your SQL skills with ROW_NUMBER, RANK, LAG, and CTEs.
Build retention curves, rolling metrics, and conversion funnels in SQL.
Translate SQL queries into high-performance Python DataFrame code.
Execute full ANSI SQL queries directly inside modern web applications.