DBMS Lab
DBMS Lab
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_DBMSLab |
+---------------------------+
| Boats |
| EMPLOYEE |
| Employee |
| Reserves |
| Sailors |
| Student |
| emp |
+---------------------------+
7 rows in set (0.00 sec)
5. Display names and ID’s of sailors whose age is less than 50 and Rating is greater than 7
mysql> select Sid,Sname from Sailors where Age<50 AND Rating>7;
+------+---------+
| Sid | Sname |
+------+---------+
| 32 | Andy |
| 58 | Rusty |
| 71 | Zorba |
| 74 | Horatio |
+------+---------+
4 rows in set (0.00 sec)
12.Display the name of the Sailors whose rating > 7 and who have reserved boat no 103
mysql> Select distinct Sname from Sailors s, Reserves r where s.Rating > 7 and r.bid = 103;
+---------+
| Sname |
+---------+
| Lubber |
| Andy |
| Rusty |
| Zorba |
| Horatio |
+---------+
5 rows in set (0.00 sec)
Department of Computer Science Enginerring Page no:4
13.display bname which have been reserved by sid=22
mysql> select distinct bname from Boats b, Reserves r where r.sid=22;
+-----------+
| bname |
+-----------+
| Interlake |
| Clipper |
| Marine |
+-----------+
3 rows in set (0.04 sec)
15. Display Sname from Sailors who reserved red colors boat
mysql> select distinct Sname from Sailors s,Reserves r, Boats b where b.color="red" and r.bid=b.bid and
s.Sid=r.sid;
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
3 rows in set (0.00 sec)
17. Display the name of boats which was reserved by Sailor Lubber.
mysql> select bname from Boats b, Sailors s, Reserves r where s.Sname="Lubber" and s.Sid = r.sid and
r.bid=b.bid;
+-----------+
| bname |
+-----------+
| Interlake |
Department of Computer Science Enginerring Page no:5
| Clipper |
| Marine |
+-----------+
3 rows in set (0.00 sec)
or
mysql> select distinct bname from Boats b, Sailors s, Reserves r where s.Sname="Lubber" and s.Sid =
r.sid;
+-----------+
| bname |
+-----------+
| Interlake |
| Clipper |
| Marine |
+-----------+
3 rows in set (0.00 sec)
18. Display the rating, age of Sailors who reserved Interlake boat
mysql> select distinct Rating,Age from Sailors s, Reserves r, Boats b where b.bname="Interlake" and
b.bid=r.bid and s.Sid= r.sid;
+--------+------+
| Rating | Age |
+--------+------+
| 7 | 45 |
| 8 | 55.5 |
| 7 | 35 |
+--------+------+
3 rows in set (0.00 sec)
19.Display the ID of the sailors who reserved Red of Green color boat
mysql> select distinct s.Sid from Sailors s, Reserves r, Boats b where b.bid=r.bid and r.sid=s.Sid and
(b.color="red" or b.color="green");
+------+
| Sid |
+------+
| 22 |
| 31 |
| 64 |
| 74 |
+------+
4 rows in set (0.00 sec)
20. Display the ID of the Sailor who reserved both Red and Green boats
21.Display the Name of the Sailor who reserved red color Interlake boat
mysql> select distinct Sname from Sailors s, Reserves r, Boats b where b.bname="Interlake" and
b.color="red" and b.bid = r.bid and r.sid=s.Sid;
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
Department of Computer Science Enginerring Page no:6
3 rows in set (0.00 sec)
25. Display the Name of the Sailors who reserved boats atleast 2 times
mysql> select Sname from Sailors s, Reserves r where s.Sid=r.sid group by s.Sname having
count(r.bid)>=2;
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
3 rows in set (0.00 sec)
26. Display the name of Sailors whose age is above 20 and reserved Red color boat
mysql> select distinct Sname from Sailors s,Boats b, Reserves r where b.color="red" and b.bid=r.bid and
s.Sid=r.sid and s.Age>20;
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
Department of Computer Science Enginerring Page no:7
| Horatio |
+---------+
3 rows in set (0.00 sec)
27.Display the list of IDs of Sailors who have reserved Red color boat but not Green boat
mysql> select s.Sid from Sailors s, Reserves r, Boats b where b.color!="green" and b.color="red" and
b.bid=r.bid and s.Sid=r.sid and s.sid not in (select s.Sid from Sailors s, Reserves r, Boats b where
s.Sid=r.sid and r.bid=b.bid and b.color="green");
+------+
| Sid |
+------+
| 64 |
+------+
1 row in set (0.00 sec)
28. Display the name of the Sailors whose rating is less than the average
mysql> select Sname from Sailors where Rating < (select AVG(Rating) from Sailors);
+--------+
| Sname |
+--------+
| Brutus |
| Art |
| Bob |
+--------+
3 rows in set (0.00 sec)
29. Display the name of Sailors whose rating is greater than Horatio
mysql> select Sname from Sailors where Rating > (select Max(Rating) from Sailors where
Sname="Horatio");
+-------+
| Sname |
+-------+
| Rusty |
| Zorba |
+-------+
2 rows in set (0.00 sec)
35. Find the Name of the Sailors who are older than the oldest Sailors with Ration 10
mysql> select Sname from Sailors where Age>(Select Max(Age) from Sailors where Rating = 10);
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
| Bob |
+---------+
4 rows in set (0.00 sec)
38. Find all the Sailors whose name Starts with ‘A’
mysql> select * from Sailors where Sname like 'A%';
+------+-------+--------+------+
| Sid | Sname | Rating | Age |
+------+-------+--------+------+
| 32 | Andy | 8 | 25.5 |
| 85 | Art | 3 | 25.5 |
+------+-------+--------+------+
2 rows in set (0.01 sec)
40. Find all the Sailors whose name Starts and End with ‘B’
mysql> select * from Sailors where Sname like 'B%B';
+------+-------+--------+------+
| Sid | Sname | Rating | Age |
+------+-------+--------+------+
| 95 | Bob | 3 | 63.5 |
+------+-------+--------+------+
1 row in set (0.00 sec)
42. Find the name of the sailorrs who has ‘st’ in their name
mysql> select Sname from Sailors where Sname like '%st%';
+--------+
| Sname |
Department of Computer Science Enginerring Page no:10
+--------+
| Dustin |
| Rusty |
+--------+
2 rows in set (0.00 sec)
49. Display the ID of boats which have been reserved by all Sailors who are listed in reserved table
mysql> select bid from Reserves group by bid having count(distinct sid) = (select count(Sid) from
Sailors);
Empty set (0.00 sec)
51. Find the ID of Sailors who reserved boats from Sep to Nov
mysql> select distinct sid from Reserves where day between "1998-09-01" and "1998-11-01";
+------+
| sid |
+------+
| 22 |
| 64 |
| 74 |
+------+
3 rows in set (0.00 sec)
53. Display the details of 1st five Sailors in order of their rating from high to low
mysql> select * from Sailors order by Rating desc limit 5;
+------+---------+--------+------+
| Sid | Sname | Rating | Age |
+------+---------+--------+------+
| 58 | Rusty | 10 | 35 |
| 71 | Zorba | 10 | 16 |
| 74 | Horatio | 9 | 40 |
| 31 | Lubber | 8 | 55.5 |
| 32 | Andy | 8 | 25.5 |
+------+---------+--------+------+
5 rows in set (0.00 sec)
54. Find the Name of the Sailors whose Rating is Second highest
mysql> select Sname from Sailors where Rating = (select max(Rating) from Sailors where Rating <
(select max(Rating) from Sailors));
+---------+
| Sname |
+---------+
| Horatio |
+---------+
1 row in set (0.00 sec)