Internship Offers by Department and Salary
UptoSkills Arena is tracking internship offers from various companies to students across different departments. The HR department needs a report summarizing the total number of offers and the average salary offered by each department.
Tables:
Departments(dept_id INT PRIMARY KEY, dept_name VARCHAR(50))Companies(comp_id INT PRIMARY KEY, comp_name VARCHAR(100))Internships(intern_id INT PRIMARY KEY, comp_id INT, dept_id INT, offered_salary NUMERIC(10, 2), offer_date DATE)
Return the department name, the total count of internship offers, and the average offered salary for each department. The average salary should be rounded to two decimal places.
Handle ties by including all tied departments. NULL salaries should be ignored in the average calculation. If a department has no offers, it should not appear in the result.
Tables
CREATE TABLE Departments (
dept_id INTEGER PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);
CREATE TABLE Companies (
comp_id INTEGER PRIMARY KEY,
comp_name VARCHAR(100) NOT NULL
);
CREATE TABLE Internships (
intern_id INTEGER PRIMARY KEY,
comp_id INTEGER,
dept_id INTEGER,
offered_salary NUMERIC(10, 2),
offer_date DATE,
FOREIGN KEY (comp_id) REFERENCES Companies(comp_id),
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);Example data
INSERT INTO Departments (dept_id, dept_name) VALUES (1, 'Computer Science'), (2, 'Electrical Engineering'), (3, 'Mechanical Engineering'), (4, 'Civil Engineering'); INSERT INTO Companies (comp_id, comp_name) VALUES (101, 'Tech Innovations Inc.'), (102, 'Power Solutions Ltd.'), (103, 'AutoMakers Corp.'); INSERT INTO Internships (intern_id, comp_id, dept_id, offered_salary, offer_date) VALUES (1, 101, 1, 50000.00, '2024-05-15'), (2, 101, 1, 55000.00, '2024-05-20'), (3, 102, 2, 60000.00, '2024-06-01'), (4, 103, 3, 48000.00, '2024-06-10'), (5, 101, 1, NULL, '2024-05-25'), (6, 102, 2, 62000.00, '2024-06-05'), (7, 103, 3, 52000.00, '2024-06-15'), (8, 101, 4, 45000.00, '2024-07-01');
Your query is also checked on hidden datasets with different data.
Hints
Similar problems
- Internship Offers by Company and DepartmentMedium
- Intern Stipend by Department and Start DateMedium
- Internship Performance by DepartmentMedium
- Average Score per Student GroupMedium
- Students with Above Average GPA in Their DepartmentMedium
- Find Students with Above-Average GPA in Their DepartmentMedium
More in: SQL and Databases problems
-- Calculate internship offers and average salary by department
SELECT
d.dept_name
FROM
Departments d
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.