0% found this document useful (0 votes)
30 views20 pages

Dbms Ass (12.03.25)

Uploaded by

nusrat147030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views20 pages

Dbms Ass (12.03.25)

Uploaded by

nusrat147030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Jamalpur Science & Technology University

Assignment
Course Name: Database Management System Sessional
Course Code: CSE 2242

Submitted By: Submitted To:


Name : Md Jobayer Hossen Md. Anwarul Islam
ID : 22111105 LECTURER
Session : 2021-22 Department of CSE, Netrokona
Department of CSE, JSTU University

Submission Date: 12.03.2025


SQL Questions
1. Retrieve all employees along with their department names.

SQL CODE:

SELECT e.emp_id, e.emp_name, d.dept_name

FROM Employees e

JOIN Departments d ON e.dept_id = d.dept_id;

Output:

2. Find employees who have worked on projects and list their names, project names, and
hours worked.

SQL CODE:

SELECT e.emp_name, p.project_name, ep.hours_worked

FROM Employee_Projects ep

JOIN Employees e ON ep.emp_id = e.emp_id

JOIN Projects p ON ep.project_id = p.project_id;


Output:

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,

(e.salary + s.bonus) AS total_salary

FROM

Employees e

JOIN

Salaries s ON e.emp_id = s.emp_id;

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

Employee_Projects ep ON e.emp_id = ep.emp_id

LEFT JOIN

Projects p ON ep.project_id = p.project_id;

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

Employees e ON ep.emp_id = e.emp_id;

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

Employee_Projects ep ON e.emp_id = ep.emp_id

WHERE

ep.project_id IS NULL;

Output:

7. Retrieve employees whose name starts with ‘A’.

SQL CODE:

SELECT

emp_id,

emp_name

FROM

Employees

WHERE

emp_name LIKE 'A%';


Output:

8. Extract the domain name from each employee's email.

SQL CODE:

SELECT emp_name

FROM Employees

WHERE emp_name LIKE 'A%';

Output:

9. Find the total number of employees.

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:

11. Find the highest salary among employees.

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

Employees e ON d.dept_id = e.dept_id

GROUP BY

d.dept_name

ORDER BY

num_employees DESC

LIMIT 1;

Output:

13. Get the total hours worked by each employee.

SQL CODE:

SELECT e.emp_id, e.emp_name, SUM(ep.hours_worked) AS total_hours

FROM Employees e

JOIN Employee_Projects ep ON e.emp_id = ep.emp_id

GROUP BY e.emp_id, e.emp_name;


Output:

14. Find employees earning more than the average salary.

SQL CODE:

SELECT e.emp_id, e.emp_name, s.salary

FROM Employees e

JOIN Salaries s ON e.emp_id = s.emp_id

WHERE s.salary > (SELECT AVG(salary) FROM Salaries);

Output:
15. Find the employee who has worked the most hours on a project.

SQL CODE:

SELECT ep.emp_id, e.emp_name, ep.project_id, p.project_name, ep.hours_worked

FROM Employee_Projects ep

JOIN Employees e ON ep.emp_id = e.emp_id

JOIN Projects p ON ep.project_id = p.project_id

ORDER BY ep.hours_worked DESC

LIMIT 1;

Output:

16. Retrieve projects that have at least two employees working on them.

SQL CODE:

SELECT ep.project_id, p.project_name, COUNT(ep.emp_id) AS num_employees

FROM Employee_Projects ep

JOIN Projects p ON ep.project_id = p.project_id

GROUP BY ep.project_id, p.project_name

HAVING COUNT(ep.emp_id) >= 2;

Output:
17. Get the department with the highest total salary.

SQL CODE:

SELECT d.dept_id, d.dept_name, SUM(s.salary) AS total_salary

FROM Departments d

JOIN Employees e ON d.dept_id = e.dept_id

JOIN Salaries s ON e.emp_id = s.emp_id

GROUP BY d.dept_id, d.dept_name

ORDER BY total_salary DESC

LIMIT 1;

Output:

Advanced SQL Questions


1. Find employees who are working on all projects (i.e., employees assigned to every
project).

SQL Code:

SELECT e.emp_id, e.emp_name

FROM Employees e

JOIN Employee_Projects ep ON e.emp_id = ep.emp_id

GROUP BY e.emp_id, e.emp_name

HAVING COUNT(DISTINCT ep.project_id) = (SELECT COUNT(*) FROM Projects);

Output:
2. Retrieve employees who have never worked on a project in the last 6 months.

SQL Code:

SELECT e.emp_id, e.emp_name

FROM Employees e

