0% found this document useful (0 votes)
14 views13 pages

DBMS Lab

The document contains a series of MySQL commands executed in a database named DBMSLab, showcasing various queries to display and manipulate data related to boats and sailors. It includes commands to show tables, select specific records based on conditions, and aggregate functions to analyze sailor ratings and ages. The document serves as a practical example of using SQL for database management and querying.

Uploaded by

saimanjithp
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)
14 views13 pages

DBMS Lab

The document contains a series of MySQL commands executed in a database named DBMSLab, showcasing various queries to display and manipulate data related to boats and sailors. It includes commands to show tables, select specific records based on conditions, and aggregate functions to analyze sailor ratings and ages. The document serves as a practical example of using SQL for database management and querying.

Uploaded by

saimanjithp
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/ 13

DBMS Lab

amaravadi@amaravadi-sanjay:~$ sudo mysql;


[sudo] password for amaravadi:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.39-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2024, 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 |
+---------------------------+
| DBMSLab |
| information_schema |
| mysql |
| performance_schema |
| sys |
+-------------------------=+
5 rows in set (0.00 sec)

mysql> use DBMSLab;


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> show tables;
+---------------------------+
| Tables_in_DBMSLab |
+---------------------------+
| Boats |
| EMPLOYEE |
| Employee |
| Reserves |
| Sailors |
| Student |
| emp |
+---------------------------+
7 rows in set (0.00 sec)

Department of Computer Science Enginerring Page no:1


1. Display Boats Table
mysql> select * from Boats;
+-----+-----------+-------+
| bid | bname | color |
+-----+-----------+-------+
| 101 | Interlake | blue |
| 102 | Interlake | red |
| 103 | Clipper | green |
| 104 | Marine | red |
+-----+-----------+-------+
4 rows in set (0.00 sec)

2.Display Sailors Table


mysql> select * from Sailors;
+------+---------+--------+-------+
| Sid | Sname | Rating | Age |
+------+---------+--------+-------+
| 22 | Dustin | 7 | 45 |
| 29 | Brutus | 1 | 33 |
| 31 | Lubber | 8 | 55.5 |
| 32 | Andy | 8 | 25.5 |
| 58 | Rusty | 10 | 35 |
| 64 | Horatio | 7 | 35 |
| 71 | Zorba | 10 | 16 |
| 74 | Horatio | 9 | 40 |
| 85 | Art | 3 | 25.5 |
| 95 | Bob | 3 | 63.5 |
+------+---------+--------+-------+
10 rows in set (0.00 sec)

3.Display Reaserves Table


mysql> select * from Reserves;
+------+------+------------+
| sid | bid | day |
+------+------+------------+
| 22 | 101 | 1998-10-10 |
| 22 | 102 | 1998-10-10 |
| 22 | 103 | 1998-10-08 |
| 22 | 104 | 1998-10-07 |
| 31 | 102 | 1998-11-10 |
| 31 | 103 | 1998-11-06 |
| 31 | 104 | 1998-11-12 |
| 64 | 101 | 1998-09-05 |
| 64 | 102 | 1998-09-08 |
| 74 | 103 | 1998-09-08 |
+------+------+------------+
10 rows in set (0.00 sec)

4.Display the names of Sailors whose rating is greater than 5


mysql> select * from Sailors where Rating>5;
+------+---------+--------+------+
| Sid | Sname | Rating | Age |
+------+---------+--------+------+
Department of Computer Science Enginerring Page no:2
| 22 | Dustin | 7 | 45 |
| 31 | Lubber | 8 | 55.5 |
| 32 | Andy | 8 | 25.5 |
| 58 | Rusty | 10 | 35 |
| 64 | Horatio | 7 | 35 |
| 71 | Zorba | 10 | 16 |
| 74 | Horatio | 9 | 40 |
+------+---------+--------+------+
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)

6. Display ID’s of Sailors who have reserved boat 103


