0% found this document useful (0 votes)
3 views

Lab Report 1SQL

The document outlines a lab report for a student named Chityala Saikumar in the Advanced SQL course, detailing five SQL queries related to employee and department data. Each query addresses specific requirements such as identifying employees without departments, listing employees and departments, comparing salaries to averages, and calculating total salaries using ROLLUP and CUBE functions. The report includes explanations of how each query works and the expected results.

Uploaded by

kumarraj93895
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Report 1SQL

The document outlines a lab report for a student named Chityala Saikumar in the Advanced SQL course, detailing five SQL queries related to employee and department data. Each query addresses specific requirements such as identifying employees without departments, listing employees and departments, comparing salaries to averages, and calculating total salaries using ROLLUP and CUBE functions. The report includes explanations of how each query works and the expected results.

Uploaded by

kumarraj93895
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name of the Student: CHITYALA SAIKUMAR Academic Year: 2nd year

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

Explanation: To find the total number of employees without a matching department:

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.

This query uses ROLLUP to calculate:


1. Total salary for each employee within their department.
2. Total salary for each department.
3. Overall total salary for all departments.
How it works:
1. GROUP BY DEPT_ID, EMP_ID:
o Groups data by department and employee.
2. WITH ROLLUP:
o Adds subtotals for each department and a grand total for all departments.
o NULL in DEPT_ID or EMP_ID indicates subtotals or the grand total.
3. SUM(emp_SALARY):
o Calculates the total salary for each group.
Result:
The query will return:
 Employee-wise total salaries.
 Department-wise total salaries (where EMP_ID is NULL).
 Overall total salary (where both DEPT_ID and EMP_ID are NULL).
5 Cube by Department Name and Employee
Problem:
You want to calculate the total salary for each employee by department and include a subtotal
for each department and a grand total using the department name and employee name. Make
sure to include the department names.
This query uses ROLLUP to calculate:
1. Total salary for each employee by department.
2. Subtotals for each department.
3. Grand total for all departments.
How it works:
1. JOIN:
o Combines the EMP and DEPT tables to get employee names and department
names.
2. GROUP BY D.DEPT_NAME, E.EMP_NAME:
o Groups data by department and employee.
3. WITH ROLLUP:
o Adds subtotals for each department (where EMP_NAME is NULL).
o Adds a grand total for all departments (where
both DEPT_NAME and EMP_NAME are NULL).
4. SUM(E.emp_SALARY):
o Calculates the total salary for each group.
Result:
The query will return:
 Employee-wise total salaries.
 Department-wise subtotals.
 Grand total salary for all departments.

You might also like