0% found this document useful (0 votes)
21 views

MySQL Lab No 18-22

Uploaded by

guptaakshat3909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

MySQL Lab No 18-22

Uploaded by

guptaakshat3909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MySQL Lab no 18 (Database Fetching records)

[1] Consider the following MOVIE table and write the SQL queries based on it.

Movie_ID MovieName Type ReleaseDate ProductionCost BusinessCost

M001 The Kashmir Files Action 2022/01/26 1245000 1300000

M002 Attack Action 2022/01/28 1120000 1250000

M003 Looop Lapeta Thriller 2022/02/01 250000 300000

M004 Badhai Do Drama 2022/02/04 720000 68000

M005 Shabaash Mithu Biography 2022/02/04 1000000 800000

M006 Gehraiyaan Romance 2022/02/11 150000 120000

1. Display all information from movie.


2. Display the type of movies.
3. Display movieid, moviename, total_eraning by showing the business done by the movies.
Claculate the business done by movie using the sum of productioncost and businesscost.
4. Display movieid, moviename and productioncost for all movies with productioncost greater
thatn 150000 and less than 1000000.
5. Display the movie of type action and romance.
6. Display the list of movies which are going to release in February, 2022.

Answers:

[1] select * from movie;

Output:
2. select distinct from a movie;

3. select movieid, moviename, productioncost + businesscost “total earning” from movie;

4. select movie_id,moviename, productioncost from movie where producst is >150000 and


<1000000;

5. select moviename from movie where type =’action’ or type=’romance’;


6. select moviename from moview where month(releasedate)=2;

MySQL Lab no 19 (Based on Functions)


1. Write a query to display cube of 5.
2. Write a query to display the number 563.854741 rounding off to the next hundred.
3. Write a query to display “put” from the word “Computer”.
4. Write a query to display today’s date into DD.MM.YYYY format.
5. Write a query to display ‘DIA’ from the word “MEDIA”.
6. Write a query to display moviename – type from the table movie.
7. Write a query to display first four digits of productioncost.
8. Write a query to display last four digits of businesscost.
9. Write a query to display weekday of release dates.
10. Write a query to display dayname on which movies are going to be released.

Answers:

[1] select pow(5,3);

[2] select round(563.854741,-2);


[3] select mid(“Computer”,4,3);

[4] select concat(day(now()),concat(‘.’,month(now()),concat(‘.’,year(now())))) “Date”;

[5] select right(“Media”,3);

[6] select concat(moviename,concat(‘ – ‘,type)) from movie;


[7] select left(productioncost,4) from movie;

[8] select right(businesscost,4) from movie;

[9] select weekday(releasedate) from movie;


[10] select dayname(releasedate) from movie;

MySQL Lab no 20 (DDL Commands)


Suppose your school management has decided to conduct cricket matches between students of
Class XI and Class XII. Students of each class are asked to join any one of the four teams – Team
Titan, Team Rockers, Team Magnet and Team Hurricane. During summer vacations, various
matches will be conducted between these teams. Help your sports teacher to do the following:

1. Create a database “Sports”.


2. Create a table “TEAM” with following considerations:
o It should have a column TeamID for storing an integer value between 1 to 9, which
refers to unique identification of a team.
o Each TeamID should have its associated name (TeamName), which should be a
string of length not less than 10 characters.
o Using table level constraint, make TeamID as the primary key.
o Show the structure of the table TEAM using a SQL statement.
o As per the preferences of the students four teams were formed as given below.
Insert these four rows in TEAM table:
 Row 1: (1, Tehlka)
 Row 2: (2, Toofan)
 Row 3: (3, Aandhi)
 Row 3: (4, Shailab)
oShow the contents of the table TEAM using a DML statement.
3. Now create another table MATCH_DETAILS and insert data as shown below. Choose
appropriate data types and constraints for each attribute.

MatchID MatchDate FirstTeamID SecondTeamID FirstTeamScore SecondTeamScore

M1 2021/12/20 1 2 107 93

M2 2021/12/21 3 4 156 158

M3 2021/12/22 1 3 86 81

M4 2021/12/23 2 4 65 67

M5 2021/12/24 1 4 52 88

M6 2021/12/25 2 3 97 68

Answers:

[1] create database sports

[2] Creating table with the given specification

create table team


