Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second and Third Max in SQL

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

Your question is Second and Third Max in SQL. 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

Cognizant's project finance team maintains billing rates for delivery projects. Write a PostgreSQL query to identify the second-highest and third-highest distinct billable rates in the active project rate-card table.

Requirements

  1. Ignore rows where billable_rate is NULL.
  2. Treat duplicate rates as one value, so ranking must be based on distinct rates.
  3. Return the rank position and corresponding billable rate only for ranks 2 and 3.
  4. Sort the result by rank position in ascending order.

Schema

project_rate_cards
ColumnTypeDescription
rate_card_idPKINTUnique rate-card record identifier
project_nameVARCHAR(100)Cognizant project name
billable_rateDECIMAL(10,2)Hourly billable rate
effective_dateDATEDate the rate became effective
Tablesproject_rate_cards
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results