Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL Queries

The document contains a series of SQL queries that retrieve and manipulate employee and project data from a database. Key operations include selecting employee details by department, calculating average salaries, updating salaries in the HR department, and categorizing employees by tenure. Additionally, it identifies duplicate entries in the Employee_Projects table.

Uploaded by

vanidhote123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Queries

The document contains a series of SQL queries that retrieve and manipulate employee and project data from a database. Key operations include selecting employee details by department, calculating average salaries, updating salaries in the HR department, and categorizing employees by tenure. Additionally, it identifies duplicate entries in the Employee_Projects table.

Uploaded by

vanidhote123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1: Retrieve the first_name, last_name, and department_name for all employees,

ordered by department_name.
SELECT e.first_name, e.last_name, d.department_name
FROM Employees e
JOIN Departments d ON e.department_id = d.department_id
ORDER BY d.department_name;

Q2: Find the names of all projects in the 'IT' department


SELECT p.project_name
FROM Projects p
JOIN Employee_Projects ep ON p.project_id = ep.project_id
JOIN Employees e ON ep.employee_id = e.employee_id
JOIN Departments d ON e.department_id = d.department_id
WHERE d.department_name = 'IT';

Q3: Find the average salary of employees in each department


SELECT d.department_name, AVG(e.salary) AS average_salary
FROM Employees e
JOIN Departments d ON e.department_id = d.department_id
GROUP BY d.department_name;

Q4: Find the top 3 highest-paid employees in each department.


SELECT first_name, last_name, department_name, salary
FROM (
SELECT e.first_name, e.last_name, d.department_name, e.salary,
RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS
rank1
FROM Employees e
JOIN Departments d ON e.department_id = d.department_id
) ranked
WHERE rank1 = 3;

Q5: Increase the salary of all employees in the 'HR' department by 10%
UPDATE Employees
SET salary = salary * 1.10
WHERE department_id = (SELECT department_id FROM Departments WHERE department_name
= 'HR');

Q6: Find all employees whose salary is higher than the average salary of all
employees in the company.
SELECT first_name, last_name, salary
FROM Employees
WHERE salary > (SELECT AVG(salary) FROM Employees);

Q7: Categorize employees based on their tenure (Senior, Mid-Level, Junior).


SELECT first_name, last_name, hire_date,
CASE
WHEN TIMESTAMPDIFF(YEAR, hire_date, CURDATE()) > 5 THEN 'Senior'
WHEN TIMESTAMPDIFF(YEAR, hire_date, CURDATE()) BETWEEN 2 AND 5 THEN
'Mid-Level'
ELSE 'Junior'
END AS employee_category
FROM Employees;

Q8: Identify any duplicate entries in the Employee_Projects table.


SELECT employee_id, project_id, COUNT(*)
FROM Employee_Projects
GROUP BY employee_id, project_id
HAVING COUNT(*) > 1;

You might also like