WHERE e.emp_id NOT IN (

SELECT ep.emp_id

FROM Employee_Projects ep

JOIN Projects p ON ep.project_id = p.project_id

WHERE ep.hours_worked > 0

AND p.project_id IN (

SELECT project_id

FROM Employee_Projects

WHERE ep.emp_id = e.emp_id

AND p.start_date >= CURRENT_DATE - INTERVAL 6 MONTH

);

3. Find employees who are getting a salary higher than the average salary of their
department.

SQL Code:

SELECT e.emp_id, e.emp_name, e.salary, d.dept_name

FROM Employees e

JOIN Departments d ON e.dept_id = d.dept_id

WHERE e.salary > (

SELECT AVG(s.salary)

FROM Employees s

WHERE s.dept_id = e.dept_id

)
Output:

4. Find employees who work in multiple departments.

SQL Code:

SELECT e.emp_id, e.emp_name

FROM Employees e

GROUP BY e.emp_id, e.emp_name

HAVING COUNT(DISTINCT e.dept_id) > 1;

5. Get the name of employees who have the second-highest salary.

SQL Code:

SELECT e.emp_id, e.emp_name, e.salary

FROM Employees e

WHERE e.salary = (

SELECT MAX(salary)

FROM Employees

WHERE 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,

RANK() OVER (PARTITION BY e.dept_id ORDER BY e.salary DESC) AS salary_rank

FROM Employees e

JOIN Departments d ON e.dept_id = d.dept_id

ORDER BY e.dept_id, salary_rank;

Output:

7. Find employees who have the highest salary in their department.

SQL Code:

SELECT e.emp_id, e.emp_name, e.salary, d.dept_name

FROM Employees e

JOIN Departments d ON e.dept_id = d.dept_id

WHERE e.salary = (

SELECT MAX(salary)

FROM Employees

WHERE dept_id = e.dept_id

ORDER BY e.dept_id, e.salary DESC;


Output:

8. Calculate the cumulative salary paid to employees in order of hire date.

SQL Code:

SELECT e.emp_id, e.emp_name, e.hire_date, e.salary,

SUM(e.salary) OVER (ORDER BY e.hire_date) AS


cumulative_salary

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:

SELECT e.emp_id, e.emp_name, e.hire_date, e.salary,

LAG(e.salary) OVER (ORDER BY e.hire_date) AS previous_salary

FROM Employees e

ORDER BY e.hire_date;

Output:

10. Find the percentage of total company salary that each employee earns.

SQL Code:

SELECT e.emp_id, e.emp_name, e.salary,

(e.salary / total_salary.total) * 100 AS salary_percentage

FROM Employees e

JOIN (SELECT SUM(salary) AS total FROM Employees) total_salary

ORDER BY e.salary DESC;

Output:
11. Find all employees under a given manager (assuming a self-referential table for
hierarchy).

SQL Code:

CREATE TABLE Employees (

emp_id INT PRIMARY KEY,

emp_name VARCHAR(100),

dept_id INT,

hire_date DATE,

email VARCHAR(100),

salary DECIMAL(10,2),

manager_id INT, -- this field references emp_id of the manager

CONSTRAINT fk_manager FOREIGN KEY (manager_id) REFERENCES


Employees(emp_id)

);

12. Find the total salary of an employee and all their subordinates.

WITH RECURSIVE EmployeeHierarchy AS (

SELECT e.emp_id, e.emp_name, e.salary, e.manager_id

FROM Employees e

WHERE e.emp_id = <given_emp_id> -- Replace <given_emp_id> with the manager's ID

UNION ALL

SELECT e.emp_id, e.emp_name, e.salary, e.manager_id

FROM Employees e

JOIN EmployeeHierarchy eh ON e.manager_id = eh.emp_id

SELECT SUM(eh.salary) AS total_salary

FROM EmployeeHierarchy eh;


13. Extract first name and last name from emp_name.

SQL Code:

SELECT

SUBSTRING_INDEX(e.emp_name, ' ', 1) AS first_name, -- Extract first name

SUBSTRING_INDEX(e.emp_name, ' ', -1) AS last_name -- Extract last name

FROM Employees e;

Output:

15. Find employees whose email contains their name.

SQL Code:

SELECT e.emp_id, e.emp_name, e.email

FROM Employees e

WHERE LOWER(e.email) LIKE LOWER(CONCAT('%',


REPLACE(e.emp_name, ' ', ''), '%'));

16. Find employees who were hired on a Monday.

SQL Code:

SELECT e.emp_id, e.emp_name, e.hire_date

FROM Employees e

WHERE DAYOFWEEK(e.hire_date) = 2; -- 2 represents Monday


Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy