Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Grouping and Ranking

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

Your question is SQL Grouping and Ranking. Start with the requirements and the one table 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

United Airlines is reviewing employee compensation data for workforce reporting. Write a PostgreSQL query to identify the second-highest distinct salary in the employee_compensation table.

Requirements

  1. Exclude rows where annual_salary is NULL.
  2. Group employees by salary, count employees at each salary, and return the second-highest distinct salary with its employee count.

The query should treat tied salaries as one salary level. For example, if multiple employees earn the highest salary, the next distinct salary is still the second-highest salary.

Schema

employee_compensation
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee's name
annual_salaryNUMERIC(12,2)Annual salary in U.S. dollars
work_groupVARCHAR(50)United Airlines organizational group
Tablesemployee_compensation
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results