-> (teamid int(1),
-> teamname varchar(10), primary key(teamid));
Showing the structure of table using SQL statement:

desc team;
Inserting data:

mqsql> insert into team


-> values(1,'Tehlka');

Show the content of table – team:

select * from team;

Creating another table:


create table match_details
-> (matchid varchar(2) primary key,
-> matchdate date,
-> firstteamid int(1) references team(teamid),
-> secondteamid int(1) references team(teamid),
-> firstteamscore int(3),
-> secondteamscore int(3));

MySQL Lab no 21 (Based on Two Tables)


1. Display the matchid, teamid, teamscore whoscored more than 70 in first ining along with
team name.
2. Display matchid, teamname and secondteamscore between 100 to 160.
3. Display matchid, teamnames along with matchdates.
4. Display unique team names
5. Display matchid and matchdate played by Anadhi and Shailab.

Answers:

[1] select match_details.matchid, match_details.firstteamid,


team.teamname,match_details.firstteamscore from match_details, team where
match_details.firstteamid=team.teamid and match_details.firstteamscore>70;
[2] select matchid, teamname, secondteamscore from match_details, team where
match_details.secondteamid=team.teamid and match_details.secondteamscore between 100 and
160;

[3] select matchid,teamname,firstteamid,secondteamid,matchdate from match_details, team


where match_details.firstteamid=team.teamid;

[4] select distinct(teamname) from match_details, team where


match_details.firstteamid=team.teamid;

[5] select matchid,matchdate from match_details, team where


match_details.firstteamid=team.teamid and team.teamname in (‘Aandhi’,’Shailab’);
MySQL Lab no 22 (Group by , Order By)
Consider the following table stock table to answer the queries:

itemno item dcode qty unitprice stockdate

S005 Ballpen 102 100 10 2018/04/22

S003 Gel Pen 101 150 15 2018/03/18

S002 Pencil 102 125 5 2018/02/25

S006 Eraser 101 200 3 2018/01/12

S001 Sharpner 103 210 5 2018/06/11

S004 Compass 102 60 35 2018/05/10

S009 A4 Papers 102 160 5 2018/07/17

1. Display all the items in the ascending order of stockdate.


2. Display maximum price of items for each dealer individually as per dcode from stock.
3. Display all the items in descending orders of itemnames.
4. Display average price of items for each dealer individually as per doce from stock which
avergae price is more than 5.
5. Diisplay the sum of quantity for each dcode.

[1] select * from stock order by stockdate;


[2] select dcode,max(unitprice) from stock group by code;

[3] select * from stock order by item desc;

[4] select dcode,avg(unitprice) from stock group by dcode having avg(unitprice)>5;


[5] select dcode,sum(qty) from stock group by dcode;

MySQL Lab no 23 SQL Database Connectivity


1. Write a MySQL connectivity program in Python to
o Create a database school
o Create a table students with the specifications – ROLLNO integer, STNAME
character(10) in MySQL and perform the following operations:
 Insert two records in it
 Display the contents of the table
2. Perform all the operations with reference to table ‘students’ through MySQL-Python
connectivity.
Answers:

By using mysqlconnector
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",data
base='school')
cn=db.cursor()
def insert_rec():
try:
while True:
rn=int(input("Enter roll number:"))
sname=input("Enter name:")
marks=float(input("Enter marks:"))
gr=input("Enter grade:")
cn.execute("insert into students
values({},'{}',{},'{}')".format(rn,sname,marks,gr))
db.commit()
ch=input("Want more records? Press (N/n) to stop entry:")
if ch in 'Nn':
break
except Exception as e:
print("Error", e)

def update_rec():
try:
rn=int(input("Enter rollno to update:"))
marks=float(input("Enter new marks:"))
gr=input("Enter Grade:")
cn.execute("update students set marks={},grade='{}' where
rno={}".format(marks,gr,rn))
db.commit()
except Exception as e:
print("Error",e)

def delete_rec():
try:
rn=int(input("Enter rollno to delete:"))
cn.execute("delete from students where rno={}".format(rn))
db.commit()
except Exception as e:
print("Error",e)

def view_rec():
try:
cn.execute("select * from students")
except Exception as e:
print("Error",e)

while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete
Record\n4. Display Record \n5. Exit")
ch=int(input("Enter your choice<1-4>="))
if ch==1:
insert_rec()
elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
print("Wrong option selected")

You might also like