Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Department of CSE (AI/AI&ML)

Lab Practice sheet -5


Task 1: (Aggregate & Group by clause)

1. Write a query in SQL to display the full name (first and last name) of manager who is
supervising 4 or more employees.
SELECT first_name||' '||last_name as manager_name
FROM employees
WHERE employee_id IN (
SELECT manager_id
FROM employees
GROUP BY manager_id
HAVING COUNT(*) >= 4);

2. Write a subquery that returns a set of rows to find all departments that do actually have one
or more employees assigned to them.

select department_id from employees group by department_id having count(*)>=1;


Department of CSE (AI/AI&ML)

3. Display average salary of employees in each department who have commission percentage.
select AVG(salary) from employees where commission_pct is not null group by
department_id;

4. Display job ID, number of employees, sum of salary, and difference between highest salary
and lowest salary of the employees of the job.
select job_id, count(*), sum(salary), max(salary)-min(salary) from employees group by
job_id;
Department of CSE (AI/AI&ML)

5. Display job ID for jobs with average salary more than 10000.
select job_id from employees group by job_id having AVG(salary)>10000;

6. Display department ID, year, and Number of employees joined.


select department_id , hire_date, count(*) from employees group by department_id ,
hire_date;

7. Display departments where any manager is managing more than 5 employees.


select distinct department_id from employees where manager_id in(select manager_id
from employees group by manager_id having count(employee_id)>5);
Department of CSE (AI/AI&ML)

8. Modify the query to display the minimum, maximum, sum, and average salary for each job
type.

select min(salary), max(salary),sum(salary),avg(salary) from employees group by job_id;

9. Display the manager number and the salary of the lowest paid employee for that manager.
Exclude anyone whose manager is not known. Exclude any groups where the minimum
salary is $6,000 or less. Sort the output in descending order of salary.

select manager_id , min(salary) from employees where manager_id is not NULL group by
manager_id having min(salary)>6000 order by min(salary) desc;
Department of CSE (AI/AI&ML)

10. Write a query to display each department’s name, location, number of employees, and the
average salary for all employees in that department. Label the columns Name, Location,
Number of People, and Salary, respectively.
SELECT d.department_name AS Name, d.location_id AS Location,
COUNT(e.employee_id) AS Number_of_People, AVG(e.salary) AS Salary
FROM departments d
JOIN employees e ON d.department_id = e.department_id
GROUP BY d.department_name, d.location_id;

11. Write a query to display the last name, job, department number, and department name for
all employees who work in Toronto.

SELECT e.last_name, e.job_id, e.department_id, d.department_name


FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
WHERE l.city = 'Toronto';
Department of CSE (AI/AI&ML)

12. Show the structure of the JOB_GRADES table. Create a query that displays the name,
job, department name, salary, and grade for all employees.
Task 2: (Set operations- Practice SQL queries)
Objective: To implement queries using Union, Intersect, Minus

Depositor: customer_id account_number


C001 A001
C002 A002
C003 A010
C004 A033
C005 A012

Borrower:

customer_id loan_number
C001 L001
C003 L002
C005 L010
C006 L033
customer_id customer_name customer_city
Customer: C001 Korth NOIDA
C002 Sudarshan Ghaziabad
C003 Navathe NOIDA
C004 Leon Lucknow
C005 Sudarshan Ghaziabad
C006 C.J.Date Greater NOIDA

Account:
account_number branch_name balance
A001 NOIDA MAIN 20000
A002 NOIDA MAIN 30000
A010 MOHAN NAGAR 15000
A012 GREATER NOIDA 40000
A033 TILAK NAGAR 50000

Branch:

branch_name branch_city assets


NOIDA MAIN NOIDA 200000
Department of CSE (AI/AI&ML)

MOHAN NAGAR Ghaziabad 500000


GREATER NOIDA NOIDA 700000
TILAK NAGAR Lucknow 400000

SQL Queries
Use Union, Intersect, Minus

1. Find the Customer Ids of those customers who are having a loan or an account or both. (Union)
SELECT customer_id FROM Depositor
UNION
SELECT customer_id FROM Borrower;
2. Find the Customer Ids of those customers who are having a loan and account both. (Intersect)
SELECT customer_id FROM Depositor
INTERSECT
SELECT customer_id FROM Borrower;
3. Find the Customer Ids of those customers who are having loan but not an account. (Minus)
SELECT customer_id FROM Borrower
MINUS
SELECT customer_id FROM Depositor;
4. Find the Customer Ids of those customers who are having account but not loan. (Minus)
SELECT customer_id FROM Depositor
MINUS
SELECT customer_id FROM Borrower;
Department of CSE (AI/AI&ML)

You might also like