Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views7 pages

Exp8 DBMS

The document outlines Experiment No. 8, which focuses on studying and implementing nested queries in a Database Management System (DBMS). It explains the theory, syntax, uses, advantages, and disadvantages of nested queries, along with numerous SQL examples demonstrating their application. The conclusion emphasizes the effectiveness of nested queries in simplifying complex data retrieval tasks.

Uploaded by

Soni Memon
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)
4 views7 pages

Exp8 DBMS

The document outlines Experiment No. 8, which focuses on studying and implementing nested queries in a Database Management System (DBMS). It explains the theory, syntax, uses, advantages, and disadvantages of nested queries, along with numerous SQL examples demonstrating their application. The conclusion emphasizes the effectiveness of nested queries in simplifying complex data retrieval tasks.

Uploaded by

Soni Memon
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/ 7

Date:10.03.

25
Roll no: A4-460

EXPERIMENT NO.8

AIM: To study and implement nested or complex queries in DBMS.


Theory:- Nested
Query
A nested query is also known as a complex query or subquery. In this, one query is written within
another query. The output of the inner query gets executed first, and the result is passed to the outer
query.

Syntax :
SELECT columnname(s) FROM tablename
WHERE columnname operator (SELECT cn FROM tn WHERE conditions);

Uses of Nested Queries


● To break down complex queries into smaller ones.
● To fetch data based on aggregated results like MAX(), MIN(), AVG(), etc.
● To filter records dynamically using another table.
● Used in data validation and security by limiting access to certain data.

Advantages of Nested Queries


1. Simplifies complex queries by breaking them into multiple steps.
2. Reduces redundancy by using subqueries instead of multiple joins.
3. Improves readability as queries are structured logically.
4. Can be used within Insert, Update and Delete queries.

Disadvantages of Nested Queries


1. Performance issues – Inner queries execute first, which may slow down execution, especially
for large datasets.
2. Hard to debug – Nested queries can become complex and difficult to troubleshoot.
3. Not always optimized – Some databases perform better using Joins instead of subqueries.
4. Limited use in some databases – Some SQL databases have restrictions on nested queries.

emp Table
eid name sal

1 ajay 5000

2 vijay 7000

3 sanjay 9000

4 jayesh 10000

5 viraj 2000

product Table

pid name eid

11 Shirt 1

12 Tshirt 2

13 Top 2

14 Jeans 3

Nested Queries
1. Find the Employee with the Highest Salary
SELECT name, sal FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);

2. Find Employees Earning More than the Average Salary


SELECT name, sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp);

3. Find Products Sold by Employees with a Salary Greater than 5000


SELECT name FROM product WHERE eid IN (SELECT eid FROM emp WHERE sal > 5000);

4. Find Employees Who Have the Same Salary as Another Employee


SELECT name FROM emp WHERE sal IN (SELECT sal FROM emp GROUP BY sal HAVING
COUNT(*) > 1);

5. Find Employees Who Have Sold a ‘Shirt’


SELECT name FROM emp WHERE eid IN (SELECT eid FROM product WHERE name = 'Shirt');

6. Find Employees Whose Salary is Higher than Vijay's Salary


SELECT name, sal FROM emp WHERE sal > (SELECT sal FROM emp WHERE name = 'vijay');

7. Find Employees Who Haven’t Sold Any Product


SELECT name FROM emp WHERE eid NOT IN (SELECT DISTINCT eid FROM product);

8. Find the Employee with the Second Highest Salary


SELECT name, sal FROM emp
WHERE sal = (SELECT MAX(sal) FROM emp WHERE sal < (SELECT MAX(sal) FROM emp));

9. Find Employees Working in Departments Where Salary is Above 8000


SELECT name FROM emp WHERE eid IN (SELECT eid FROM emp WHERE sal > 8000);

10. Find All Products Sold by ‘Ajay’


SELECT name FROM product WHERE eid = (SELECT eid FROM emp WHERE name = 'ajay');

11. Find Employees Who Sold More Than One Product


SELECT name FROM emp
WHERE eid IN (SELECT eid FROM product GROUP BY eid HAVING COUNT(*) > 1);

12. Find Employees Who Earn Less than the Minimum Salary in Another Table
SELECT name FROM emp
WHERE sal < (SELECT MIN(sal) FROM emp WHERE sal > 1000);

13. Find Products Sold by Employees Earning Above the Average Salary
SELECT name FROM product
WHERE eid IN (SELECT eid FROM emp WHERE sal > (SELECT AVG(sal) FROM emp));

14. Find Employees with a Salary in the Top 3 Salaries


SELECT name, sal FROM emp
WHERE sal IN (SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 3);

15. Find Employees Who Have Not Sold ‘Jeans’


SELECT name FROM emp
WHERE eid NOT IN (SELECT eid FROM product WHERE name = 'Jeans');

16. Find the Third Highest Salary


SELECT name, sal FROM emp
WHERE sal = (SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 1 OFFSET 2);

17. Find Products Not Sold by Any Employee


SELECT name FROM product
WHERE eid IS NULL OR eid NOT IN (SELECT eid FROM emp);

18. Find Employees Who Have Sold at Least One Product But Do Not Earn the Highest
Salary
SELECT name FROM emp
WHERE eid IN (SELECT DISTINCT eid FROM product) AND sal < (SELECT MAX(sal) FROM
emp);

19. Find the Name of the Employee Who Sold the Most Products
SELECT name FROM emp
WHERE eid = (SELECT eid FROM product GROUP BY eid ORDER BY COUNT(*) DESC
LIMIT 1);

