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| Column | Type |
|---|---|
| id | INT |
| salary | INT |
Examples
Input:
CREATE TABLE employees (id INT, salary INT); INSERT INTO employees VALUES (1,10),(2,20),(3,30);Output:
20.0Explanation:
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.0Explanation:
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.0Explanation:
Sorting the salaries and pulling the one or two central values, then averaging them, yields the median.
Constraints
- •
Use standard SQL (SQLite).