Lab Report 1SQL
Lab Report 1SQL
Student Registration Number: 232P4R254 Year & Term: 2nd year & 1st Term
Study Level: PG Class & Section: MCA DS B
Name of the Course: Advance SQL Name of the Instructor: Mr. Mohan
Rao
Name of the Assessment: Lab Report 1 Date of Submission: 25/01/2025
Advance SQL
1.What are the names of employees who do not belong to a department listed in the
Departments table
1. Use a LEFT JOIN to include all employees, even those without a matching
department.
2. Check for NULL in the dept.dept_id column to identify employees without a
department.
3. Use COUNT to get the total number of these employees.
How it works:
LEFT JOIN: Includes all rows from the emp table, even if there's no match in the
dept table.
WHERE dept.dept_id IS NULL: Filters only those employees who don't have a
matching department.
COUNT: Counts the total number of such employees.
Result: The query will return the total number of employees who do not belong to any
department listed in the Departments table.
2. Get a list of employees and their departments, including those with no department
assigned. Also, list departments that have no employees.
This query retrieves a list of employees and their departments, including:
1. Employees with no department assigned.
2. Departments with no employees.
How it works:
1. First Part (LEFT JOIN):
o Lists all employees and their departments.
o If an employee has no department, it shows "No
Department" using COALESCE.
2. Second Part (RIGHT JOIN):
o Lists all departments and their employees.
o If a department has no employees, it shows "No
Employee" using COALESCE.
3. UNION:
o Combines the results of both queries into one list.
Result:
The query will return:
Employees with their departments.
Employees without departments (marked as "No Department").
Departments without employees (marked as "No Employee").
3. List all employees along with their department name and their salary compared to
the average salary of their department.
This query lists employees, their department name, salary, and compares their salary to
the average salary of their department.
How it works:
1. First Query:
o Calculates the average salary for each department
using AVG(emp_salary) and groups by dept_id.
2. Second Query:
o Joins the emp and dept tables to get employee details and department names.
o Joins the result with the average salary calculated in the first query.
o Uses a CASE statement to compare each employee’s salary with the
department’s average salary:
Above Average: If the employee’s salary is higher.
Below Average: If the employee’s salary is lower.
Average: If the salary matches the average.
Result:
The query will return:
Employee ID, department name, salary, average salary of the department, and a
comparison (Above Average, Below Average, or Average).
4. Rollup with Department Names
Problem:
You need to get the total salary for each employee within their department and also the total
salary for each department, and an overall total salary.