0% found this document useful (0 votes)
76 views2 pages

Experiment No. 7 AIM:-Perform Nested and Complex Queries OBJECTIVES:-To Understand Nested and Complex Queries in SQL Theory

This document discusses nested and complex queries in SQL. Some examples of nested queries are provided: 1. Find sailors who reserved a specific boat number by selecting sailors that match reserves records for that boat ID. 2. Select sailors who reserved red boats but not green boats using a nested query to exclude sailors matching the green boat reserve records. 3. Find sailors with a rating higher than a specific sailor by nesting a query to select the comparison sailor's rating.

Uploaded by

kalpesh mayekar
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)
76 views2 pages

Experiment No. 7 AIM:-Perform Nested and Complex Queries OBJECTIVES:-To Understand Nested and Complex Queries in SQL Theory

This document discusses nested and complex queries in SQL. Some examples of nested queries are provided: 1. Find sailors who reserved a specific boat number by selecting sailors that match reserves records for that boat ID. 2. Select sailors who reserved red boats but not green boats using a nested query to exclude sailors matching the green boat reserve records. 3. Find sailors with a rating higher than a specific sailor by nesting a query to select the comparison sailor's rating.

Uploaded by

kalpesh mayekar
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/ 2

EXPERIMENT NO.

AIM:- Perform Nested and Complex Queries

OBJECTIVES :-To understand Nested and Complex Queries in SQL

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\

Let's look into the sub query syntax -

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');

4) Find the sailor with the highest rating.


select sid, sname, rating from sailors where rating in(select max(rating) from sailors );

5) Find the names of sailors who have reserved all boats.


select s.sname from sailors s where not exists ( select b.bid from boat b where not exists ( select
r. bid fom reserves r where r.bid = b.bid and r.sid = s.sid ))
6) Find the age of the youngest sailors who is eligible to vote (i.e., is at least 18 years old) for each rating level
with at least two such sailors.
select min(age),count(*) from sailors where age>18 group by rating having count(*)=2 or
count(*)>2;

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;

You might also like