0
0
PostgreSQLquery~10 mins

FIRST_VALUE and LAST_VALUE in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the first salary value in each department.

PostgreSQL
SELECT department_id, [1](salary) OVER (PARTITION BY department_id ORDER BY salary) AS first_salary FROM employees;
Drag options to blanks, or click blank then click option'
AFIRST_VALUE
BLAST_VALUE
CMAX
DMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using LAST_VALUE instead of FIRST_VALUE.
Using aggregate functions like MAX or MIN without window clause.
2fill in blank
medium

Complete the code to select the last salary value in each department.

PostgreSQL
SELECT department_id, [1](salary) OVER (PARTITION BY department_id ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary FROM employees;
Drag options to blanks, or click blank then click option'
ALAST_VALUE
BFIRST_VALUE
CMAX
DMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the frame clause, which causes LAST_VALUE to return unexpected results.
Using FIRST_VALUE instead of LAST_VALUE.
3fill in blank
hard

Fix the error in the code to correctly get the last salary per department.

PostgreSQL
SELECT department_id, LAST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary [1]) AS last_salary FROM employees;
Drag options to blanks, or click blank then click option'
AROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BRANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
CRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
DROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
Attempts:
3 left
💡 Hint
Common Mistakes
Using default frame which ends at current row.
Using RANGE frame which behaves differently.
4fill in blank
hard

Fill both blanks to select the first and last hire dates per department.

PostgreSQL
SELECT department_id, [1](hire_date) OVER (PARTITION BY department_id ORDER BY hire_date) AS first_hire, [2](hire_date) OVER (PARTITION BY department_id ORDER BY hire_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_hire FROM employees;
Drag options to blanks, or click blank then click option'
AFIRST_VALUE
BMAX
CLAST_VALUE
DMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using aggregate functions like MAX or MIN instead of window functions.
Omitting the frame clause for LAST_VALUE.
5fill in blank
hard

Fill all three blanks to create a query that shows employee name, first salary, last salary, and the difference between them per department.

PostgreSQL
SELECT employee_name, department_id, [1](salary) OVER (PARTITION BY department_id ORDER BY salary) AS first_salary, [2](salary) OVER (PARTITION BY department_id ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary, [3] - [2] AS salary_diff FROM employees;
Drag options to blanks, or click blank then click option'
AFIRST_VALUE
Blast_salary
Csalary
DLAST_VALUE
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of FIRST_VALUE and LAST_VALUE.
Using column names incorrectly in the difference calculation.