SQL Docx
SQL Docx
SQL Docx
PROBLEM STATEMENT: Create a SHOPPE table with the ID, SName and Area as attributes
where the ID is Primary Key
SOURCE CODE:
mysql> use PRACTICAL;
Database changed
mysql> create table SHOPPE(Id varchar(20),SName varchar(20),Area varchar(20));
Query OK, 0 rows affected (7.96 sec)
OUTPUT :
mysql> describe SHOPPE;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id | varchar(20) | YES | | NULL | |
| SName | varchar(20) | YES | | NULL |
| Area | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (1.49 sec)
SCREENSHOT OF Output:
1|Page
1. Insert the details of new students in the above table
SOURCE CODE:
mysql> insert into SHOPPE values("S001","ABC Computeronics","CP");
Query OK, 1 row affected (1.54 sec)
mysql> insert into SHOPPE values("S002","All Infotech Media","GK II");
Query OK, 1 row affected (0.18 sec)
OUTPUT :
mysql> Select*from SHOPPE;
+------+--------------------+-------------+
| Id | SName | Area |
+------+--------------------+-------------+
| S001 | ABC Computeronics | CP |
| S002 | All Infotech Media | GK II |
| S003 | Tech Shoppe | CP |
| S004 | Geeks Tecno Soft | Nehru Place |
| S005 | Hitech Tech Store | Nehru Place |
+------+--------------------+-------------+
5 rows in set (0.40 sec)
2|Page
SCREENSHOT OF Output:
3|Page
PROBLEM NUMBER:2
PROBLEM STATEMENT: Create a database COMPANY and within that database, create
tables PRODUCT and CLIENT and perform the following queries. (For the first table,
mention P_ID as the primary key and for the second table, mention C_ID as the primary
key.)
TABLE: PRODUCT
TABLE: CLIENT
4|Page
OUTPUT :
+------+---------------+-------+------+
| C_ID | CLIENT_NAME | CITY | P_ID |
+------+---------------+-------+------+
| 1 | COSMETIC SHOP | DELHI | FW05 |
| 12 | LIVE LIFE | DELHI | SH06 |
| 15 | PRETTY WOMAN | DELHI | FW12 |
+------+---------------+-------+------+
SCREENSHOT OF Output:
2. To display the details of those clients whose price is between 50 and 100
SOURCE CODE:
mysql> Select*from PRODUCT where PRICE between 50 and 100;
OUTPUT :
+------+--------------+--------------+-------+
| P_ID | PRODUCT_NAME | MANUFACTURER | PRICE |
+------+--------------+--------------+-------+
| BS01 | BATH SHOP | ABC | 55 |
| FW12 | FACE WASH | XYZ | 95 |
+------+--------------+--------------+-------+
2 rows in set (0.05 sec)
5|Page
SCREENSHOT OF Output:
OUTPUT :
+---------------+-----------+--------------+-------+
| CLIENT_NAME | CITY | PRODUCT_NAME | PRICE |
+---------------+-----------+--------------+-------+
| COSMETIC SHOP | DELHI | FACE WASH | 45 |
| TOTAL HEALTH | MUMBAI | BATH SHOP | 55 |
| LIVE LIFE | DELHI | SHAMPOO | 120 |
| PRETTY WOMAN | DELHI | FACE WASH | 95 |
| DREAMS | BENGALURU | TALCUM POWER | 40 |
+---------------+-----------+--------------+-------+
5 rows in set (0.07 sec)
SCREENSHOT OF Output:
6|Page
4.to update the price increased by 10
SOURCE CODE:
mysql> UPDATE PRODUCT SET PRICE=PRICE+10;
Query OK, 5 rows affected (0.21 sec)
Rows matched: 5 Changed: 5 Warnings: 0
OUTPUT :
+------+--------------+--------------+-------+
| P_ID | PRODUCT_NAME | MANUFACTURER | PRICE |
+------+--------------+--------------+-------+
| BS01 | BATH SHOP | ABC | 65 |
| FW05 | FACE WASH | ABC | 55 |
| FW12 | FACE WASH | XYZ | 105 |
| SH06 | SHAMPOO | XYZ | 130 |
| TP01 | TALCUM POWER | LAK | 50 |
+------+--------------+--------------+-------+
5 rows in set (0.00 sec)
SCREENSHOT OF Output:
7|Page
5. To display details from product where MANUFACTURER is "ABC" AND PRICE< is less
than 50;
SOURCE CODE:
mysql> SELECT* from product where MANUFACTURER="ABC" AND PRICE<50;
OUTPUT :
Empty set (0.00 sec)
SCREENSHOT OF Output:
SOURCE CODE:
mysql> SELECT MAX(PRICE) FROM PRODUCT;
OUTPUT :
+------------+
| MAX(PRICE) |
+------------+
| 130 |
+------------+
8|Page
SCREENSHOT OF Output:
SOURCE CODE:
OUTPUT :
+------+---------------+-------+
| C_ID | CLIENT_NAME | CITY |
+------+---------------+-------+
| 1 | COSMETIC SHOP | DELHI |
| 15 | PRETTY WOMAN | DELHI |
+------+---------------+-------+
2 rows in set (0.04 sec)
SCREENSHOT OF Output:
9|Page
PROBLEM NUMBER: 3
PROBLEM STATEMENT:
Create a student table with the student ID, Name and marks as attributes where the
Student ID is Primary Key and perform the following queries
SOURCE CODE:
mysql> create database school;
Query OK, 1 row affected (0.48 sec)
OUTPUT :
+------------+--------------+------+-----+---------+---
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| STUDENT_ID | int | NO | PRI | NULL | |
| NAME | varchar(20) | NO | | NULL | |
| MARKS | decimal(5,2) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (1.29 sec)
10 | P a g e
SCREENSHOT OF Output:
OUTPUT :
+------------+--------+-------+
| STUDENT_ID | NAME | MARKS |
+------------+--------+-------+
| 1 | Arpit | 78.50 |
| 2 | Bharti | 87.50 |
| 3 | Chintu | 62.50 |
| 4 | Charmi | 89.50 |
11 | P a g e
| 5 | Divya | 80.00 |
+------------+--------+-------+
5 rows in set (0.00 sec)
SCREENSHOT OF Output:
OUTPUT :
+------------+--------+-------+
| STUDENT_ID | NAME | MARKS |
+------------+--------+-------+
| 1 | Arpit | 78.50 |
| 2 | Bharti | 87.50 |
| 3 | Chintu | 62.50 |
| 4 | Charmi | 89.50 |
+------------+--------+-------+
4 rows in set (0.00 sec)
12 | P a g e
SCREENSHOT OF Output:
2.Use the select command to get the details of the students with marks more than 80.
SOURCE CODE:
mysql> select*from student where marks>80;
mysql> select*from student;
OUTPUT :
+------------+--------+-------+
| STUDENT_ID | NAME | MARKS |
+------------+--------+-------+
| 2 | Bharti | 87.50 |
| 4 | Charmi | 89.50 |
+------------+--------+-------+
2 rows in set (0.03 sec)
SCREENSHOT OF Output:
3.Find the min, max, sum and average of the marks in a student marks table.
SOURCE CODE:
mysql> select max(Marks), min(marks),sum(marks),avg(marks) from STUDENT;
13 | P a g e
OUTPUT :
+------------+------------+------------+------------+
| max(Marks) | min(marks) | sum(marks) | avg(marks) |
+------------+------------+------------+------------+
| 89.50 | 62.50 | 318.00 | 79.500000 |
+------------+------------+------------+------------+
1 row in set (0.03 sec)
SCREENSHOT OF Output:
4.Write an SQL query to display the marks without decimal places, display the
reminder after dividing marks by 3 and display the square of marks.
SOURCE CODE:
mysql> select round(marks,0), mod(marks,3), pow(marks,2) from student;
OUTPUT :
+----------------+--------------+--------------+
| round(marks,0) | mod(marks,3) | pow(marks,2) |
+----------------+--------------+--------------+
| 79 | 0.50 | 6162.25 |
| 88 | 0.50 | 7656.25 |
| 63 | 2.50 | 3906.25 |
| 90 | 2.50 | 8010.25 |
+----------------+--------------+--------------+
4 rows in set (0.12 sec)
14 | P a g e
SCREENSHOT OF Output:
5.Write an SQL query to display names into capital letter, display first 3 letters of
name, display last 3 letters of name, display the position the letter A in name.
SOURCE CODE:
mysql>select ucase(name), lcase(name), left(name,3), right(name,3), instr(name,'a') from student;
OUTPUT :
+-------------+-------------+--------------+---------------+-----------------+
| ucase(name) | lcase(name) | left(name,3) | right(name,3) | instr(name,'a') |
+-------------+-------------+--------------+---------------+-----------------+
| ARPIT | arpit | Arp | pit | 1|
| BHARTI | bharti | Bha | rti | 3|
| CHINTU | chintu | Chi | ntu | 0|
| CHARMI | charmi | Cha | rmi | 3|
+-------------+-------------+--------------+---------------+-----------------+
SCREENSHOT OF Output:
6.Remove extra spaces from left, right and both sides from the text-“Computer Science
Class XII”
SOURCE CODE:
mysql> select ltrim(" Computer Science Class XII") "Left spaces", rtrim(" Computer Science Class XII ") "Right
Spaces",trim(" Computer Science Class XII ") "Both spaces";
15 | P a g e
SCREENSHOT OF THE SOURCE CODE:
OUTPUT :
+----------------------------+-----------------------------+---------------+
| Left spaces | Right Spaces | Both spaces |
+----------------------------+-----------------------------+---------------+
| Computer Science Class XII | Computer Science Class XII | Computer Science Class XII |
+------------------------+-----------------------------+----------------+
SCREENSHOT OF Output:
OUTPUT :
+------------------------------------------------------+
| concat(day(now()),"/", month(now()),"/",year(now())) |
+------------------------------------------------------+
| 22/10/2022 |
+------------------------------------------------------+
1 row in set (0.12 sec)
SCREENSHOT OF Output:
16 | P a g e
8.Display dayname, monthname, day of month, day of year for today’s date.
SOURCE CODE:
mysql> select dayname(now()), monthname(now()) , day(now()),dayname(now()),dayofyear(now());
SCREENSHOT OF THE SOURCE CODE:
OUTPUT :
+----------------+------------------+------------+----------------+------------------+
| dayname(now()) | monthname(now()) | day(now()) | dayname(now()) | dayofyear(now()) |
+----------------+------------------+------------+----------------+------------------+
| Saturday | October | 22 | Saturday | 295 |
+----------------+------------------+------------+----------------+------------------+
1 row in set (0.12 sec)
SCREENSHOT OF Output:
17 | P a g e
PROBLEM NUMBER: 4
PROBLEM STATEMENT: Create a employee table with the E_CODE, E_Name, SEX,
Dept, Salary and City as attributes and perform the following functions.
SOURCE CODE: TABLE EMPLOYEE
mysql> create table EMPLOYEE(E_CODE varchar(4),E_NAME text(20),SEX text(2), DEPT text(20),SALARY int(10),CITY
text(10));
ducation","15000","Gawhati");
00","Surat");
","Jaipur City");
300","Mumbai");
600","Chandigarh");
0","Chennai");
18 | P a g e
Query OK, 1 row affected (0.00 sec)
69000","Hridaypur");
9800","Jaipur City");
t","75000","Gawhati");
19 | P a g e
SCREENSHOT OF TABLE:
SOURCE CODE:
mysql> select distinct DEPT from EMPLOYEE;
SCREENSHOT:
2.Find the name and salary of those employee whose salary 35000 and 40000.
SOURCE CODE:
mysql> select E_NAME,SALARY from EMPLOYEE where SALARY>35000 and salary<40000;
20 | P a g e
SCREENSHOT:
SCREENSHOT:
SCREENSHOT:
SCREENSHOT:
SCREENSHOT:
22 | P a g e
8.Display average salary of employee in the employee table.
SOURCE CODE:
mysql> select avg(SALARY) from EMPLOYEE;
SCREENSHOT:
SCREENSHOT:
23 | P a g e
SCREENSHOT:
24 | P a g e
PROBLEM NUMBER: 5
PROBLEM STATEMENT: Create WORKERS and DESIG table and perform the following
functions
SOURCE CODE: TABLE WORKERS
mysql> create database prac;
Database changed
SCREENSHOT:
SCREENSHOT OF TABLE:
26 | P a g e
SOURCE CODE: TABLE DESIG
mysql> create table DESIG(W_ID int(20),SALARY int(20), BENEFITS int(20), DESIGNATION varchar(20));
27 | P a g e
SCREENSHOT:
SCREENSHOT OF TABLE:
1. To display W_ID, Firstname, Address and city of all employees living in New York
from table workers.
SOURCE CODE:
mysql> select W_ID,FIRSTNAME,ADDRESS,CITY from WORKERS where CITY="New York";
SCREENSHOT:
28 | P a g e
OUTPUT:
SOURCE CODE:
mysql> select * from WORKERS order by LASTNAME;
SCREENSHOT:
OUTPUT:
3. To display the firstname, Lastname and Total salary of all clerks from table
WORKERSand DESIG where Total Salary is calculated as salary+benefits.
SOURCE CODE:
mysql> select FIRSTNAME,LASTNAME,SALARY+BENEFITS from WORKERS,DESIG where
DESIGNATION="Clerk";
SCREENSHOT:
29 | P a g e
OUTPUT:
4.To display the minimum salary among managers and clerks from table DESIG
SOURCE CODE:
mysql> select max(SALARY) from DESIG where DESIGNATION="Manager" or
DESIGNATION="Clerk";
SCREENSHOT:
OUTPUT:
5.To display the Firstname, salary where designation is of managers and clerks from
table DESIG and WORKERS.
SOURCE CODE:
mysql> select FIRSTNAME,SALARY from WORKERS,DESIG where DESIGNATION = "Manager"and
WORKERS.W_ID=DESIG.W_ID;
30 | P a g e
SCREENSHOT:
OUTPUT:
SOURCE CODE:
mysql> select count(DISTINCT DESIGNATION)from DESIG;
SCREENSHOT:
OUTPUT:
31 | P a g e
7.To display DESIGNATION, Sum(SALARY) from table DESIG grouped by DESIGNATION
having less than three frequency
SOURCE CODE:
mysql> select DESIGNATION, Sum(SALARY)from DESIG group by DESIGNATION HAVING
COUNT(*)<3;
SCREENSHOT:
OUTPUT:
8.To display the sum of benefits of the attributes having designation as salesman.
SOURCE CODE:
mysql> select Sum(BENEFITS)from DESIG where DESIGNATION="Salesman";
SCREENSHOT:
OUTPUT:
32 | P a g e
PROBLEM NUMBER: 6
PROBLEM STATEMENT: Consider the following MOVIE table and write the SQL queries
based on it.
SOURCE CODE: TABLE MOVIE
create table MOVIE(Movie_ID varchar(5),MovieName text(20),type text(15),ReleaseDate text(20),ProductionCost
int(15),BusinessCost int(15));
SCREENSHOT:
33 | P a g e
SCREENSHOT OF TABLE:
SOURCE CODE:
SCREENSHOT:
34 | P a g e
2. Display movie id, movie name, total_earning by showing the business done by the
movies. Calculate the business done by movie using the sum of production cost
and business cost.
SOURCE CODE:
mysql> select Movie_ID,MovieName,ProductionCost + BusinessCost "Total Earning" from MOVIE;
SCREENSHOT:
3. Display movie id, movie name and production cost for all the movies with greater
than 150000 and less than 1000000.
SOURCE CODE:
mysql> select Movie_ID,MovieName,ProductionCost from MOVIE where ProductionCost<1000000 and
ProductionCost>150000;
SCREENSHOT:
35 | P a g e
4. Display the movie of type action and romance.
SOURCE CODE:
mysql> select MovieName from MOVIE where type='Action' or type='Romance';
SCREENSHOT:
SCREENSHOT:
36 | P a g e
6. Write a query to display cube of 5.
SOURCE CODE:
mysql> select pow(5,3);
SCREENSHOT:
7.Write a query to display the number 563.854741 rounding off to the next hundred.
SOURCE CODE:
mysql> select round(563.854741,-2);
SCREENSHOT:
SCREENSHOT:
37 | P a g e
9.Write a query to display today’s date into DD.MM.YYYY fromat.
SOURCE CODE:
mysql> select concat(day(now()),concat('.',month(now()),concat('.',year(now())))) "Date";
SCREENSHOT:
SCREENSHOT:
SCREENSHOT:
38 | P a g e
12. Write a query to display first four digits of production cost.
SOURCE CODE:
mysql> select left(ProductionCost,4) from MOVIE;
SCREENSHOT:
SOURCE CODE:
mysql> select right(BusinessCost,4) from MOVIE;
SCREENSHOT:
39 | P a g e
SCREENSHOT:
SCREENSHOT:
40 | P a g e
PROBLEM NUMBER: 7
PROBLEM STATEMENT: Suppose your school management has decided to conduct
matches between students of class XI & XII. Students of each class are asked to join
any one of the four teams- Team Titans, Team Rockers, Team Magnet, Team
Magnet. During summer vacations, various matches will be conducted between
these teams help your sports teacher to do the following:
SCREENSHOT:
mysql> 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));
41 | P a g e
mysql> insert into match_details values("M1","2021-12-20",1,2,107,93);
SCREENSHOT:
42 | P a g e
SCREENSHOT:
1. Create a table “Team” and show the contents using DML statements.
Structure of table:
SOURCE CODE:
mysql> desc team;
SCREENSHOT:
SCREENSHOT:
43 | P a g e
Showing the table details:
SOURCE CODE:
mysql> desc team;
SCREENSHOT:
2. Create another table “Match_Details” and insert show the inserted data.
SOURCE CODE:
mysql> 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));
SCREENSHOT:
44 | P a g e
PROGRAM NUMBER :8
PROGRAM STATEMENT:
Write a MySQL connectivity program in Python to
a) Create a database school
b) Create a table students with the specifications-ROLLNO integer, STNAME
character(10) in MySQL and perform the following operations:
i) Insert two records in it
ii) Display the contents of the table
SOURCE CODE:
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",password="suchi",database="school")
c=db.cursor()
def c_database( ):
try:
c.execute("use {}".format(dn))
except Exception as a:
print("Database Error",a)
def d_database():
try:
except Exception as a:
def c_table():
try:
45 | P a g e
#c.execute("use {}".format(dn))
except Exception as a:
def e_data():
try:
while True:
# c.execute("use {}".format(dn))
db.commit()
if choice in "Nn":
break
except Exception as a:
def d_data():
try:
data=c.fetchall()
for i in data:
print(i)
except Exception as a:
while True:
print("MENU\n1. Create Database\n2. Drop Database\n3.Create Table\n4. Insert Record\n5. Display Entire
Data\n6.Exit")
if choice==1:
46 | P a g e
c_database()
d_database()
elif choice==3:
c_table()
elif choice==4:
e_data()
elif choice==5:
d_data()
elif choice==6:
break
else:
47 | P a g e
OUTPUT:
MENU
1. Create Database
2. Drop Database
3.Create Table
4. Insert Record
6.Exit
MENU
1. Create Database
2. Drop Database
3.Create Table
4. Insert Record
6.Exit
MENU
1. Create Database
48 | P a g e
2. Drop Database
3.Create Table
4. Insert Record
6.Exit
MENU
1. Create Database
2. Drop Database
3.Create Table
4. Insert Record
6.Exit
MENU
1. Create Database
2. Drop Database
3.Create Table
4. Insert Record
6.Exit
49 | P a g e
Do you want to add more record <y/n>=y
SCREENSHOT OF OUTPUT:
50 | P a g e
PROGRAM NUMBER:9
PROGRAM STATEMENT:
Perform all the operations with reference to table ‘students’ through MySQL-Python
connectivity.
SOURCE CODE:
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="suchi",database="school")
cn=db.cursor()
def insert_rec():
try:
while True:
sname=input("Enter name:")
marks=float(input("enter marks:"))
gr=input("enter grade:")
db.commit()
if ch in 'Nn':
break
except Exception as e:
print("error",e)
def update_rec():
try:
gr=input("Enter Grade:")
db.commit()
print("Update succesfully")
except Exception as e:
print("Error",e)
51 | P a g e
def delete_rec():
try:
db.commit()
print("DElete successful")
except Exception as e:
print("Error",e)
def view_rec():
try:
data=cn.fetchall()
for i in data:
print(i)
except Exception as a:
while True:
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:
52 | P a g e
SCREENSHOT OF SOURCE CODE:
OUTPUT:
MENU
1.Insert Record
2.Update Record
3.Delete Record
4.Display Record
5.Exit
Enter name:Rik
enter marks:90
enter grade:A
53 | P a g e
Enter roll number:2
Enter name:Rita
enter marks:70
enter grade:B
Enter name:Sita
enter marks:95
enter grade:A
Enter name:Ron
enter marks:75
enter grade:B
Enter name:Ayan
enter marks:33
enter grade:F
MENU
1.Insert Record
2.Update Record
3.Delete Record
4.Display Record
5.Exit
54 | P a g e
SCREENSHOT OF OUTPUT:
55 | P a g e
PROGRAM NUMBER:10
PROGRAM STATEMENT:
Write a program in python by using the ORDER BY statement to
sort the result in ascending or descending order.
SOURCE CODE:
import mysql.connector
mydb =mysql.connector.connect(host="localhost",user="root",password="suchi",database="school")
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
OUTPUT:
(1, 'arnab', 39.8, 'a')
SCREENSHOT OF OUTPUT:
56 | P a g e
PROGRAM NUMBER:11
PROGRAM STATEMENT:
Write a program in python to limit the number of records
returned from the sql query, by using the "LIMIT" statement:
SOURCE CODE:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",user="root",password="suchi",database="school")
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
OUTPUT:
(1, 'suchi', 100.0, 'a')
SCREENSHOT OF OUTPUT:
57 | P a g e