Median Salary

HardSQLDatabaseMathSorting

Description

Write a SQL query to return the median salary of the employees table as a single value named median. For an even number of employees the median is the average of the two middle salaries. **Output columns (in order):** median

Table:employees
ColumnType
idINT
salaryINT

Examples

Input:CREATE TABLE employees (id INT, salary INT); INSERT INTO employees VALUES (1,10),(2,20),(3,30);
Output:20.0
Explanation:

Sorting the salaries and pulling the one or two central values, then averaging them, yields the median.

Input:CREATE TABLE employees (id INT, salary INT); INSERT INTO employees VALUES (1,10),(2,20),(3,30),(4,40);
Output:25.0
Explanation:

Sorting the salaries and pulling the one or two central values, then averaging them, yields the median.

Input:CREATE TABLE employees (id INT, salary INT); INSERT INTO employees VALUES (1,100);
Output:100.0
Explanation:

Sorting the salaries and pulling the one or two central values, then averaging them, yields the median.

Constraints

  • Use standard SQL (SQLite).

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.