Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Third Highest Value with PySpark

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

Your question is Third Highest Value with PySpark. Start with the requirements and the three 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

CGI Group needs a compensation report for active employees in Canadian offices. Write a PostgreSQL query that returns one salary value based on the distinct eligible salaries.

Requirements

  1. Join employees to their departments and offices.
  2. Include only employees with employment_status = 'Active', a non-NULL salary, and an office in Canada.
  3. Rank distinct salaries from highest to lowest, so duplicate employee salaries count once.
  4. Return the third-highest distinct salary when at least three exist. If exactly two exist, return the second-highest. If exactly one exists, return that salary. Return NULL when no eligible salary exists.
  5. Return one row with the exact output column selected_salary.

Schema

employees
ColumnTypeDescription
employee_idPKINTEmployee identifier
employee_nameVARCHAR(100)Employee name
department_idINTReferences departments.department_id
salaryNUMERIC(12,2)Annual employee salary
employment_statusVARCHAR(20)Current employment status
departments
ColumnTypeDescription
department_idPKINTDepartment identifier
department_nameVARCHAR(100)Department name
office_idINTReferences offices.office_id
offices
ColumnTypeDescription
office_idPKINTOffice identifier
office_nameVARCHAR(100)Office name
countryVARCHAR(50)Office country
cityVARCHAR(80)Office city
Tablesemployeesdepartmentsoffices
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results