sql practicef
sql practicef
Sailors Table
create table sailors(sid number(5) primary key,
sname varchar(20) NOTNULL,
rating number(5) CHECK Rating≥1 and Rating≤10,
age number(4,2));
3
Reserves Table
create table reserves( sid number(5), bid number(5) , day date,
foreign key(sid) references sailors(sid),
foreign key(bid) references boats(bid) )
5
Find the names and ages of all sailors
Select sname,age
from sailors;
SNAME AGE
Dustin 45
Brutus 33
Lubber 55.5
Andy 25.5
Rusty 35
Horatio 35
Bob 63.5
Art 25.5
Zorba 16
Horatio 35 6
Find the details of sailors whose rating is more than 7
7
Find the details of sailors whose rating is more than 7
Select *
from sailors
where rating>7;
8
Find the distinct names of Sailors whose rating is better
than 7
9
Find the distinct names of Sailors whose rating is better
than 7
10
Find the average age of sailors
11
Find the average age of sailors
AVG(AGE)
36.9
select avg(age) from sailors
12
Find the average age of sailors
AVG(AGE)
36.9
select avg(age) from sailors
13
Find the average age of sailors
AVG(AGE)
36.9
select avg(age) from sailors
select sum(age)
SUM(AGE)
from sailors
where rating=10; 51
14
Find the average age of sailors
AVG(AGE)
36.9
select avg(age) from sailors
select sum(age)
SUM(AGE)
from sailors
where rating=10; 51
15
Find the average age of sailors
AVG(AGE)
36.9
select avg(age) from sailors
select sum(age)
SUM(AGE)
from sailors
where rating=10; 51
16
Find the name and age of oldest sailor
17
Find the name and age of oldest sailor
select max(s2.age)
from sailors s2);
18
SID SNAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5 63.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
95 Bob 3 63.5
85 Art 3 25.5
71 Zorba 10 16
74 Horatio 9 35
19
Find the name and age of oldest sailor
20
Find the name and age of oldest sailor
21
Find the name and age of oldest sailor
from sailors s;
22
Find the sids of sailors who reserved red color boat
23
SID BID DAY
22 101 10/10/98 BID BNAME COLOR
74 103 9/8/98 104 Marine Red
64 102 9/8/98 103 Clipper Green
64 101 9/5/98
102 Interlake Red
31 104 11/12/98
31 103 11/6/98 101 Interlake Blue
31 102 11/10/98
22 104 10/7/98
22 103 10/8/98
22 102 10/10/98
24
Find the sids of sailors who reserved red color boat
select r.sid
from boats b, reserves r
where b.color='red' and b.bid=r.bid;
25
Find the sids of sailors who reserved red color boat
select r.sid
from boats b, reserves r
where b.color='red' and b.bid=r.bid;
26
Find the sids of sailors who reserved red color boat
select r.sid
from boats b, reserves r
where b.color='red' and b.bid=r.bid;
27
Find the details of sailors who reserved at least one boat
28
Find the details of sailors who reserved at least one boat
29
Find the details of sailors who reserved at least one boat
30
Find the details of sailors who reserved at least one boat
select s.sname
from sailors s, boats b, reserves r
where (b.color='red' and b.bid=r.bid and r.sid=s.sid);
31
Find the sids of sailors who reserved red or green color boats
32
Find the sids of sailors who reserved red or green color boats
select r.sid
from boats b, reserves r
where (b.color='red' or b.color='green') and b.bid=r.bid;
33
Find the sids of sailors who reserved red or green color boats
select r.sid
from boats b, reserves r
where (b.color='red' or b.color='green') and b.bid=r.bid;
34
Find the sids of sailors who reserved red or green color boats
select r.sid
from boats b, reserves r
where (b.color='red' or b.color='green') and b.bid=r.bid;
select s.sid
from sailors s, reserves r
where (s.rating=10 or r.bid=104) and s.sid=r.sid;
35
Find the names of sailors who reserved both red and green
color boats
36
Find the names of sailors who reserved both red and green
color boats
select r.sid
from boats b, reserves r
where b.color='red' and b.bid=r.bid
intersect
select r.sid
from boats b, reserves r
where b.color='green' and b.bid=r.bid
37
Find all sids of sailors who are having rating of 10 or
reserved boat 104
38
Find all sids of sailors who are having rating of 10 or
reserved boat 104
39
Find the age of the youngest sailor for each rating level
40
SELECT Command
41
Find the age of the youngest sailor for each rating level
RATING MIN(S.AGE)
1 33
8 25.5
7 35
3 25.5
10 16
9 35
42
Find the age of youngest sailor who is eligible to vote(18
years age)for each rating level with at least 2 sailors
43
Find the age of youngest sailor who is eligible to vote(18
years age)for each rating level with atleast 2 sailors
where s.age>18
7 35
8 25.5
group by s.rating
having count(*)>1
44
Find the sailors whose rating is better than some sailor
called Horatio
45
Find the sailors whose rating is better than some sailor
called Horatio
select s.sid
from sailors s
where s.rating > any (select s2.rating
from sailors s2
SID
where s2.sname='Horatio');
58
71
74
31
32
46
Find the names of sailors who have not reserved a red boat
47
Find the names of sailors who have not reserved a red boat
22 102 10/10/98
48
Find the names of sailors who have not reserved a red boat
select s.sname
from sailors s
where s.sid NOT IN (select r.sid
from reserves r
where r.bid IN ( select b.bid
from boats
SNAME
where b.color='red‘);
Dustin
Lubber
Horatio
49
Find the sailors with highest rating
50
Find the sailors with highest rating
select s1.sid
from sailors s1
where s1.rating > = all(select s2.rating
from sailors s2);
SID
58
71
51
Find the details of sailors with Second highest rating
52
Find the details of sailors with Second highest rating
select max(s1.rating)
from sailors s1
where s1.rating<(select max(s2.rating)
from sailors s2)
53
Find the names of sailors with second highest age
54
Find the names of sailors with second highest age
55
Find the names of sailors with second highest age
56
Find the details of sailors with third highest rating
57
Find the details of sailors with third highest rating
58
S1
SID SNAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5 S2
32 Andy 8 25.5
58 Rusty 10 35 SID SNAME RATING AGE
64 Horatio 7 35 22 Dustin 7 45
95 Bob 3 63.5 29 Brutus 1 33
85 Art 3 25.5 31 Lubber 8 55.5
71 Zorba 10 16 32 Andy 8 25.5
74 Horatio 9 35 58 Rusty 10 35
64 Horatio 7 35
95 Bob 3 63.5
85 Art 3 25.5
71 Zorba 10 16
74 Horatio 9 35
59
Correlated Nested Queries
60
Get a list of customers who placed at least one order
61
EXISTS
SELECT column-names
FROM table-name
WHERE EXISTS (SELECT column-name
FROM table-name
WHERE condition)
62
Get a list of customers who placed at least one order
SELECT *
FROM Customer c
WHERE EXISTS (SELECT *
FROM Order o
WHERE o.CustomerId = c.CustomerId)
63
Get the list of customers who haven’t placed any orders yet
SELECT *
FROM Customer c
WHERE NOT EXISTS (SELECT *
FROM Order o
WHERE o.CustomerId = c.CustomerId)
64
Find the names of sailors who have reserved boat number 103
65
Find the names of sailors who have reserved boat number 103
select s.sname
from sailors s
where s.sid IN (select r.sid
from reserves r
where r.bid=103);
SNAME
Dustin
Lubber
Horatio
66
Find the names of sailors with age over 20 who have not reserved a red
boat
67
Find the names of sailors with age over 20 who have not reserved a red
boat
select s.sname
from sailors s
where s.age>20 and s.sid not in (select r.sid
from reserves r
where r.bid in(select b.bid
from boats b
SNAME where b.color='Red'));
Brutus
Andy
Rusty
Bob
Art
Horatio
68
Find the names of sailors who have reserved boat 103.
69
Find the names of sailors who have reserved boat 103.
SELECT S.sname
FROM Sailors S
WHERE EXISTS ( SELECT *
FROM Reserves R
WHERE R.bid = 103 AND R.sid = S.sid )
70
SELECT S.sname
FROM Sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid = 103 )
71
Find the ids and names of sailors who have reserved two
different boats on the same day.
72
Find the ids and names of sailors who have reserved two
different boats on the same day.
73
Find the ids and names of sailors who have reserved two
different boats on the same day.
74
Find the names of sailors who have reserved all boats.
75
Find the names of sailors who have reserved all boats.
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS ( ( SELECT B.bid FROM Boats B)
EXCEPT
( SELECT distinct R.bid FROM Reserves R
WHERE R.sid = S.sid )
);
76