Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Top two salaries per department

    Top two salaries per department

    Hardsqlwindow functionsdense rankjoin

    For each department, return the employees whose salary is among the department's two highest distinct salaries: department, employee and salary. Sort by department, then salary from highest, then employee name.

    Tables

    • departments (id INTEGER, name TEXT)
    • employees (id INTEGER, name TEXT, salary INTEGER, department_id INTEGER)

    Example 1

    departments

    idname
    1Engineering
    2Sales

    employees

    idnamesalarydepartment_id
    1Mona300001
    2Omar250001
    3Laila300001
    4Ziad200001
    5Hana150002

    Expected result

    departmentemployeesalary
    EngineeringLaila30000
    EngineeringMona30000
    EngineeringOmar25000
    SalesHana15000

    Constraints

    • 0 ≤ rows ≤ 1000
    • The order of rows matters

    Submit checks your solution against 6 tests, including hidden ones.

    SQL
    -- Write one SELECT query. Tables: departments, employees
    SELECT
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.