20.Find the Most Expensive Product Sold by an Employee


SELECT name FROM product
WHERE pid = (SELECT pid FROM product ORDER BY pid DESC LIMIT 1);
rgit36@RGIT36:~$ sudo -i
[sudo] password for rgit36:
root@RGIT36:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35-0ubuntu0.23.04.1 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| A1410 |
| A1432 |
| A20 |
| A2432 |
| A4460 |
| A474 |
| A531 |
| A533 |
| A543 |
| A557 |
| A564 |
| Company |
| a564 |
| affan |
| college |
| company |
| customer |
| dk |
| dwm |
| em1 |
| exam |
| exp5 |
| exp6 |
| faiza |
| information_schema |
| lib |
| mysql |
| performance_schema |
| pr |
| sample |
| sid0938 |
| sys |
| uni |
+--------------------+
33 rows in set (0.00 sec)
mysql> use A4460;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from player where Team_id = (select Team_id from Team where team_name =
'pune');
+-----------+-------------+----------+---------+
| player_id | player_name | position | Team_id |
+-----------+-------------+----------+---------+
| 3 | TEJAL | midfield | 12 |
+-----------+-------------+----------+---------+
1 row in set (0.01 sec)

mysql> select Team_id,team_name from Team where Team_id in (select Team_id from player
group by Team_id having count(player_id)>1);
Empty set (0.00 sec)

mysql> select * from player where Team_id =(select Team_id from Team where team_name =
'hyderabad');
Empty set (0.00 sec)

mysql> select * from Team where Team_id in (select distinct Team_id from player);
+---------+-----------+-----------+
| Team_id | player_id | team_name |
+---------+-----------+-----------+
| 11 | 1 | MI |
| 12 | 2 | Pune |
| 13 | 2 | hyderbad |
| 13 | NULL | hyderbad |
+---------+-----------+-----------+
4 rows in set (0.01 sec)

mysql> select team_name from Team where Team_id = (select Team_id from player where
player_name ='saloni');
+-----------+
| team_name |
+-----------+
| hyderbad |
| hyderbad |
+-----------+
2 rows in set (0.00 sec)

mysql> select * from player where Team_id in (select Team_id from Team where team_name like
'%Indians%');
Empty set (0.00 sec)

mysql> select * from player where Team_id in (select Team_id from player group by Team_id
having count(*) >= 2);
Empty set (0.00 sec)

mysql> select team_name from Team where Team_id = (select Team_id from player where
player_name = 'nandini');
+-----------+
| team_name |
+-----------+
| MI |
+-----------+
1 row in set (0.00 sec)

mysql> select * from player where Team where Team_id not in (select distinct Team_id from
player);
mysql> +-----------+-------------+----------+---------+
-> | player_id | player_name | position | Team_id |
-> +-----------+-------------+----------+---------+
-> | 3 | TEJAL | midfield | 12 |
-> +-----------+-------------+----------+---------+
-> 1 row in set (0.01 sec)

mysql> SELECT team_name FROM Team WHERE Team_id = (


SELECT Team_id FROM Player WHERE player_name = 'Saloni'
);
+-----------+
| team_name |
+-----------+
| Hyderabad |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM Player WHERE Team_id IN (


SELECT Team_id FROM Team WHERE team_name LIKE '%Indians%'
);
+-----------+-------------+------------+---------+
| player_id | player_name | position | Team_id |
+-----------+-------------+------------+---------+
| 4 | Rahul | Forward | 14 |
| 5 | Amit | Goalkeeper | 14 |
+-----------+-------------+------------+---------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM Team WHERE Team_id IN (


SELECT DISTINCT Team_id FROM Player
);
+---------+-----------+
| Team_id | team_name |
+---------+-----------+
| 11 | MI |
| 12 | Pune |
| 13 | Hyderabad |
| 14 | Indians |
| 15 | Chennai |
+---------+-----------+
5 rows in set (0.00 sec)

mysql> SELECT team_name FROM Team WHERE Team_id IN (


SELECT Team_id FROM Player WHERE position = 'Forward'
);
+-----------+
| team_name |
+-----------+
| MI |
| Indians |
+-----------+
2 rows in set (0.00 sec)

mysql> SELECT player_name FROM Player WHERE Team_id IN (


SELECT Team_id FROM Player WHERE position = 'Striker'
);
+-------------+
| player_name |
+-------------+
| Nandini |
| Tejal |
| Saloni |
| Rahul |
| Amit |
| Virat |
+-------------+

6 rows in set (0.00 sec)

mysql> SELECT * FROM Team WHERE Team_id IN (


SELECT DISTINCT Team_id FROM Player WHERE Team_id NOT IN (
SELECT Team_id FROM Player WHERE position = 'Goalkeeper'
)
);
+---------+-----------+
| Team_id | team_name |
+---------+-----------+
| 11 | MI |
| 12 | Pune |
| 13 | Hyderabad |
| 15 | Chennai |
+---------+-----------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM Player WHERE Team_id IN (


SELECT Team_id FROM Team WHERE team_name LIKE 'H%'
);
+-----------+-------------+----------+---------+
| player_id | player_name | position | Team_id |
+-----------+-------------+----------+---------+
| 3 | Saloni | Defender | 13 |
+-----------+-------------+----------+---------+
1 row in set (0.00 sec)

Conclusion
Nested queries (subqueries) are a powerful feature in SQL that allow one query to be embedded
inside another query. They help in fetching complex data efficiently by breaking down problems
into smaller parts.

You might also like