Experiment No. 7 AIM:-Perform Nested and Complex Queries OBJECTIVES:-To Understand Nested and Complex Queries in SQL Theory
Experiment No. 7 AIM:-Perform Nested and Complex Queries OBJECTIVES:-To Understand Nested and Complex Queries in SQL Theory
THEORY:-
A sub/nested query is a select query that is contained inside another query. The inner select query is usually used
to determine the results of the outer select query\
Nested Query: some nested queries are written based on below schema.
1) Find the names of sailors who have reserved boat number 103.
select s.sid, s.sname , r.bid from sailors s, reserves r where s.sid = r.sid and r.bid=103;
2) Find the sids of all sailors who have reserved red boats but not green boats.
select s.sid , s.sname, b.colour from sailors s, reserves r, boat b where s.sid=r.sid and r.bid
=b.bid and b.colour='red' and s.sid not in (select s.sid from sailors s,reserves r,boat b where
s.sid=r.sid and r.bid=b.bid and b.colour='green');
3) Find sailors whose rating is better than some sailor called Horatio.
select sid,sname,rating from sailors where rating > any (select rating from sailors where
sname='horatio');
7) Find the average age of sailors who have of voting age (i.e.~ at least 18 years old) for each rating level that
has at least two sailors.
select rating, avg(age),count(*) from sailors where age>18 group by rating having
count(*)>1;