Skip to main content
SQL • LESSON 160

Last Value

How can we retrieve the lowest salary across the entire department frame?

Advanced3 Minutes1730 XP
🤔 THE QUESTION

How can we retrieve the lowest salary across the entire department frame?

💡 WHAT IS IT?

LAST_VALUE() with frame ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING captures the final row in the partition.

🎯 WHAT IS IT USED FOR?

Full-window extremes, comparing against lowest tier in a partition, range spreads.

💻 EXAMPLE
SELECT
department_id,
name,
salary,
LAST_VALUE(salary) OVER (
    PARTITION BY department_id
    ORDER BY salary
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS lowest_salary
FROM employees;

🎯 Mission Objectives

Practice typing production-grade SQL code for Last Value.

  • LAST_VALUE()
  • Full frame specification
  • UNBOUNDED FOLLOWING
  • Partition boundary extraction