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

Mysql

Uploaded by

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

Mysql

Uploaded by

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

Part B

SQL QUERIES
Answer the following questions according to given tables:
T_id Name Age Department Doj Salary Gender Adhar_no
1 Jugal gupta 34 CS 2017-01-10 12000 M 987655
2 Sharmilla 31 History 2008-03-24 20000 F 998877
3 Sandeep 32 Maths Null 30000 M 887766
4 Sangeeta 35 History 2015-07-01 40000 F 776655
5 Rakesh gupta 42 Maths 2007-09-05 25000 M Null
6 Shyam singh 50 History Null 30000 M 554433
7 Shiv om 44 CS 2017-02-25 21000 M 443322
8 Shalakha 33 Maths 2018-07-31 20000 F 332211
9 Shyam Kumar 35 Maths 2016-09-24 40000 M 221100
10 Moahn kumar 30 Maths 2016-09-24 25000 M 110022
Table: Teacher

P_id Department Place


1 History Agra
2 Maths Raipur
3 CS Delhi
Table: posting, P_id

Set1: ALTER table to add new attributes / modify data type / drop attribute
1) To Add new column “City” in teacher table.

Ans: alter table teacher add city varchar(20);


2) To rename column “City” to “T_City” in teacher table.
Ans: alter table teacher change city T_city varchar(30);

3) To delete “City” column from teacher table.


Ans: alter table teacher drop T_city;
4) To delete primary key constraint from from T_id column.
Ans: alter table teacher drop primary key;

5) To add primary key constraint to T_id column.


Ans: alter table teacher add primary key(t_id);
Set2: Conditions Based on Pattern – LIKE operators

6) To display name, age and salary of those teacher who’s name start with
‘S’.
Ans: select name,age,salary from teacher where name like "s%";

7) To display name, age and salary of those teacher who’s name ends with
‘a’.
Ans: select name,age,salary from teacher where name like "%a";

8) To display name, age and salary of those teacher who’s name contains letter ‘a’ at the second
place.
Ans: select name,age,salary from teacher where name like "_a%";
9) To display name, age and salary of those teacher who’s name not having 7
character.
Ans: select name,age,salary from teacher where name like "_ _ _ _ _ _ _ ";

10) To display the number of all teacher whose name ends with ‘Kumar’.
Ans: select name,age,salary from teacher where name like "%kumar";

Set3: Insert into and Update Queries


11) Write query to add new data in T_id, name, department, salary column in ‘teacher’
table.
Ans: insert into teacher (t_id, name, department, salary) values (12, 'Arun kumar',
'CS',25000);
12) Write query to change department of all male teacher to ‘History’.
Ans: update teacher set department="History" where gender="M";

13) Query to increase salary of all Maths teacher by 5000.


Ans: update teacher set salary=salary+5000 where department="Maths";
14)Query to delete record of those teacher whose salary is NULL.
Ans: delete from teacher where salary is NULL;

Set4: ORDER By to display data in ascending / descending


order
15) To display all record teacher table name-wise.
Ans: select * from teacher order by name;
16)To display all record of teacher table in decreasing order age-wise.
Ans: select * from teacher order by name DESC;

17) To display sum of salary department wise in teacher table.


Ans: select department,count(salary) from teacher group by department;

18) To display maximum salary gender wise or According to gender.


Ans: select gender,max(salary) from teacher group by gender;
19)To display minimum, maximum, sum, average of salary department wise if number
of teacher in department in more than 2.
Ans: select department,min(salary),max(salary),sum(salary),avg(salary) from
teacher group by department;

Set5: Queries based on joining of two tables


20) Write query to join teacher and posting table on the basic of department columns
in both table.
Ans: select * from teacher natural join posting;

21) Write query to display name and place of all teacher belongs to delhi.
Ans: select name,place from teacher natural join posting where place='delhi';
22) Write query to display number of teachers city-wise.
Ans: select place,count(place) from teacher natural join posting group by place;

23) Write query to display number of teachers city-wise. Order by city name.
Ans: select place,count(place) from teacher natural join posting group by place
order by place;

24) Write query to display number of teachers city-wise if in each city having more
than 3 teachers order by city name.
Ans: select place,count(place) from teacher natural join posting group by place
having count(place)>3;
Part C

PROGRAMS
BASED ON
PYTHON-SQL
CONNECTIVITY
Q1: Write a program to Creating database and Show database in Mysql
through Python.
Code:
import mysql.connector
mydb=mysql.connector.connect
(host="localhost",
user="root",
password="123456")
mycursor=mydb.cursor()
mycursor.execute("create database demo")
mycursor.execute("Show databases")
for i in mycursor:
print(i)
Q2: Write a program to Creating table inside any school database through
Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("create table std_details3\
(rno int primary key, name varchar(20), age int, city varchar(20))")
std=mycursor.execute("desc std_details3")
for i in mycursor:
print(i)
Q3: Write a program to Display all male teachers data from ‘teacher’ table
using fetchall() Through Python-Mysql connectivity program.

Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("select name,department from teacher where gender='m'")
myrecords=mycursor.fetchall()
for i in myrecords:
print(i)
print("Total number of rows retrieved",mycursor.rowcount)
Q4: Write a Parameterized program to insert data in std_details3 table
Through Python-Mysql connectivity program.

Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
y="y"
while y=="y":
r=int(input("Enter rollno:"))
n=input("Enter name:")
a=int(input("Enter age:"))
c=input("Enter City:")
mycursor.execute("insert into std_details3\
values({},'{}',{},'{}')".format(r,n,a,c))
mydb.commit()
print(mycursor.rowcount,"Record Inserted")
y=input("Do you want to insert more data:")
Thanks

You might also like