Lab 5
Lab 5
Lab 5
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.
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;
8. Modify the query to display the minimum, maximum, sum, and average salary for each job
type.
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.
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
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:
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)