//Create the table for employees1
CREATE TABLE employees1 (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10,2),
hire_date DATE
);
//INSERT INTO employees1(emp_id, emp_name,department, salary, hire_date) VALUES
(1,'Akshaya','HR',50000.00,'2021-01-15'),
(2,'Bharath','IT',70000.00,'2017-06-10'),
(3,'Cristiano','Finance',65000.00,'2019-03-20'),
(4,'Dev','IT',72000.00,'2020-11-05'),
(5,'Eliza','HR',48000.00,'2024-02-30'),
(6,'Fredy','Marketing',55000.00,'2021-08-25');
//Create the table for departments
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50),
location VARCHAR(50)
);
//INSERT INTO depart(dept_id, dept_name, location) VALUES
(1,'HR','Chennai'),
(2,'IT','Bangalore'),
(3,'Finance','Delhi'),
(4,'Marketing','Chennai'),
(5,'Support','Mumbai');
//Create the table for projects
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(100),
emp_id INT,
start_date DATE,
end_date DATE,
FOREIGN KEY (emp_id) REFERENCES employees1 (emp_id)
);
//INSERT INTO projects(project_id, project_name, emp_id, start_date,end_date)
VALUES
(101,'Recruitment System',1,'2023-01-10','2023-06-30'),
(102,'ERP Upgrade',2,'2022-09-01','2023-03-23'),
(103,'Budget Analysis',3,'2023-01-08','2023-04-15'),
(104,'Cloud Migration',4,'2022-03-08','2023-07-30'),
(105,'Ad Campaign',6,'2023-03-01','2023-06-01'),
(106,'Website Revamp',2,'2023-05-01','2023-08-31');
1. List all employees who work in the ‘HR’ department.
//Query
SELECT * FROM employees1 WHERE department = 'HR';
2. Get the names and salaries of employees ordered by salary descending.
//Query
SELECT name, salary FROM employees1 ORDER BY salary DESC;
3. List all departments in ‘Chennai’.
//Query
SELECT * FROM departments WHERE location = 'Chennai';
4. Show project names and the names of employees working on them.
//Query
SELECT p.project_name, e.name FROM projects p JOIN employees1 e ON p.emp_id =
e.emp_id;
5.Display total salary paid to employees in each department.
//Query
SELECT department, SUM(salary) AS total_salary FROM employees1 GROUP BY department;
6.Find the highest-paid employee in each department.
//Query
SELECT department, name, salary FROM employees1 e1
WHERE salary = (
SELECT MAX(salary)
FROM employees1 e2
WHERE e1.department = e2.department
);
7.List employees who joined after 2022.
//Query
SELECT * FROM employees1 WHERE hire_date > '2022-12-31';
8.Count the number of employees in each department.
//Query
SELECT department, COUNT(*) AS emp_count FROM employees1 GROUP BY department;
9.List departments having more than 3 employees.
//Query
SELECT department, COUNT(*) AS emp_count FROM employees1 GROUP BY department HAVING
COUNT(*) > 3;
10.Get employees who are not assigned to any project.
//Query
SELECT e.* FROM employees1 e LEFT JOIN projects p ON e.emp_id = p.emp_id WHERE
p.project_id IS NULL;
11.Find departments where the average salary is more than 50,000.
//Query
SELECT department, AVG(salary) AS avg_salary FROM employees1 GROUP BY department
HAVING AVG(salary) > 50000;
12. List employee names who are working on more than 1 project.
//Query
SELECT e.name, COUNT(*) AS project_count FROM projects p JOIN employees1 e ON
p.emp_id = e.emp_id
GROUP BY e.emp_id, e.name HAVING COUNT(*) > 1;
13.Get employees who are working on the longest duration project.
//Query
SELECT e.name, p.project_name, DATEDIFF(p.end_date, p.start_date) AS duration FROM
projects p JOIN employees1 e ON p.emp_id = e.emp_id
WHERE DATEDIFF(p.end_date, p.start_date) = (
SELECT MAX(DATEDIFF(end_date, start_date)) FROM projects
);
14.Show total, min, and max salary per department.
//Query
SELECT department, SUM(salary) AS total_salary,MIN(salary) AS
min_salary,MAX(salary) AS max_salary FROM employees1
GROUP BY department;
15.List names of employees whose salary is above average salary.
//Query
SELECT name, salary FROM employees1 WHERE salary > (SELECT AVG(salary) FROM
employees1);