Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Second Largest Value

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

Your question is SQL for Second Largest Value. 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

Teradata uses employee compensation data to support workforce analysis. Write a PostgreSQL query to identify employees earning the second highest distinct salary among currently active employees.

Requirements

  1. Consider only rows where employment_status = 'Active'.
  2. Rank distinct salary values from highest to lowest, handling duplicate salaries correctly.
  3. Return every active employee whose salary has rank 2, including employee_id, employee_name, and salary.
  4. Exclude employees with a NULL salary from consideration and order the results by employee_id.

Schema

employees
ColumnTypeDescription
employee_idPKINTEGERUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
salaryNUMERIC(12,2)Annual employee salary
employment_statusVARCHAR(20)Current employment status
Tablesemployees
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results