Find Interns with Above Average Salary
UptoSkills Arena needs to identify high-potential interns for potential full-time offers. From the internship records, find all interns whose monthly salary is greater than the average monthly salary of all interns.
- interns (intern_id integer PRIMARY KEY, name varchar(100) NOT NULL, department_id integer, start_date date, end_date date, monthly_salary numeric(10,2))
- departments (department_id integer PRIMARY KEY, dept_name varchar(100) NOT NULL)
Return the intern_id, name, and monthly_salary of these interns, ordered by intern_id. Handle ties by ordering by intern_id. NULL salaries should be ignored in average calculation and should not appear in the output.
Tables
CREATE TABLE interns (
intern_id integer PRIMARY KEY,
name varchar(100) NOT NULL,
department_id integer,
start_date date,
end_date date,
monthly_salary numeric(10,2)
);
CREATE TABLE departments (
department_id integer PRIMARY KEY,
dept_name varchar(100) NOT NULL
);Example data
INSERT INTO departments (department_id, dept_name) VALUES (1, 'Engineering'), (2, 'Marketing'), (3, 'Sales'); INSERT INTO interns (intern_id, name, department_id, start_date, end_date, monthly_salary) VALUES (101, 'Alice', 1, '2023-06-01', '2023-08-31', 3000.00), (102, 'Bob', 1, '2023-06-01', '2023-08-31', 3500.00), (103, 'Charlie', 2, '2023-07-01', '2023-09-30', 2800.00), (104, 'David', 3, '2023-07-01', '2023-09-30', 4000.00), (105, 'Eve', 2, '2023-06-15', '2023-08-15', 3200.00), (106, 'Frank', 1, '2023-06-01', '2023-08-31', 3500.00); -- Tie with Bob
Your query is also checked on hidden datasets with different data.
Hints
Similar problems
- Find Departments with Above Average Salary SpendingMedium
- Students with Above Average GPA in Their DepartmentMedium
- Find Students with Above-Average GPA in Their DepartmentMedium
- Internship Performance by DepartmentMedium
- Average Score per Student GroupMedium
- Intern Stipend by Department and Start DateMedium
More in: SQL and Databases problems
-- Select interns earning above average salary
SELECT
Your query runs on the example tables shown with the problem, then on hidden datasets when you submit.
Write standard SQL. MySQL habits work too: IFNULL, IF, YEAR(), DATE_FORMAT, GROUP_CONCAT, `backticks` and LIMIT 5, 10. It runs on PostgreSQL, so integer division keeps whole numbers (5 / 2 = 2): write 5.0 / 2 for 2.5.