mysql> select sid from Reserves where bid=103;
+------+
| sid |
+------+
| 22 |
| 31 |
| 74 |
+------+
3 rows in set (0.00 sec)

7.List the ID’s who’ve reserved atleast one boat


mysql> select Distinct sid from Reserves;
+------+
| sid |
+------+
| 22 |
| 31 |
| 64 |
| 74 |
+------+
4 rows in set (0.04 sec)

8.Display color of boats


mysql> select color from Boats;
+-------+
| color |
+-------+
| blue |
| red |
| green |
Department of Computer Science Enginerring Page no:3
| red |
+-------+
4 rows in set (0.00 sec)

9.Display Name of the boats whose colour is red


mysql> select bname from Boats where color="red";
+-----------+
| bname |
+-----------+
| Interlake |
| Marine |
+-----------+
2 rows in set (0.00 sec)

9.Display Name of the boats whose colour is blue


mysql> select bname from Boats where color="blue";
+-----------+
| bname |
+-----------+
| Interlake |
+-----------+
1 row in set (0.00 sec)

10.Display ID’s of Sailors who’ve reserved boats on 10-10-1998


mysql> select DISTINCT sid from Reserves where day="1998-10-10";
+------+
| sid |
+------+
| 22 |
+------+
1 row in set (0.01 sec)

11.Display Name’s of Sailors who’ve reserved boats on 10-10-1998


mysql> select Distinct Sname from Sailors s, Reserves r where r.day="1998-10-10" and s.Sid=r.sid;
+--------+
| Sname |
+--------+
| Dustin |
+--------+
1 row in set (0.04 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)

14. Display Sname who have reserved bname equal to ‘Interlake’


mysql> select distinct Sname from Sailors s, Boats b, Reserves r where s.Sid=r.sid and r.bid=b.bid and
b.bname="Interlake";
+---------+
| Sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
3 rows in set (0.00 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)

16.Display the names of Sailors who reserved atleast one boat


mysql> select distinct Sname from Sailors s,Reserves r where 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)

22.Display name of the Sailors whose rating is better than Lubber


mysql> select Sname from Sailors where Rating > (select Rating from Sailors where Sname="Lubber");
+---------+
| Sname |
+---------+
| Rusty |
| Zorba |
| Horatio |
+---------+
3 rows in set (0.04 sec)

23. Display the name of Sailors whose rating is highest


mysql> select Sname from Sailors where Rating=(select MAX(Rating) from Sailors);
+-------+
| Sname |
+-------+
| Rusty |
| Zorba |
+-------+
2 rows in set (0.00 sec)

24. Display the name of 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)

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)

30. Count number of Sailors in Sailors table


mysql> select Count(Sid) from Sailors;
+------------+
| Count(Sid) |
+------------+
| 10 |
+------------+
1 row in set (0.00 sec)

31. count number of different Sailors


mysql> select Count(distinct Sname) from Sailors;
+-----------------------+
| Count(distinct Sname) |
+-----------------------+
| 9|
+-----------------------+
Department of Computer Science Enginerring Page no:8
1 row in set (0.00 sec)

32. Find average Age of all Sailors


mysql> select avg(Age) from Sailors;
+----------+
| avg(Age) |
+----------+
| 37.4 |
+----------+
1 row in set (0.00 sec)

33. Find average Age of Sailors with Rating 10


mysql> select avg(Age) from Sailors where Rating =10;
+----------+
| avg(Age) |
+----------+
| 25.5 |
+----------+
1 row in set (0.00 sec)

34. Find the Name and Age of the Oldest Sailors


mysql> select Sname,Age from Sailors where Age=(select Max(Age) from Sailors);
+-------+------+
| Sname | Age |
+-------+------+
| Bob | 63.5 |
+-------+------+
1 row 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)

36. Find Sailors whose Rating is better 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)

37.Find Sailor whose Rating is high


Department of Computer Science Enginerring Page no:9
mysql> select Sname from Sailors where Rating = (Select Max(Rating) from Sailors);
+-------+
| Sname |
+-------+
| Rusty |
| Zorba |
+-------+
2 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)

