๐ Q7. Find employees who never received a bonus
๐ค Table: employees
id | name
1 | Arjun
2 | Riya
3 | Meena
๐ฐ Table: bonus
employee_id | bonus_amount
1 | 3000
3 | 5000
๐ Query:
SELECT e.id, e.name
FROM employees e
LEFT JOIN bonus b ON e.id = b.employee_id
WHERE b.employee_id IS NULL;
๐ Result: Riya
๐ Q8. Get highest salary employee from each department
๐งพ Table: employees
id | name | dept | salary
1 | Arjun | HR | 40000
2 | Riya | IT | 55000
3 | Meena | IT | 62000
4 | Kabir | HR | 45000
๐ Query:
SELECT e.*
FROM employees e
JOIN (
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department
) t
ON e.department = t.department
AND e.salary = t.max_salary;
๐ Result: Kabir (HR), Meena (IT)
๐ Q9. Count number of employees who joined each year
๐ Table: employees
id | name | join_date
1 | Arjun | 2021-03-10
2 | Riya | 2022-05-12
3 | Meena | 2021-11-03
4 | Kabir | 2023-01-09
๐ Query:
SELECT YEAR(join_date) AS join_year, COUNT(*) AS total
FROM employees
GROUP BY YEAR(join_date)
ORDER BY join_year;
๐ Result:
2021 โ 2
2022 โ 1
2023 โ 1
๐ Q10. Find employees earning more than department average
๐งพ Table: employees
id | name | dept | salary
1 | Arjun | HR | 40000
2 | Riya | IT | 55000
3 | Meena | IT | 62000
4 | Kabir | HR | 45000
๐ Query:
SELECT e.*
FROM employees e
JOIN (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
) t
ON e.department = t.department
WHERE e.salary > t.avg_salary;
๐ Result: Kabir (HR), Meena (IT)
๐ Q11. Fetch the 5th highest salary from employee table
๐งพ Table: employees
Salaries: 90000, 85000, 78000, 76000, 72000, 70000
๐ Query:
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 4;
๐ Result: 72000
๐ Q12. Find employees working on more than one project
๐ Table: project_assignments
employee_id | project_id
1 | 101
1 | 102
2 | 103
3 | 104
3 | 105
3 | 106
๐ Query:
SELECT employee_id, COUNT(*) AS project_count
FROM project_assignments
GROUP BY employee_id
HAVING COUNT(*) > 1;
๐ Result:
1 โ 2 projects
3 โ 3 projects
๐ฌ Tap โค๏ธ for more!