SQL Vamsi
SQL Vamsi
8.Retrieve the total salary for all employees in the Marketing department.
select sum(Salary) as totalsal
from Employee e join Department d
on e.DepartmentID=d.DepartmentID
where DepartmentName='Marketing';
11.Find the total number of employees with the same job title.
select JobTitle ,count(EmployeeID) as noofemployeeswithsametitle
from Employee
group by JobTitle;
15.Find the average number of years employees have worked in each department.
SELECT DepartmentID, AVG(DATEDIFF(YEAR, DateHired, GETDATE())) AS
average_years_worked
FROM Employee
GROUP BY DepartmentID;
17.Find the projects that employees from the Sales department are working on.
select p.ProjectName
from Employee e join Department d
on e.DepartmentID=d.DepartmentID
join Project p on e.ProjectID=p.ProjectID
where d.DepartmentName='Sales';
SELECT FirstName,LastName
FROM Employee
WHERE DepartmentID = (
SELECT DepartmentID
FROM Employee
WHERE FirstName = 'Priya' and LastName='Patel'
)
AND Firstname!='Priya' and LastName!= 'Patel';
22.Retrieve employees and the names of their projects, if they are assigned one.
SELECT e.FirstName, p.ProjectName
FROM Employee e
LEFT JOIN Project p ON e.ProjectID = p.ProjectID;
23.Find employees who have been hired for longer than their department manager.
SELECT e.FirstName, e.DateHired, m.FirstName AS manager_name, m.DateHired AS
manager_hire_date
FROM Employee e
JOIN Employee m ON e.DepartmentID = m.DepartmentID
WHERE m.JobTitle In ('SalesManager','FinanceManager','HRManager')
AND e.DateHired < m.DateHired;
24.List all departments that have at least one employee working on "Project B".
SELECT DISTINCT d.DepartmentName
FROM Employee e
JOIN Project p ON e.ProjectID = p.ProjectID
JOIN Department d ON e.DepartmentID = d.DepartmentID
WHERE p.ProjectName = 'Project B';
SELECT e.FirstName
FROM Employee e
LEFT JOIN Project p ON e.ProjectID = p.ProjectID
WHERE p.ProjectID IS NULL;