Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free
lesson

Closures and Higher-Order Functions

Closures and Higher-Order Functions

In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in SQL and why it matters in real work.

Why it matters

Functions that take or return functions unlock concise, composable code.

Key ideas

  • Functions as values
  • map / filter / reduce
  • Callbacks
  • Composition

In practice

Here's how it looks in idiomatic SQL:

-- Window functions: an aggregate parameterized by a frame (HOF-like)
SELECT
  employee_id,
  salary,
  AVG(salary)  OVER (PARTITION BY dept_id)              AS dept_avg,
  RANK()       OVER (PARTITION BY dept_id
                     ORDER BY salary DESC)              AS rank_in_dept,
  SUM(salary)  OVER (ORDER BY hire_date
                     ROWS UNBOUNDED PRECEDING)          AS running_total
FROM employees;

SQL note: Window functions are the closest SQL gets to higher-order behavior — an aggregate like AVG is 'applied over' a configurable partition/frame without collapsing rows, letting you compute running totals and ranks that plain GROUP BY cannot.

Try it yourself

Exercise: In SQL, use map and filter to get the squares of the even numbers.

Recap

You now understand closures and higher-order functions and can apply it in SQL. Mark this lesson complete and continue to the next one.