Mysql
Mysql
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
Set1: ALTER table to add new attributes / modify data type / drop attribute
1) To Add new column “City” in teacher table.
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";
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