Dbms Ass (12.03.25)
Dbms Ass (12.03.25)
Assignment
Course Name: Database Management System Sessional
Course Code: CSE 2242
SQL CODE:
FROM Employees e
Output:
2. Find employees who have worked on projects and list their names, project names, and
hours worked.
SQL CODE:
FROM Employee_Projects ep
3. Get the total salary (salary + bonus) of each employee by joining the Employees and
Salaries tables.
SQL CODE:
SELECT
e.emp_id,
e.emp_name,
FROM
Employees e
JOIN
Output:
4. List all employees and their projects, including those who are not assigned to any
project.
SQL CODE:
SELECT
e.emp_id,
e.emp_name,
p.project_name
FROM
Employees e
LEFT JOIN
LEFT JOIN
Output:
5. Retrieve all projects along with the employees assigned to them, including projects
with no employees assigned.
SQL CODE:
SELECT
p.project_id,
p.project_name,
e.emp_id,
e.emp_name
FROM
Projects p
LEFT JOIN
Employee_Projects ep ON p.project_id =
ep.project_id
LEFT JOIN
Output:
6. Find employees who have not been assigned any project.
SQL CODE:
SELECT
e.emp_id,
e.emp_name
FROM
Employees e
LEFT JOIN
WHERE
ep.project_id IS NULL;
Output:
SQL CODE:
SELECT
emp_id,
emp_name
FROM
Employees
WHERE
SQL CODE:
SELECT emp_name
FROM Employees
Output:
SQL CODE:
SELECT
COUNT(*) AS total_employees
FROM
Employees;
Output:
10. Calculate the average salary of employees.
SQL CODE:
SELECT
AVG(salary) AS average_salary
FROM
Employees;
Output:
SQL CODE:
SELECT
MAX(salary) AS highest_salary
FROM
Employees;
Output:
12. Find the department with the highest number of employees.
SQL CODE:
SELECT
d.dept_name,
COUNT(e.emp_id) AS num_employees
FROM
Departments d
JOIN
GROUP BY
d.dept_name
ORDER BY
num_employees DESC
LIMIT 1;
Output:
SQL CODE:
FROM Employees e
SQL CODE:
FROM Employees e
Output:
15. Find the employee who has worked the most hours on a project.
SQL CODE:
FROM Employee_Projects ep
LIMIT 1;
Output:
16. Retrieve projects that have at least two employees working on them.
SQL CODE:
FROM Employee_Projects ep
Output:
17. Get the department with the highest total salary.
SQL CODE:
FROM Departments d
LIMIT 1;
Output:
SQL Code:
FROM Employees e
Output:
2. Retrieve employees who have never worked on a project in the last 6 months.
SQL Code:
FROM Employees e
SELECT ep.emp_id
FROM Employee_Projects ep
AND p.project_id IN (
SELECT project_id
FROM Employee_Projects
);
3. Find employees who are getting a salary higher than the average salary of their
department.
SQL Code:
FROM Employees e
SELECT AVG(s.salary)
FROM Employees s
)
Output:
SQL Code:
FROM Employees e
SQL Code:
FROM Employees e
WHERE e.salary = (
SELECT MAX(salary)
FROM Employees
);
Output:
6. Rank employees by salary within their department.
SQL Code:
SELECT e.emp_id, e.emp_name, e.salary, d.dept_name,
FROM Employees e
Output:
SQL Code:
FROM Employees e
WHERE e.salary = (
SELECT MAX(salary)
FROM Employees
SQL Code:
FROM Employees e
ORDER BY e.hire_date;
Output:
9. Get the previous employee’s salary for each employee (ordered by hire date).
SQL Code:
FROM Employees e
ORDER BY e.hire_date;
Output:
10. Find the percentage of total company salary that each employee earns.
SQL Code:
FROM Employees e
Output:
11. Find all employees under a given manager (assuming a self-referential table for
hierarchy).
SQL Code:
emp_name VARCHAR(100),
dept_id INT,
hire_date DATE,
email VARCHAR(100),
salary DECIMAL(10,2),
);
12. Find the total salary of an employee and all their subordinates.
FROM Employees e
UNION ALL
FROM Employees e
SQL Code:
SELECT
FROM Employees e;
Output:
SQL Code:
FROM Employees e
SQL Code:
FROM Employees e