Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stock Span and SQL Salary Query

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Stock Span and SQL Salary Query. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Avalara needs a compensation report showing employees who earn the second-highest distinct salary among active employees. Write a PostgreSQL query using a window function and a join to return all employees tied at that salary.

Requirements

  1. Consider only employees where is_active is TRUE and salary is not NULL.
  2. Rank distinct salary levels from highest to lowest using an appropriate window function.
  3. Return every employee whose salary has rank 2, including ties.
  4. Include the employee ID, employee name, salary, and Avalara department name. Sort the output by employee_id.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
department_idINTReferences departments.department_id
salaryNUMERIC(12,2)Annual employee salary
is_activeBOOLEANWhether the employee is currently active
departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Avalara department name
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results