Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Second Largest Value
00:00
5 left

SQL for Second Largest Value

MediumSQL · PostgreSQL

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
Interviewer

Your question is SQL for Second Largest Value. Start with the requirements and the one table in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.