Finding Median Rows per Partition

Finding Median Rows per Partition

Given a table Employee with columns id (INT, PK), company (VARCHAR), and salary (INT), write a SQL query to identify the specific rows that represent the median salary for each company. Rules for median calculation: Sort salaries in ascending order for each company. In case of identical salaries, use id as a secondary ascending sort key to break ties. If a company has an odd number of employees, return the single middle row. If a company has an even number of employees, return the two middle rows. The result should include the id, company, and salary columns for these specific median records.
PostgreSQLCTEWindow Function
10
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Interview Solver

  • Coding Puzzles
  • System Design
  • Behavioral Challenges
  • ML System Design
  • SQL Puzzles
  • FE System Design
Explore Solver

Question Bank

  • Coding Interview Questions
  • System Design Interview Questions
  • Behavioral Interview Questions
  • ML System Design Questions
  • SQL & Database Questions
  • FE System Design Questions
Explore Questions

Golden Blogs

  • Coding Solutions
  • System Design Guides
  • Behavioral Guides
  • ML System Design Guides
  • SQL Solutions
  • FE System Design Guides
Explore Blogs

Intervipedia

  • Coding Concepts
  • System Design Concepts
  • Behavioral Concepts
  • ML System Concepts
  • SQL Concepts
  • FE System Concepts
Explore Concepts

Application Tools

  • Self-Intro Generator

Company

  • Pricing
  • FAQ
  • About
  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers