Block 128: SQL Aggregations & Window Functions
Write advanced SQL queries for analytics.
Concepts
- GROUP BY with HAVING clause
- Aggregate functions: COUNT, SUM, AVG, MIN, MAX
- Window functions: ROW_NUMBER, RANK, LAG, LEAD
- Subqueries and CTEs (WITH clause)
Code Examples
See exercise below.
Exercise
Write a query using GROUP BY + HAVING to find courses with more than 2 enrolled students. Use a window function to rank students by grade within each course.
Homework
Explain the difference between WHERE and HAVING. When can each be used? Friday