39.Find all the Sailors whose name consits 5 letters


mysql> select * from Sailors where length(Sname) =5;
+------+-------+--------+------+
| Sid | Sname | Rating | Age |
+------+-------+--------+------+
| 58 | Rusty | 10 | 35 |
| 71 | Zorba | 10 | 16 |
+------+-------+--------+------+
2 rows in set (0.00 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)

41. Find the Sailors whose age is between 35 & 40


mysql> select * from Sailors where Age>=35 and Age<=40;
+------+---------+--------+------+
| Sid | Sname | Rating | Age |
+------+---------+--------+------+
| 58 | Rusty | 10 | 35 |
| 64 | Horatio | 7 | 35 |
| 74 | Horatio | 9 | 40 |
+------+---------+--------+------+
3 rows 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)

43. Count number of Records in Reserves table


mysql> select count(Sid) from Reserves;
+------------+
| count(Sid) |
+------------+
| 10 |
+------------+
1 row in set (0.00 sec)

44. Number of Sailors for each Rating count


mysql> select Rating,count(Rating) from Sailors group by Rating;
+--------+---------------+
| Rating | count(Rating) |
+--------+---------------+
| 7| 2|
| 1| 1|
| 8| 2|
| 10 | 2|
| 9| 1|
| 3| 2|
+--------+---------------+
6 rows in set (0.00 sec)

45. Number of reserves for each Red color boat


mysql> select bid,count(bid) from Reserves where bid in (select bid from Boats where color="red")
group by bid;
+------+------------+
| bid | count(bid) |
+------+------------+
| 102 | 3|
| 104 | 2|
+------+------------+
2 rows in set (0.00 sec)

46. Display Name, ID, No.of Boats reserved by each Sailor


mysql> select s.Sname,s.Sid,count(r.bid) from Sailors s, Reserves r where s.Sid=r.sid group by
s.Sid,s.Sname;
+---------+------+--------------+
| Sname | Sid | count(r.bid) |
+---------+------+--------------+
| Dustin | 22 | 4|
| Lubber | 31 | 3|
| Horatio | 64 | 2|
| Horatio | 74 | 1|
+---------+------+--------------+
4 rows in set (0.00 sec)

Department of Computer Science Enginerring Page no:11


47. Find the Average age of Sailors of each Rating level that has atleast 2 Sailors
mysql> select Rating,avg(Age) from Sailors group by Rating having count(Sid)>=2;
+--------+----------+
| Rating | avg(Age) |
+--------+----------+
| 7| 40 |
| 8 | 40.5 |
| 10 | 25.5 |
| 3 | 44.5 |
+--------+----------+
4 rows in set (0.00 sec)

48. Name of Sailors who reserved all Boats


mysql> select Sname from Sailors s where not exists(select bid from Boats except select bid from
Reserves r where r.sid=s.sid);
+--------+
| Sname |
+--------+
| Dustin |
+--------+
1 row 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)

50. Display age difference between Dustin and Lubber


mysql> select abs(s1.Age - s2.Age) from Sailors s1, Sailors s2 where s1.Sname="Dustin" and
s2.Sname="Lubber";
+----------------------+
| abs(s1.Age - s2.Age) |
+----------------------+
| 10.5 |
+----------------------+
1 row in 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)

52. Find the Length of all Sailors name


mysql> select Sname,length(Sname) from Sailors;
+---------+---------------+
| Sname | length(Sname) |
+---------+---------------+
Department of Computer Science Enginerring Page no:12
| Dustin | 6|
| Brutus | 6|
| Lubber | 6|
| Andy | 4|
| Rusty | 5|
| Horatio | 7|
| Zorba | 5|
| Horatio | 7|
| Art | 3|
| Bob | 3|
+---------+---------------+
10 rows in set (0.01 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)

Department of Computer Science Enginerring Page no:13

You might also like