Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Third Highest Value with PySpark
00:00
5 left

Third Highest Value with PySpark

MediumSQL · PostgreSQL

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
Interviewer

Your question is Third Highest Value with PySpark. Start with the requirements and the three tables 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.