Practical Imp Questions Class 12
Practical Imp Questions Class 12
Table Creation:
Create table Teacher(Ti d int(3) primary key, TName varchar(20),
Post varchar(5), subject varchar(15),
Salary double(7,2), Dt_Join date);
Record Insertion:
Insert into Teacher values(101,’Mr. R K Sharma’,’TGT’,’Science’,45000, '2018-04-01');
Insert into Teacher values(102,'Mr. Shiv Sahai',’TGT’,’Hindi’,43000, '2018-04-01');
Insert into Teacher values(103,'Ms. Rama', ‘PRT’,’English’,33000, '2018-08-11');
Insert into Teacher values(104,'Ms. Preeti', ‘PGT’,’IP’,76000, '2018-01-07');
Insert into Teacher values(105,'Ms. Nidhi', ‘PGT’,’IP’,80000, '2018-08-11');
Insert into Teacher values(106,'Mr. P K Khanna',’TGT’,’Science’, 47000, '2018-01-07');
a) Count and display how many male teachers are there as TGT.
Select count(*) from Teacher where Post='TGT' and Tname like 'Mr.%';
b) Display number of teachers and their average salary for each post category(But only
for PGT and TGT"
Select post, count(*) as "No. of Teachers", avg(Salary), from Teacher group by Post
having
Post='TGT' or post='PGT';
OR
Select post, count(*) as "No. of Teachers", avg(Salary), from Teacher group by Post
having
Post in('TGT','PGT');
e) Display detail of Teachers Seniority wise according to date of joining ( Sr. to Jr.).
Select * from Teacher order by Dt_join asc;
#a) Display Teacher Name,subject and Salary for all TGT teachers.
print(df.loc[(df.Post=='TGT'),['TName','Subject','Salary']])
OR
print(df.loc[(df['Post']=='TGT'),['TName','Subject','Salary']])
#b) Add a Row for a PGT teacher.(Take your own Data). Print the data frame now.
df.loc[107]=['Mr. K K Singh','PGT','English',78000,'2018-01-07']
print(df)
#c) Create a CSV File from the above data frame named Teacher.csv.
df.to_csv("Teacher.csv",index=False)
print("Data Transferred to csv file successfully.")
#d) Add a new column Bonus which is 15% of their Salary. Print the data frame now.
df['Bonus']=df['Salary']*0.15
print(df)
OR
df['Bonus']=df.Salary*0.15
print(df)
# e) Now plot a multi-line chart depicting the Teacher Name on x-axis and their corresponding Salary
and Bonus
# on y-axis, with appropriate Graph title, x-axis title, y-axis title, gridlines, legends and color etc.
x=df['TName']
y1=df['Salary']
y2=df['Bonus']
plt.plot(x,y1,color='r',label='Salary',linestyle='dashdot',linewidth=2,marker='o',markersize=8,markeredg
ecolor='m')
plt.plot(x,y2,color='b',label='Bonus',linestyle='dashed',linewidth=2,marker='^',markersize=8,markeredge
color='m')
plt.xlabel('Teacher Name-->',fontsize=12,color='b')
plt.ylabel('Salary & Bonus(INR)->',fontsize=12,color='b')
plt.legend()
plt.title('ABC Public School\nTeacher Wise Salary Analysis', fontsize=14, color='r')
plt.grid()
plt.show()
Python Pandas and Data Visualization
Q.1- Write a program in python to create the following dataframe named “df” storing the
following details:
Country Population BirthRate UpdateDate
0 China 1379750000 12.00 2016-08-11
1 India 1330780000 21.76 2016-08-11
2 United States 324882000 13.21 2016-08-11
3 Indonesia 260581000 18.84 2016-01-07
4 Brazil 206918000 18.43 2016-08-11
5 Pakistan 194754000 27.62 2016-08-11
Considering the above dataframe answer the following queries by writing appropriate
command in python pandas.
a) Display complete data for China and India.
b) Display Country, Population and BirthRate of Brazil and Pakistan.
c) Create a CSV File from the above data frame.
d) Retrieve data from above created csv file into a new dataframe df1 with columns
Country and Population only .
e) Now plot a bar chart depicting the Country on x-axis and their corresponding
Population on y-axis, with appropriate Graph title, x-axis title, y-axis title, gridlines and
color etc.
Table Creation:
Create table pop(Country varchar(20) primary key,
Population bigint(13), BirthRate double(5,2), UpdateDate date);
Record Insertion:
Insert into pop values('China',1379750000,12.00, '2016-08-11');
Insert into pop values('India',1330780000,21.76, '2016-08-11');
Insert into pop values('United States', 324882000,13.21, '2016-08-11');
Insert into pop values('Indonesia', 260581000,18.84, '2016-01-07');
Insert into pop values('Brazil', 206918000,18.43, '2016-08-11');
Insert into pop values('Pakistan', 194754000,27.62, '2016-08-11');
a) Display those records whose population lies in the range 1300000000 and
140000000.
Select * from pop where population between 1300000000 and 140000000;
OR
Select * from pop where population>=1300000000 and population<=140000000;
print(df[(df.Country=='India')|(df.Country=='China')])
OR
print(df.loc[(df.Country=='India')|(df.Country=='China')])
b) Display Country, Population and BirthRate of Brazil and Pakistan.
print(df.loc[(df.Country=='Brazil')|(df.Country=='Pakistan'),['Country','Population','BirthRate']])
df.to_csv("Population.csv",index=False)
print("Data Transferred to csv file successfully.")
d) Retrieve data from above created csv file into a new dataframe df1 with columns Country and
Population only .
df1=pd.read_csv("Population.csv",usecols=['Country','Population'])
print(df1)
e) Now plot a bar chart depicting the Country on x-axis and their corresponding Population on y-axis,
with appropriate Graph title, x-axis title, y-axis title, gridlines and color etc.
x=df['Country']
y=df['Population']
plt.bar(x,y,color='g')
plt.xlabel('Country-->',fontsize=12,color='r')
plt.ylabel('Population(In Billion)->',fontsize=12,color='r')
plt.title('Country Wise Population Analysis', fontsize=14, color='g')
plt.grid()
plt.show()
x=emp['salary']*0.15
# Add a new column named Bonus ( just after salary column) which is 15% of their salary.
emp.insert(3,'bonus',value=x)
print(emp)
# Display the employee name, job and salary for all those employees whose
# salary lies between 30000 and 50000.
print(emp.loc[(emp.salary>=30000) & (emp.salary<=50000),['ename','job','salary']])
#Now plot a bar chart depicting the employee name on x-axis and their corresponding
#salary on y-axis, with appropriate Graph title, x-axis title, y-axis title,
#gridlines and color etc.
x=emp['ename']
y=emp['salary']
plt.bar(x,y,color='r')
plt.xlabel('Name-->',fontsize=12,color='g')
plt.ylabel('Salary (Rs.)-->',fontsize=12,color='g')
plt.title('ABC PVT Limited\n Employees Salary analysis', fontsize=14, color='r')
plt.grid()
plt.show()
OUTPUT:
Database creation:
Create database boardexam2021;
Use boardexam2021;
Table Creation:
Create table weather(Cid char(3) , City varchar(15),
MaxTemp double(3,1), MinTemp double(3,1),
Rainfall double(3,1));
Record Insertion:
Insert into weather values('C01','Delhi',40.0,32.0,24.1);
Insert into weather values('C02','Bengaluru',31.0,32.0,36.2);
Insert into weather values('C03','Chennai',35.0,27.0,40.8);
Insert into weather values('C04','Mumbai',29.0,21.0,35.2);
Insert into weather values('C05','Kolkata',39.0,23.0,41.8);
Insert into weather values('C06','Allahabad',41.0,30.0,32.4);
a) Add a column Zone and update this with values - North, South, South, West, East,
North.
Alter table weather add column Zone varchar(5);
Update weather set Zone='North' where Cid='C01';
Update weather set Zone='South' where Cid='C02';
Update weather set Zone='South' where Cid='C03';
Update weather set Zone='West' where Cid='C04';
Update weather set Zone='East' where Cid='C05';
Update weather set Zone='North' where Cid='C06';
d) Display City and Rainfall whose rainfall is in the range 30 and 40.
Select City, Rainfall from weather where rainfall between 30 and 40;
Select City, Rainfall from weather where rainfall >=30 and rainfall<=40;
#c) Create a CSV File from the above data frame named weather.csv.
temp.to_csv("weather.csv",index=False)
print("Data Transferred to csv file successfully.")
#d) Change the column name MaxTemp to MxT and MinTemp to MnT.
temp=temp.rename(columns={'MaxTemp':'MxT', 'MinTemp':'MnT'})
print(temp)
# e) Now plot a multi-bar chart depicting the City Name on x-axis and their corresponding MaxTemp
and MinTemp # on y-axis, with appropriate Graph title, x-axis title, y-axis title, gridlines, legends and
color etc
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
city=temp['City']
y1=temp['MxT']
y2=temp['MnT']
X=np.arange(len(city))
plt.bar(city,y1,color='r',label='Max. Temp',width=0.25)
plt.bar(X+0.25,y2,color='b',label='Min. Temp',width=0.25)
plt.xlabel('City-->',fontsize=12,color='b')
plt.ylabel('Max. vs Min Temperature-->',fontsize=12,color='b')
plt.legend()
plt.title('Temperature Analysis', fontsize=14, color='r')
plt.grid()
plt.show()
Python Pandas and Data Visualization
Q.1- Write a program in python to create the following dataframe named “emp” storing the details
of employees:
ename job salary dojoin department
1001 Scott Manager 90000 2010-10-01 Accounts
1002 Tom Clerk 34000 2010-01-04 Admin
1003 Joy Clerk 32000 2009-03-01 Admin
1004 Sam Salesman 40000 2009-04-01 Sales
1005 Martin Manager 85000 2008-08-05 Sales
1006 Michel Salesman 43000 2008-08-06 Sales
1007 Francis Clerk 30000 2009-10-10 Accounts
Write a menu based program to perform the following operations on columns of the above
defined dataframe based on user specified inputs:
Coding-
Output –
Que) Consider the table STUDENT given below, write MySQL Commands .
TABLE: STUDENT
Name Class DOB Sex City Marks
Nanda X 1995-06-06 M Agra 451
Saurabh XII 1993-07-05 M Mumbai 462
Sanal XI 1994-05-06 F Delhi 400
Trisla XII 1995-08-08 F Mumbai 450
Steve XII 1995-10-08 M Delhi 369
Marisla XI 1994-12-12 F Dubai 250
Neha X 1995-12-08 F Moscow 377
Nishant X 1995-12-06 M Moscow 489
(i) To display all the information of males whose city is NEITHER Delhi or Mumbai.
select * from student where sex='M' and city not in('Delhi','Mumbai');
select * from student where sex='M' and city!='Delhi' and city!='Mumbai';
select * from student where sex='M' and not(city='Delhi' or city='Mumbai');
(ii) To display the details of all the students whose date of birth is after Nishant’s Birth date
1995-12-06. (consider the Sql’s standard date format)
select * from student where dob>'1995-12-06';
(iii) To Display all information about class XII students rankwise (descending order).
select * from student where class='XII' order by marks desc;
(iv) List names of all students whose name has the character ‘a’.
select name from student where name like '%a%';
(v) Display Name and Marks of those students whose marks is in the range 400 and 500 (both
are inclusive)
select name, marks from student where marks between 400 and 500;
select name, marks from student where marks>=400 and marks<=500;
Output -
Que) Consider the table STUDENT given below, write MySQL Commands. 7
TABLE: STUDENT
(i) Display average marks, highest marks and total no. of students for each class.
select class, avg(marks), max(marks), count(*) from student group by class;
(ii) Display total no of males and females separately, along with their average marks.
select sex, count(*), avg(marks) from student group by sex;
(iii) Increase the marks of 10th class students by 5% marks.
update student set marks=marks +(marks*0.05) where class='X';
(iv) Add a new column named Address. ( Take the data type and size yourself)
alter table student add address varchar(30);
Output -
PROBLEM DEFINITION:
Write a Python program to create a
DataFrame as given below, and display all
its attributes ( index, columns, shape,
size, axes, rows and columns.
Coding-
Output-
Q.1)
Output -
Que) Consider the table STUDENT given below, write MySQL Commands .
TABLE: STUDENT
Name Class DOB Sex City Marks
Nanda X 1995-06-06 M Agra 474
Saurabh XII 1993-07-05 M Mumbai 462
Sanal XI 1994-05-06 F Delhi 400
Trisla XII 1995-08-08 F Mumbai 450
Steve XII 1995-10-08 M Delhi 369
Marisla XI 1994-12-12 F Dubai 250
Neha X 1995-12-08 F Moscow 396
Nishant X 1995-12-06 M Moscow 513
(i) Display name, class and city of 'XII' class students who lives in 'Mumbai'.
select Name, Class City from student where class=’XII’ and City=’Mumbai’;
(ii) Display name, data of birth of all females, whose marks is greater than or equal to 400.
select Name,DOB from student where Sex=’F’ and Marks>=400;
(iii) Display City and highest marks of students of each city, but only for Delhi and Mumbai.
select City, Max(Marks) from student group by City having City in(‘Delhi’, ‘ Mumbai’);
(iv) Display round of average marks up to zero places, truncate of average marks up to 1 decimal
place for class 'X' students only.
select round(avg(Marks),0) , truncate(avg(Marks),1) from student where Class=’X’;
(v) To display name and the position of character 'a' in the names of students having 'a' anywhere
in their names.
select name instr(name, ’a’) from student name like ‘%a%’;
Que) Consider the table pharma given below, write MySQL Commands.
Table pharma
(iv) To display the Drug ID, DrugName and Pharmacy Name of all the records in descending order of
their price.
select DurgID, DurgName, PharmacyName from pharma order by price desc;
Que) Consider the table STUDENT given below, write MySQL Commands .
TABLE: STUDENT
Name Class DOB Sex City Marks
Nanda X 1995-06-06 M Agra 474
Saurabh XII 1993-07-05 M Mumbai 462
Sanal XI 1994-05-06 F Delhi 400
Trisla XII 1995-08-08 F Mumbai 450
Steve XII 1995-10-08 M Delhi 369
Marisla XI 1994-12-12 F Dubai 250
Neha X 1995-12-08 F Moscow 396
Nishant X 1995-12-06 M Moscow 513
(i) Display name, character number 3rd and 4th of each name and counting of number of
characters in each name.
select Name substr(Name,3,2) ,length(name) from student;
(ii) Display current date and current time together.
select curdate() as “Current date” , curtime() as “Current time”;
(iii) Display name, marks and round up the Square root of marks up to 2 decimal places.
select Name , Marks , round (sqrt(Marks),2) from student;
(iv) Increase the marks of 10th class students by 5% marks.
update student set marks=marks +(marks*0.05) where class='X';
(v) Display all information about class XII students rank wise.
select * from student where Class=’XII’ order by Marks desc;