You are on page 1of 78

DELHI PUBLIC SCHOOL

NERUL, NAVI MUMBAI


RECORD FILE-2024

INFORMATICS PRACTICES

Name: TOOHINA MISHRA

Board Roll No:

1
DELHI PUBLIC SCHOOL
NERUL, NAVI MUMBAI

Certificate

This is to certify that this record file is the

work of Toohina Mishra of Class XII, Board

Roll Number.

She has satisfactorily completed the

required number of practicals as per the

guidelines of the Central Board of

Secondary Education for AISSCE-2024

practical examination.

____________ __________ __________ _____

Internal External Principal School


Seal
Examiner Examiner

2
INDEX

Sno. Program Date PageNo. Sign

1. CREATION OF SERIES EMPLOYEE USING LIST AND


DICTIONARY 04-04-23 6-7

2. CREATION OF SERIES DATA USING RANGE


04-04-23 8-9

3. SERIES OBJECT FROM SCALAR VALUE


04-04-23 10-11

4. SERIES OBJECT FROM TUPLE


04-04-23 12-13

5. ATTRIBUTES OF SERIES
04-04-23 14-15

6. DIFFERENT OPERATIONS ON SERIES EMPLOYEE


04-04-23 16-17

7.
ARITHMETIC OPERATIONS ON SERIES 04-04-23 18-19

8.
VECTOR OPERATIONS ON SERIES 04-04-23 20-21

9.
CREATING SERIES DATA NDARRAY 04-04-23 22-23

10.
CREATION OF DATAFRAME USING DIFFERENT 16-04-23 24-26

3
TYPES

11. ATTRIBUTES TO DISPLAY INFORMATION FROM 16-04-23 27-28


THE DATAFRAME

12. DISPLAYING THE ROWS /COLUMNS / VALUES 16-04-23 29-33


FROM DATA FRAME

13. CREATION OF DATAFRAME AND DIFFERENT 16-04-23 34-36


OPERATIONS

14.
IMPORT CSV FILE TO DATAFRAME 16-04-23 37-38

15.
EXPORT DATAFRAME TO CSV FILE 16-04-23 39-40

16.
PLOT SINGLE LINE CHART 06-05-23 41-43

DATA VISUALIZATION OF DATA FRAME USING


MULIPLE LINE CHART WITH ALL PROPERTIES
17.
06-05-23 44-45

18.
PLOT MULTIPLE LINE CHART 06-05-23 46-47

19.
MULTI BAR CHART 06-05-23 48-50

4
20.
LINE PLOT 06-05-23 51-52

21. PLOTTING OF HISTOGRAM USING EMPLOYEE 06-05-23 53-54


DATA

22. CREATE TABLE EMPLOYEE WITH CONSTRAINT 13-07-23 55-56


AND USE OF DDL AND DML COMMANDS

23. CREATE TABLE GYM AND SELECT STATEMENT 13-07-23 57-60


WITH DIFFERENT OPERATORS

24.
SQL SINGLE ROW FUNCTIONS 13-07-23 61-64

25.
SQL SINGLE ROW FUNCTIONS 14-07-23 65-68

26. GROUP BY, HAVING AND AGGREGATE 14-07-23 69-71


FUNCTIONS

27. JOINING OF TABLES AND RETRIEVEING RECORDS 17-07-23 72-75


USING EQUIJOIN

5
1. CREATE A SERIES EMPLOYEE WITH EMPLOYEE NAME AND
MONTHLY SALARY WHERE ENAME(INDEX) AND SALARY(VALUE)

Ram 10000

Anil 30000

Ganesh 15000.5

Biju 10000

Niketan 30000

a. USING LIST
b. USING DICTIONARY

CODE:

import pandas as pd
#using list
ename=['Ram','Anil','Ganesh','Biju','Niketan']
salary=[10000,30000,15000.5,10000,30000]
employee=pd.Series(salary,ename)
print(employee)

#using dictionary
e={'Ram':10000,'Anil':30000,'Ganesh':15000.5,'Biju':10000,'Niketan':300
00}
employee=pd.Series(e)

6
print(employee)

OUTPUT:

Ram 10000.0
Anil 30000.0
Ganesh 15000.5
Biju 10000.0
Niketan 30000.0
dtype: float64

7
2. CREATE A SERIES DATA WITH ODD NUMBERS BETWEEN 50 TO 100.

CODE:

import pandas as pd
data=pd.Series(range(51,100,2))
print(data)

8
OUTPUT:

0 51
1 53
2 55
3 57
4 59
5 61
6 63
7 65
8 67
9 69
10 71
11 73
12 75
13 77
14 79
15 81
16 83
17 85
18 87
19 89
20 91
21 93
22 95
9
23 97
24 99
dtype: int64

3. CREATING THE FOLLOWING SERIES OBJECT FROM SCALAR


VALUE:

101 7
111 7
121 7

CODE:

import pandas as pd
p=pd.Series(7,[101,111,121])
print(p)

10
OUTPUT:

101 7
111 7
121 7
dtype: int64

11
4. WRITE A PROGRAM TO CREATE A SERIES FROM A GIVEN TUPLE
DATA:
data=(‘1’,’Aman’,86.3,’A’).

CODE:

import pandas as pd
data=('1','Aman',86.3,'A')
a=pd.Series(data)
print(a)

12
OUTPUT:

0 1
1 Aman
2 86.3
3 A
dtype: object

13
5. DO THE FOLLOWING FOR THE ALREADY CREATED SERIES
EMPLOYEE:
1. DISPLAY THE INDEX.
2. DISPLAY THE NUMBER OF ROWS AND COLUMNS .
3. DISPLAY THE DATATYPE.
4. DISPLAY THE TOTAL NUMBER OF EMPLOYEES.
5. ASSIGN THE NAME TO THE INDEX AS ‘ENAME’.
6. ASSIGN THE NAME OF THE SERIES AS EMPLOYEE.
7. DISPLAY THE DIMENSIONS OF THE SERIES.
8. DISPLAY THE DATATYPE OF THE SERIES.
9. CHECK THE SERIES FOR MISSING DATA.
10. CHECK IF SERIES IS EMPTY OR NOT?

14
CODE:

import pandas as pd
import numpy as np
name=['Ram','Anil','Ganesh','Biju','Niketan']
salary=[10000,30000,15000.5,10000,30000]
emp=pd.Series(salary,name)
print(emp)
print(emp.index)
print(emp.size)
print(emp.dtype)
print(emp.size)
emp.index.nam='Ename'
print(emp.index.nam)
emp.nam='Employee'
print(emp.nam)
print(emp.ndim)
print(emp.hasnans)
print(emp.empty)

OUTPUT:

Ram 10000.0
Anil 30000.0
Ganesh 15000.5
Biju 10000.0
Niketan 30000.0
dtype: float64
Index(['Ram', 'Anil', 'Ganesh', 'Biju', 'Niketan'], dtype='object')
5
15
float64
5
Ename
Employee
1
False
False

6. USE THE SERIES EMPLOYEE CREATED IN Q1 TO DO THE FOLLOWING


OPERATIONS:
1. DISPLAY ALL THE EMPLOYEES,
2. DISPLAY THE ANNUAL SALARY OF EACH EMPLOYEE.
3. DISPLAY THE SALARY OF NIKETAN.
4. DISPLAY ALL THE EMPLOYEES FROM BIJU TO GANESH.
5. CHANGE THE SALARY OF ANIL TO 20000.
6. DISPLAY THE FIRST 3 ROWS.
7. DISPLAY THE LAST 6 ROWS.
16
8. DELETE THE RECORDS OF RAM.
9. DISPLAY ALL THE EMPLOYEES IN REVERSE ORDER.
10. DISPLAY THE SALARY OF ALL EMPLOYEES BIJU AFTER INCREASING
THE SALARY OF BIJU BY 10%.
11. DOUBLE THE SALARY OF ALL EMPLOYEES ANIL TO NIKETAN.
12. DISPLAY THE EMPLOYEE NAMES WHERE SALARY IS >15000.
13. SORT THE VALUES OF SERIES BY SALARY IN DESCENDING ORDER.
14. SORT THE SERIES BY EMPLOYEE NAMES IN ASCENDING ORDER.

CODE:

import pandas as pd
ename=['Ram','Anil','Ganesh','Biju','Niketan']
salary=[10000,30000,15000.5,10000,30000]
employee=pd.Series(salary,ename)
print(employee.index)
print(employee.values)
print(employee['Niketan'])
print(employee['Biju':'Ganesh':-1])
employee['Anil']=20000
print(employee.head(3))
print(employee.tail(6))
employee=employee.drop('Ram')
print(employee[: :-1])
b=employee['Biju']
employee['Biju']=b+(b*10/100)
print(employee.values)
employee['Anil':'Niketan']=employee['Anil':'Niketan']*2
print(employee[employee>15000])
print(employee.sort_values(ascending=False))
print(employee.sort_index(ascending=True))

OUTPUT:

Index(['Ram', 'Anil', 'Ganesh', 'Biju', 'Niketan'], dtype='object')


[10000. 30000. 15000.5 10000. 30000. ]
30000.0
Biju 10000.0
Ganesh 15000.5
17
dtype: float64
Ram 10000.0
Anil 20000.0
Ganesh 15000.5
dtype: float64
Ram 10000.0
Anil 20000.0
Ganesh 15000.5
Biju 10000.0
Niketan 30000.0
dtype: float64
Niketan 30000.0
Biju 10000.0
Ganesh 15000.5
Anil 20000.0
dtype: float64
[20000. 15000.5 11000. 30000. ]
Anil 40000.0
Ganesh 30001.0
Biju 22000.0
Niketan 60000.0
dtype: float64
Niketan 60000.0
Anil 40000.0
Ganesh 30001.0
Biju 22000.0
dtype: float64
Anil 40000.0
Biju 22000.0
Ganesh 30001.0
Niketan 60000.0
dtype: float64

7.CREATE FOLLOWING SERIES CLASS11 AND CLASS12 WITH DIFFERENT


STREAMS AND NUMBER OF STUDENTS:

Class11

18
Science 198

Commer 100
ce
Humanit 150
ies

Class12
Science 108

Commer 86
ce
Humanit 55
ies

FIND THE TOTAL NUMBER OF STREAMWISE STUDENTS IN CLASS 11 AND


12.

CODE:

import pandas as pd
c11={'Science':198,'Commerce':100,'Humanities':50}
c12={'Science':108,'Commerce':86,'Humanities':55}
Class11=pd.Series(c11)
Class12=pd.Series(c12)
print(Class11)
print(Class12)
print(Class11+Class12)

OUTPUT:

19
Science 198
Commerce 100
Humanities 50
dtype: int64
Science 108
Commerce 86
Humanities 55
dtype: int64
Science 306
Commerce 186
Humanities 105
dtype: int64

20
8.GIVEN THE FOLLOWING SERIES S1 AND S2:

s1
A 23

B 50

C 34

D 70

s2
a 8

B NaN

C 7

e 9

WRITE THE COMMAND AND OUTPUT TO DO THE FOLLOWING:


1. MULTIPLY BOTH THE SERIES S1 AND S2.
2. ADD BOTH THE SERIES S1 AND S2.
3. SUBTRACT BOTH THE SERIES S1 AND S2.
4. DIVIDE BOTH THE SERIES S1 AND S2.

CODE:

import pandas as pd
import numpy as np
s1=pd.Series([23,50,34,70],['A','B','C','D'])
s2=pd.Series([8,np.NaN,7,9],['a','B','C','E'])
print(s1)
print(s2)
print(s1*s2)
print(s1+s2)
print(s1-s2)
print(s1/s2)

21
OUTPUT:
A 23
B 50
C 34
D 70
dtype: int64
a 8.0
B NaN
C 7.0
E 9.0
dtype: float64
A NaN
B NaN
C 238.0
D NaN
E NaN
a NaN
dtype: float64
A NaN
B NaN
C 41.0
D NaN
E NaN
a NaN
dtype: float64
A NaN
B NaN
C 27.0
D NaN
E NaN
a NaN
dtype: float64
A NaN
B NaN
C 4.857143
D NaN
E NaN
a NaN
dtype: float64
22
9.CREATE A SERIES DATA USING NDARRAY THAT HAS 10 ELEMENTS IN
THE RANGE 10 TO 100.

CODE:

import pandas as pd
import numpy as np
a=np.arange(10,110,10)
data=pd.Series(a)
print(data)

23
OUTPUT:

0 10
1 20
2 30
3 40
4 50
5 60
6 70
7 80
8 90
9 100
dtype: int32

24
10.CREATE AND DISPLAY THE FOLLOWING DATAFRAME EMPLOYEE
USING:
A. LIST
B. DICTIONARY
C. DICTIONARY OF SERIES
D. 2D DICTIONARY

CODE:

import pandas as pd
#using list
a=[[1001,'Ram', 10000,'10-10-2000'],[1002,'Anil',30000,'03-20-2001'],
[1003,'Ganesh',15000.5,'12-05-2002'],[1004,'Biju',10000,'09-10-2000'],
[1005,'Niketan',30000,'09-10-2000'],[1006,'Praful',15000.5,'10-10-2000'],
[1007,'Santosh',10000,'03-20-2001'],[1008,'Anant',30000,'12-05-2002'],
[1009,'Laxmi',15000.5,'09-10-2000'],[1010,'Gaurav',20000,'08-08-1998']]
employee=pd.DataFrame(a,columns=['eno','ename','salary','doj']
)
print(employee)

#using 2D dictionary
eno=[1001,1002,1003,1004,1005,1006,1007,1008,1009,1010]
ename=['Ram','Anil','Ganesh','Biju','Niketan','Praful','Santosh','An
ant','Laxmi','Gaurav']
salary=[10000,30000,15000.5,10000,30000,15000.5,10000,30000,15000.5,2
0000]
doj=['10-10-2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998','10-10-
2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998']
a={'eno':eno,'ename':ename,'salary':salary,'doj':doj}
employee=pd.DataFrame(a)

25
print(employee)

#using dictionary of series


eno=pd.Series([1001,1002,1003,1004,1005,1006,1007,1008,1009,1010])
ename=pd.Series(['Ram','Anil','Ganesh','Biju','Niketan','Praful','Sa
ntosh','Anant','Laxmi','Gaurav')
salary=pd.Series([10000,30000,15000.5,10000,30000,15000.5,10000,3000
0,15000.5,20000])
doj=pd.Series(['10-10-2000','03-20-2001','12-05-2002','09-10-2000','08-08-
1998',
'10-10-2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998'])
a={'eno':eno,'ename':ename,'salary':salary,'doj':doj}
employee=pd.DataFrame(a)
print(employee)

#using dictionary
data={'eno':{'01':1001,'02':1002,'03':1003,'04':1004,'05':1005,'06':1006,'07':100
7,
'08':1008,'09':1009,'010':1010},
'ename':{'01':'Ram','02':'Anil','03':'Ganesh','04':'Biju','05':'Niketan','06':'P
raful',
'07':'Santosh','08':'Anant','09':'Laxmi','010':'Gaurav'}, 'salary'
:{'01':10000,'02':30000,'03':15000.5,'04':10000,'05':30000,'06':15000.5,
'07':10000,'08':30000,'09':15000.5,'010':20000},
'doj':{'01':'10-10-2000','02':'03-20-2001','03':'12-05-2002','04':'09-10-2000',
'05':'08-08-1998','06':'10-10-2000','07':'03-20-2001','08':'12-05-2002',
'09':'09-10-2000','010':'08-08-1998'}}
employee=pd.DataFrame(data)
print(employee)

26
OUTPUT:

eno ename salary doj


0 1001 Ram 10000.0 10-10-2000
1 1002 Anil 30000.0 03-20-2001
2 1003 Ganesh 15000.5 12-05-2002
3 1004 Biju 10000.0 09-10-2000
4 1005 Niketan 30000.0 09-10-2000
5 1006 Praful 15000.5 10-10-2000
6 1007 Santosh 10000.0 03-20-2001
7 1008 Anant 30000.0 12-05-2002
8 1009 Laxmi 15000.5 09-10-2000
9 1010 Gaurav 20000.0 08-08-1998

27
11.CONSIDER THE DATAFRAME EMPLOYEE CREATED IN QUESTION
NUMBER 10 AND DO THE FOLLOWING:
A. THE ROW LABELS OF THE DATAFRAME.
B. THE COLUMN LABELS OF THE DATAFRAME.
C. THE AXES OF THE DATAFRAME.
D. THE DATATYPES OF THE DATAFRAME.
E. THE NUMBER OF ROWS AND COLUMNS OF THE
DATAFRAME.
F. TRANSPOSE THE DATAFRAME.
G. THE NUMPY REPRESENTATION OF THE DATAFRAME.
H. CHECK AND DISPLAY WHETHER THE DATAFRAME IS
EMPTY OR NOT.

CODE:

import pandas as pd
eno=[1001,1002,1003,1004,1005,1006,1007,1008,1009,1010]
ename=['Ram','Anil','Ganesh','Biju','Niketan','Praful','Santosh','An
ant','Laxmi','Gaurav']
salary=[10000,30000,15000.5,10000,30000,15000.5,10000,30000,15000.5,2
0000]
doj=['10-10-2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998','10-10-
2000',
'03-20-2001','12-05-2002','09-10-2000','08-08-1998']
a={'eno':eno,'ename':ename,'salary':salary,'doj':doj}
employee=pd.DataFrame(a)
print(employee.index)
print(employee.columns)
print(employee.axes)
print(employee.dtypes)
print(employee.shape)
print(employee.T)
print(employee.values)
print(employee.empty)

28
OUTPUT:

RangeIndex(start=0, stop=10, step=1)


Index(['eno', 'ename', 'salary', 'doj'], dtype='object')
[RangeIndex(start=0, stop=10, step=1), Index(['eno', 'ename',
'salary', 'doj'], dtype='object')]
eno int64
ename object
salary float64
doj object
dtype: object
(10, 4)
0 1 2 ... 7 8 9
eno 1001 1002 1003 ... 1008 1009 1010
ename Ram Anil Ganesh ... Anant Laxmi
Gaurav
salary 10000.0 30000.0 15000.5 ... 30000.0 15000.5
20000.0
doj 10-10-2000 03-20-2001 12-05-2002 ... 12-05-2002 09-10-2000 08-
08-1998

[4 rows x 10 columns]
[[1001 'Ram' 10000.0 '10-10-2000']
[1002 'Anil' 30000.0 '03-20-2001']
[1003 'Ganesh' 15000.5 '12-05-2002']
[1004 'Biju' 10000.0 '09-10-2000']
[1005 'Niketan' 30000.0 '08-08-1998']
[1006 'Praful' 15000.5 '10-10-2000']
[1007 'Santosh' 10000.0 '03-20-2001']
[1008 'Anant' 30000.0 '12-05-2002']
[1009 'Laxmi' 15000.5 '09-10-2000']
29
[1010 'Gaurav' 20000.0 '08-08-1998']]
False

12.CONSIDER THE DATAFRAME EMPLOYEE CREATED IN QUESTION


NUMBER 10 AND DO THE FOLLOWING:
A. CHANGE THE INDEX TO ENAME.
B. ALL ROWS AND COLUMNS.
C. FIRST 4 ROWS.
D. LAST 5 ROW.
E. ALTERNATE ROWS.
F. ROWS IN REVERSE ORDER.
G. DISPLAY ENO FROM THE DATAFRAME.
H. DISPLAY ENO AND SALARY FROM THE DATAFRAME.
I. DISPLAY THE RECORD OF EMPLOYEE LAXMI.
J. DISPLAY THE RECORD OF EMPLOYEES ANIL TO NIKETAN.
K. DISPLAY THE COLUMNS ENO TO DOJ.
L. DISPLAY THE RECORDS OF RAM, NIKETAN AND GAURAV
WITH ENO AND DOJ.
M. DISPLAY ROWS 2ND TO 4TH (BOTH INCLUSIVE).
N. DISPLAY THE ENO AND SALARY OF ROW 1 TO 5 ONLY.
O. DISPLAY THE SALARY OF NIKETAN.

CODE:

import pandas as pd
eno=[1001,1002,1003,1004,1005,1006,1007,1008,1009,1010]
ename=['Ram','Anil','Ganesh','Biju','Niketan','Praful','Santosh','An
ant','Laxmi','Gaurav']
salary=[10000,30000,15000.5,10000,30000,15000.5,10000,30000,15000.5,2
0000]
doj=['10-10-2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998','10-10-
2000','03-20-2001','12-05-2002','09-10-2000','08-08-1998']
a={'eno':eno,'ename':ename,'salary':salary,'doj':doj}
employee=pd.DataFrame(a)
employee.set_index('ename',inplace=True)
print(employee)
print(employee.head(4))
30
print(employee.tail(5))
print(employee[0:10:2])
print(employee[::-1])
print(employee['eno'])
print(employee[['eno','salary']])
print(employee.iloc[8,])
print(employee.iloc[1:5,])
print(employee.loc['Ram':'Gaurav','eno':'doj'])
print(employee.loc[['Ram','Niketan','Gaurav'],['eno','doj']])
print(employee.iloc[2:5,])
print(employee.at['Niketan','salary'])

31
OUTPUT:

eno salary doj


ename
Ram 1001 10000.0 10-10-2000
Anil 1002 30000.0 03-20-2001
Ganesh 1003 15000.5 12-05-2002
Biju 1004 10000.0 09-10-2000
Niketan 1005 30000.0 08-08-1998
Praful 1006 15000.5 10-10-2000
Santosh 1007 10000.0 03-20-2001
Anant 1008 30000.0 12-05-2002
Laxmi 1009 15000.5 09-10-2000
Gaurav 1010 20000.0 08-08-1998
eno salary doj
ename
Ram 1001 10000.0 10-10-2000
Anil 1002 30000.0 03-20-2001
Ganesh 1003 15000.5 12-05-2002
Biju 1004 10000.0 09-10-2000
eno salary doj
ename
Praful 1006 15000.5 10-10-2000
Santosh 1007 10000.0 03-20-2001
Anant 1008 30000.0 12-05-2002
Laxmi 1009 15000.5 09-10-2000
Gaurav 1010 20000.0 08-08-1998
eno salary doj
ename
Ram 1001 10000.0 10-10-2000
Ganesh 1003 15000.5 12-05-2002
32
Niketan 1005 30000.0 08-08-1998
Santosh 1007 10000.0 03-20-2001
Laxmi 1009 15000.5 09-10-2000
eno salary doj
ename
Gaurav 1010 20000.0 08-08-1998
Laxmi 1009 15000.5 09-10-2000
Anant 1008 30000.0 12-05-2002
Santosh 1007 10000.0 03-20-2001
Praful 1006 15000.5 10-10-2000
Niketan 1005 30000.0 08-08-1998
Biju 1004 10000.0 09-10-2000
Ganesh 1003 15000.5 12-05-2002
Anil 1002 30000.0 03-20-2001
Ram 1001 10000.0 10-10-2000
ename
Ram 1001
Anil 1002
Ganesh 1003
Biju 1004
Niketan 1005
Praful 1006
Santosh 1007
Anant 1008
Laxmi 1009
Gaurav 1010
Name: eno, dtype: int64
eno salary
ename
Ram 1001 10000.0
Anil 1002 30000.0
Ganesh 1003 15000.5
Biju 1004 10000.0
Niketan 1005 30000.0
Praful 1006 15000.5
Santosh 1007 10000.0
Anant 1008 30000.0
Laxmi 1009 15000.5
Gaurav 1010 20000.0
eno 1009
salary 15000.5
33
doj 09-10-2000
Name: Laxmi, dtype: object
eno salary doj
ename
Anil 1002 30000.0 03-20-2001
Ganesh 1003 15000.5 12-05-2002
Biju 1004 10000.0 09-10-2000
Niketan 1005 30000.0 08-08-1998

eno salary doj


ename
Ram 1001 10000.0 10-10-2000
Anil 1002 30000.0 03-20-2001
Ganesh 1003 15000.5 12-05-2002
Biju 1004 10000.0 09-10-2000
Niketan 1005 30000.0 08-08-1998
Praful 1006 15000.5 10-10-2000
Santosh 1007 10000.0 03-20-2001
Anant 1008 30000.0 12-05-2002
Laxmi 1009 15000.5 09-10-2000
Gaurav 1010 20000.0 08-08-1998
eno doj
ename
Ram 1001 10-10-2000
Niketan 1005 08-08-1998
Gaurav 1010 08-08-1998
eno salary doj
ename
Ganesh 1003 15000.5 12-05-2002
Biju 1004 10000.0 09-10-2000
Niketan 1005 30000.0 08-08-1998
30000.0

34
13.CREATE THE FOLLOWING DATAFRAME UT:
OBSERVE THE DATAFRAME UT AND WRITE THE COMMANDS TO DO
THE FOLLOWING:
A. ADD A NEW COLUMN COMP WITH 25 MARKS IN EACH.
B. ADD A NEW COLUMN TOTAL WHICH WILL DISPLAY THE TOTAL
OF ALL SUBJECTS [MATHS+SCIENCE+HINDI+ENG+COMP].
C. ADD A NEW ROW TO A DATAFRAME UT [ YOUR NAME UT1 AND
MARKS RESPECTIVELY].
D. ADD ONE MORE STUDENT [SHIVI WITH 0 MARKS UT1 IN ALL
SUBJECTS].
E. DELETE THE RECORD OF RAMAN.
F. DELETE THE COLUMN TOTAL.
G. RENAME INDEX AS S1, S2, S3.
H. DISPLAY MISHTI’S RECORDS.
I. DISPLAY MISHTI’S RECORDS WITH MATHS AND SCIENCE MARKS.

CODE:

import pandas as pd
name=['Raman','Raman','Raman','Zuhaire','Zuhaire','Zuhaire','As
hravy','Ashravy','Ashravy',
'Mishti','Mishti','Mishti']
ut=[1,2,3,1,2,3,1,2,3,1,2,3]
35
maths=[22,21,14,20,23,22,23,24,12,15,18,17]
science=[21,20,19,17,15,18,19,22,25,22,21,18]
sst=[18,17,15,22,21,19,20,24,19,25,25,20]
hindi=[20,22,24,24,25,23,15,17,21,22,24,25]
eng=[21,24,23,19,15,13,22,21,23,22,23,20]
m={'Name':name,'UT':ut,'Maths':maths,'Science':science,'S.st':sst,'Hi
ndi':hindi,'Eng':eng}
UT=pd.DataFrame(m)
comp=[25,25,25,25,25,25,25,25,25,25,25,25]
UT['Comp']=comp
total=[]
for i in range(0,12):

total.append(maths[i]+science[i]+sst[i]+hindi[i]+eng[i]+comp[i])
UT['Total']=total
print(UT)
UT.loc[12,:]=['Ananya',1,23,21,22,20,23,25,134]
UT.loc[13,:]=['Shivi',1,0,0,0,0,0,0,0]
UT.drop(UT.index[UT['Name']=='Raman'],axis=0,inplace=True)
del UT['Total']
UT.index=['s1','s2','s3','s4','s5','s6','s7','s8','s9','s10','s11']
print(UT)
print(UT.loc['s7':'s9',])
print(UT.loc['s7':'s9',['Name','UT','Maths','Science']])

36
OUTPUT:

Name UT Maths Science S.st Hindi Eng Comp Total


0 Raman 1 22 21 18 20 21 25 127
1 Raman 2 21 20 17 22 24 25 129
2 Raman 3 14 19 15 24 23 25 120
3 Zuhaire 1 20 17 22 24 19 25 127
4 Zuhaire 2 23 15 21 25 15 25 124
5 Zuhaire 3 22 18 19 23 13 25 120
6 Ashravy 1 23 19 20 15 22 25 124
7 Ashravy 2 24 22 24 17 21 25 133
8 Ashravy 3 12 25 19 21 23 25 125
9 Mishti 1 15 22 25 22 22 25 131
10 Mishti 2 18 21 25 24 23 25 136
11 Mishti 3 17 18 20 25 20 25 125

Name UT Maths Science S.st Hindi Eng Comp


s1 Zuhaire 1.0 20.0 17.0 22.0 24.0 19.0 25.0
s2 Zuhaire 2.0 23.0 15.0 21.0 25.0 15.0 25.0
s3 Zuhaire 3.0 22.0 18.0 19.0 23.0 13.0 25.0
s4 Ashravy 1.0 23.0 19.0 20.0 15.0 22.0 25.0
s5 Ashravy 2.0 24.0 22.0 24.0 17.0 21.0 25.0
s6 Ashravy 3.0 12.0 25.0 19.0 21.0 23.0 25.0
37
s7 Mishti 1.0 15.0 22.0 25.0 22.0 22.0 25.0
s8 Mishti 2.0 18.0 21.0 25.0 24.0 23.0 25.0
s9 Mishti 3.0 17.0 18.0 20.0 25.0 20.0 25.0
s10 Ananya 1.0 23.0 21.0 22.0 20.0 23.0 25.0
s11 Shivi 1.0 0.0 0.0 0.0 0.0 0.0 0.0

Name UT Maths Science S.st Hindi Eng Comp


s7 Mishti 1.0 15.0 22.0 25.0 22.0 22.0 25.0
s8 Mishti 2.0 18.0 21.0 25.0 24.0 23.0 25.0
s9 Mishti 3.0 17.0 18.0 20.0 25.0 20.0 25.0

Name UT Maths Science


s7 Mishti 1.0 15.0 22.0
s8 Mishti 2.0 18.0 21.0
s9 Mishti 3.0 17.0 18.0

14.CREATE A CSV FILE, SAVE IT AS EMPLOYEE.CSV.

Eno Ename Salary Deptno Commission


1001 Ram 10000 10 1000
1002 Anil 30000 20
1003 Ganesh 15000.5 10 3000
1004 Biju 10000 20
1005 Niketan 30000 30
1006 Praful 15000.5 10 2000
1007 Santosh 10000 10
1008 Anant 30000 40
1009 Laxmi 15000.5 10 5000

38
WRITE THE PYTHON CODE TO CONVERT EMPLOYEE.CSV TO
DATAFRAME EMP.
A. DISPLAY ENTIRE DATAFRAME.
B. WRITE THE PYTHON CODE TO CONVERT EMPLOYEE.CSV TO
DATAFRAME EMP1 WITH FIRST FIVE ROWS AND DIFFERENT
HEADER (DONT USE THE HEADER OF CSV FILE). DISPLAY THE
DATAFRAME.
C. WRITE THE PYTHON CODE TO READ THE ALTERNATE ROWS OF
CSV FILE EMPLOYEE INTO DATAFRAME EMP2.

CODE:

import pandas as pd
import numpy as np
emp=pd.read_csv('employee.csv')
print(emp)
emp1=pd.read_csv('employee.csv',names=['rno','name','sal','dptno'
,'comm'])
print(emp1.head(5))
emp2=pd.read_csv('employee.csv')
print(emp2[0:10:2])

OUTPUT:

Eno Ename Salary Deptno Commission


0 1001 Ram 10000.0 10 1000.0
1 1002 Anil 30000.0 20 NaN
2 1003 Ganesh 15000.5 10 3000.0
3 1004 Biju 10000.0 20 NaN
4 1005 Niketan 30000.0 30 NaN
5 1006 Praful 15000.5 10 2000.0
6 1007 Santosh 10000.0 10 NaN
7 1008 Anant 30000.0 40 NaN
8 1009 Laxmi 15000.5 10 5000.0
rno name sal dptno comm
0 Eno Ename Salary Deptno Commission
1 1001 Ram 10000 10 1000
39
2 1002 Anil 30000 20 NaN
3 1003 Ganesh 15000.5 10 3000
4 1004 Biju 10000 20 NaN
Eno Ename Salary Deptno Commission
0 1001 Ram 10000.0 10 1000.0
2 1003 Ganesh 15000.5 10 3000.0
4 1005 Niketan 30000.0 30 NaN
6 1007 Santosh 10000.0 10 NaN
8 1009 Laxmi 15000.5 10 5000.0

15.CREATE THE FOLLOWING DATAFRAME DEPARTMENT.


deptno dname loc
0 10 fin Delhi
1 20 sales Navi Mumbai
2 30 res Jaipur
3 40 NaN Mumbai
4 50 mkt Delhi

WRITE THE PYTHON CODE TO CONVERT DEPARTMENT DATAFRAME


TO DEPT.CSV FILE. WHILE WRITING DATAFRAME IN CSV FILE USE ‘\T’ AS
SEPREATOR AND FILL NAN VALUES WITH ‘NULL’. DISPLAY THE
CONTENTS OF DEPT.CSV FILE.

40
CODE:

import pandas as pd
import numpy as np
deptno=[10,20,30,40,50]
dname=['fin','sales','res’,np.NaN,'mkt']
loc=['Delhi','Navi Mumbai','Jaipur','Mumbai','Delhi']
dpt={'deptno':deptno,'dname':dname,'loc':loc}
department=pd.DataFrame(dpt)
department['dname']=department['dname'].replace(np.NaN,'Nu
ll')
print(department)
department.to_csv('dep.csv',sep='\t')
d=pd.read_csv('dep.csv')
print(d)

OUTPUT:

deptno dname loc


0 10 fin Delhi
1 20 sales Navi Mumbai
2 30 res Jaipur
3 40 Null Mumbai
4 50 mkt Delhi
\tdeptno\tdname\tloc
0 0\t10\tfin\tDelhi
41
1 1\t20\tsales\tNavi Mumbai
2 2\t30\tres\tJaipur
3 3\t40\tNull\tMumbai
4 4\t50\tmkt\tDelhi

16.SMILE NGO HAS PARTICIPATED IN A THREE-WEEK CULTURAL MELA.


USING PANDAS, THEY HAVE STORED THE SALES (IN RS) MADE DAY
WISE FOR EVERY WEEK IN A CSV FILE NAMED “MELASALES.CSV”, AS
SHOWN IN THE TABLE BELOW:

42
DEPICT THE SALES FOR THE THREE WEEKS USING A LINE CHART.
IT SHOULD HAVE THE FOLLOWING:
I. CHART TITLE AS “MELA SALES REPORT”.
II. X AXIS LABEL AS DAYS.
II. Y AXIS LABEL AS “SALES IN RS”.
IV. LINE COLOURS ARE RED FOR WEEK 1, BLUE FOR WEEK 2 AND
BROWN FOR WEEK 3
V. CUSTOMIZE IT BY MARKER="*", MARKERIZE=10, LINEWIDTH=3,
LINESTYLE="--”.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv('melasales.csv')
n=['mon','tue','wed','thu','fri','sat','sun']
d=pd.DataFrame(data)
w1=d['week1']
w2=d['week2']
w3=d['week3']
plt.plot(n,w1,marker='*',markersize=10,linewidth=3,linestyle='--
',color='red',label='week1')
plt.plot(n,w2,marker='*',markersize=10,linewidth=3,linestyle='-
-',color='blue',label='week2')
plt.plot(n,w3,marker='*',markersize=10,linewidth=3,linestyle='-
-',color='brown',label='week3')
plt.legend()
plt.title('Mela Sales Report')

43
plt.xlabel('Days')
plt.ylabel('Sales in Rs')
plt.show()

44
OUTPUT:

45
17.THE FOLLOWING DATA REPRESENTS NUMBER OF PEOPLE EMPLOYED
IN 2019 AND 2020.

Month 2019 2020


Jan 153454 153250
Feb 153704 153302
Mar 153964 153392
Apr 154528 153420
May 154216 153700
Jun 153653 153409
Jul 153748 153358
Aug 154073 153674
Sep 153918 154004
Oct 153709 154057
Nov 154041 153937
Dec 153613 153887

CREATE A DATAFRAME AND PLOT A LINE CHART.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','
Dec']
a=[153454,153704,153964,154528,154216,153653,153748,154073,153918,153709,
154041,153613]
b=[153250,153302,153392,153420,153700,153409,153358,153674,154004,154057,
153937,153887]
d=pd.DataFrame({'2019':a,'2020':b},index=month)
print(d)
plt.plot(month,d['2019'], marker='h', markersize=8,
linewidth=4,
linestyle='-.', markeredgecolor='blue', label=2019)
plt.plot(month, d['2020'], marker='h', markersize=8,l
inewidth=4,l
inestyle='-.', markeredgecolor='blue', label=2020)
plt.legend()
plt.xlabel('Month')
plt.ylabel('Number of people')
plt.title('People employed 2019 and 2020')
plt.show()

46
OUTPUT:

47
18.CREATE A MULTILINE CHART ABOUT EMPLOYEE REGISTRATION
ACCORDING TO PERSONAL VALUES WITH THE FOLLOWING FEATURES:

A. TITLE – PEOPLE EMPLOYED IN THE YEAR 2010 AND 2011.

B. X AXIS LABEL-MONTH, Y AXIS LABEL-NUMBER OF PEOPLE.

C. X LABELS SHOULD BE THE MONTH NAMES.

D. INCREASE THE LINE WIDTH AND GIVE THE LEGEND AS 2010 AND
2011.

E. GIVE DIFFERENT LINE STYLES.

F. GIVE LINE WIDTH FOR EACH YEAR’S DATA.

G. PUT THE MARKER AS ‘X’ (IN CHANGED SIZE) WITH MARKER


EDGE COLOR BLACK.

H. DISPLAY LEGEND TITLE AS 2010, 2011.

I. SAVE THE CHART AS EMPLOYMENT.PDF.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','No
v','Dec']
a=[43,61,44,78,82,67,65,63,54,39,47,55]
b=[47,59,53,84,79,77,72,64,60,48,45,54]
plt.plot(month, a, marker='X', markersize=7, linewidth=3,
linestyle='solid', markeredgecolor='black', label=2010)
plt.plot(month, b, marker='X', markersize=7, linewidth=3,
linestyle='solid', markeredgecolor='black', label=2011)
plt.legend()
plt.xlabel('Month')
plt.ylabel('Number of people')
plt.title('People employed 2010 and 2011')
plt.show()
plt.savefig('employment.pdf')

48
OUTPUT:

49
19.PLOT A MULTI BAR CHART USING THE FOLLOWING DATA FOR
MUMBAI CITY FROM THE INDIAN METEREOLOGICAL DEPARTMENT.

month avg high temp avg low temp avg rainfall rel
humidity
0 Jan 31.1 17.3 0.3 69
1 Feb 31.3 18.2 0.4 67
2 Mar 32.8 21.4 0.0 69
3 Apr 33.2 24.2 0.1 71
4 May 33.6 27.0 11.3 70
5 Jun 32.4 26.6 493.1 80
6 Jul 30.4 25.5 840.7 86
7 Aug 30.0 25.1 585.2 86
8 Sep 30.7 24.8 341.4 83
9 Oct 33.4 23.8 89.3 78
10 Nov 33.7 21.3 9.9 71
11 Dec 32.4 18.5 1.6 69

A. CREATE SEQUENCES HIGH TEMP AVG TEMP RAINFALL


RELHUMIDITY
B. PLOT THESE FOUR SEQUENCES ON A BAR CHART WHERE RED
COLOUR SHOWS THE HIGH TEMP, BLUE COLOUR SHOWS THE
LOW TEMP, OLIVE COLOUR DEPICTS THE RAINFALL, AND SILVER
DEPICTS REL HUMIDITY.
C. XTICKS SHOULD BE THE MONTH NAMES.
D. TITLE SHOULD BE “AVERAGE DATA FOR MUMBAI CITY.
E. WIDTH OF THE BARS SHOULD BE 0.1.
F. THE LEGEND SHOULD SHOW ALL THE DATA.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','No
v','Dec']
high=[31.1,31.3,32.8,33.2,33.6,32.4,30.4,30.0,30.7,33.4,33.7,32.4]
low=[17.3,18.2,21.4,24.2,27.0,26.6,25.5,25.1,24.8,23.8,21.3,18.5]
rain=[0.3,0.4,0.0,0.1,11.3,493.1,840.7,585.2,341.4,89.3,9.9,1.6]
hum=[69,67,69,71,70,80,86,86,83,78,71,69]
n=np.arange(len(month))

50
plt.bar(n,high,width=0.1,color='red',label='avg high temp')
plt.bar(n+0.1,low,width=0.1,color='blue',label='avg low
temp')
plt.bar(n+0.2,rain,width=0.1,color='olive',label='avg
rainfall')
plt.bar(n+0.3,hum,width=0.1,color='silver',label='rel
humidity')
plt.xticks(n,month)
plt.legend()
plt.title('Average data for Mumbai city')
plt.show()

51
OUTPUT:

52
20.PLOT THE FOLLOWING DATA USING A LINE PLOT:

DAYS MON TUE WED THU FRI SAT SUN


TICKETS SOLD 2000 2800 3000 2500 2300 2500 1000

CHANGE THE COLOR OF THE LINE TO ‘MAGENTA’.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
a=[2000,2800,3000,2500,2300,2500,1000]
n=['mon','tue','wed','thu','fri','sat','sun']
plt.plot(n,a,color='Magenta',linestyle='--
',marker='H',markersize=7)
plt.xlabel('Days')
plt.ylabel('Tickets sold')
plt.show()

53
OUTPUT:

54
21.CREATE A HISTOGRAM SHOWING NUMBER OF EMPLOYEES IN
SPECIFIC AGE GROUP:
EMP_AGES= [24,45,30,59,58,56,57,45,43,43,50,40,34,33,25,19]
BINS= [0,10,20,30,40,50,60]
SPECIFY THE FOLLOWING:
1.XLABEL, YLABEL, TITLE.
2.COLOR, EDGECOLOR.
3.CHANGE THE TYPE TO STEPFILLED.

CODE:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
emp_ages=[24,45,30,59,58,56,57,45,43,43,50,40,34,33,25,19]
bins=[0,10,20,30,40,50,60]
plt.hist(emp_ages,bins,histtype='stepfilled',color='blue',edgeco
lor='black')
plt.xlabel('age')
plt.ylabel('number of people')
plt.title('Ages of Employees')
plt.show()

55
OUTPUT:

56
Program 22

1.Create Database IP122021.


2. Activate IP122021.
3. Create table employee with following fields and constraints
eno int primary key ename varchar(20) not null
sal decimal(10,2)
comm decimal(10,2)
deptno int job varchar(10)
doj date
4. Insert 5 records
5. Display all the records.
6. Display Structure of Table.
7. Display employee name and job from the employee table.
8. Display the records

create database IP122021;


use IP12021;
use IP122021;
create table employee(eno int primary key,ename varchar(20) not
null,sal decimal(10,2), comm decimal(10,2),deptno int,job
varchar(10),doj date);
insert into employee values(101,'A',10000,2000,10,'FINANCE','2022-03-04');
insert into employee values(102,'B',20000,1500,30,'MARKETING','2022-07-
04');
insert into employee values(103,'C',50000,4000,20,'MANAGEMENT','2022-
12-04');
insert into employee values(104,'D',25000,3000,20,'MANAGEMENT','2022-
12-06');
insert into employee values(105,'E',15000,2000,10,'FINANCE','2022-11-02');

select * from employee;


+-----+-------+----------+---------+--------+------------+------------+
| eno | ename | sal | comm | deptno | job | doj |
+-----+-------+----------+---------+--------+------------+------------+
| 101 | A | 10000.00 | 2000.00 | 10 | FINANCE | 2022-03-04 |
| 102 | B | 20000.00 | 1500.00 | 30 | MARKETING | 2022-07-04 |
| 103 | C | 50000.00 | 4000.00 | 20 | MANAGEMENT | 2022-12-04 |
| 104 | D | 25000.00 | 3000.00 | 20 | MANAGEMENT | 2022-12-06 |
| 105 | E | 15000.00 | 2000.00 | 10 | FINANCE | 2022-11-02 |
+-----+-------+----------+---------+--------+------------+------------+

57
desc employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| eno | int(11) | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| doj | date | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+

select ename,job from employee;


+-------+------------+
| ename | job |
+-------+------------+
|A | FINANCE |
|B | MARKETING |
|C | MANAGEMENT |
|D | MANAGEMENT |
|E | FINANCE |
+-------+------------+

58
Program 23

Consider the following table named “GYM” with details about


Fitness products being sold in the store.
prcode int Primary Key , prname varchar(20) , Price int , manu-
facturer varchar(20) Table Name : GYM

Insert 6 records ( Note the records must cater to all require-


ments of the queries given below)

a)Display the names and price of all the products in the store
b) Display the names of all the products with price less
than Rs.20000.00 c) Display details of all the products
with price in the range 20000 to 30000 d) Display names of
all products by the select manufacturer “Fit Express” e)
Add a new row for product with the details:
“P106”,“Vibro Exerciser”, 23000, manufacturer : null.
f) Display details of all products with manufacturer
name starting with “A” g) Display details of all products
with manufacturer name not ending with “s” h) Display
all rows sorted in descending order of price.
i)Display the name and price where manufacturer is null
j) Display the name and price where manufacturer is not null
k) Display manufacturer, whose name has 10 characters
l) Display manufacturer, whose name not starting with A alpha-
bet.
m) Display the price of items where manufacturer is Avon fitness
.
n) display product name where manufacturer details are not en-
tered.
o) display product name and manufacturer details for all prod-
ucts with manufacturer details.

create table gym(Prcode int primary key,Prname


varchar(20),Unit_price int,Manufacturer varchar(20));
insert into gym values(101,'cross trainer',22500,'avon fitness');
insert into gym values(102,'treadmill',28800,'ag fitline');
insert into gym values(103,'message chair',18000,'fitexpress');
insert into gym values(104,'vibration trainer',19800,'avon fitness');
insert into gym values(105,'bike',11700,'fitexpress');
insert into gym values(106,'vibro exerciser',20700,null);

59
select Prname,Unit_price from gym;
+-------------------+------------+
| Prname | Unit_price |
+-------------------+------------+
| cross trainer | 22500 |
| treadmill | 28800 |
| message chair | 18000 |
| vibration trainer | 19800 |
| bike | 11700 |
| vibro exerciser | 20700 |
+-------------------+------------+

select Prname from gym where Unit_price<20000;


+-------------------+
| Prname |
+-------------------+
| message chair |
| vibration trainer |
| bike |
+-------------------+

select * from gym where Unit_price>20000 and Unit_price<30000;


+--------+-----------------+------------+--------------+
| Prcode | Prname | Unit_price | Manufacturer |
+--------+-----------------+------------+--------------+
| 101 | cross trainer | 22500 | avon fitness |
| 102 | treadmill | 28800 | ag fitline |
| 106 | vibro exerciser | 20700 | NULL |
+--------+-----------------+------------+--------------+

select Prname from gym where manufacturer='fitexpress';


+---------------+
| Prname |
+---------------+
| message chair |
| bike |
+---------------+

update gym set Unit_price=23000 where Prcode=106;


select * from gym;
+--------+-------------------+------------+--------------+
| Prcode | Prname | Unit_price | Manufacturer |
+--------+-------------------+------------+--------------+
| 101 | cross trainer | 22500 | avon fitness |
| 102 | treadmill | 28800 | ag fitline |
| 103 | message chair | 18000 | fitexpress |
| 104 | vibration trainer | 19800 | avon fitness |
| 105 | bike | 11700 | fitexpress |
| 106 | vibro exerciser | 23000 | NULL |
+--------+-------------------+------------+--------------+

60
select * from gym where manufacturer like 'a%';
+--------+-------------------+------------+--------------+
| Prcode | Prname | Unit_price | Manufacturer |
+--------+-------------------+------------+--------------+
| 101 | cross trainer | 22500 | avon fitness |
| 102 | treadmill | 28800 | ag fitline |
| 104 | vibration trainer | 19800 | avon fitness |
+--------+-------------------+------------+--------------+

select * from gym where manufacturer not like '%s';


+--------+-----------+------------+--------------+
| Prcode | Prname | Unit_price | Manufacturer |
+--------+-----------+------------+--------------+
| 102 | treadmill | 28800 | ag fitline |
+--------+-----------+------------+--------------+

select * from gym order by Unit_price desc;


+--------+-------------------+------------+--------------+
| Prcode | Prname | Unit_price | Manufacturer |
+--------+-------------------+------------+--------------+
| 102 | treadmill | 28800 | ag fitline |
| 106 | vibro exerciser | 23000 | NULL |
| 101 | cross trainer | 22500 | avon fitness |
| 104 | vibration trainer | 19800 | avon fitness |
| 103 | message chair | 18000 | fitexpress |
| 105 | bike | 11700 | fitexpress |
+--------+-------------------+------------+--------------+

select Prname, Unit_price from gym where manufacturer is null;


+-----------------+------------+
| Prname | Unit_price |
+-----------------+------------+
| vibro exerciser | 23000 |
+-----------------+------------+

select Prname, Unit_price from gym where manufacturer is not


null;
+-------------------+------------+
| Prname | Unit_price |
+-------------------+------------+
| cross trainer | 22500 |
| treadmill | 28800 |
| message chair | 18000 |
| vibration trainer | 19800 |
| bike | 11700 |
+-------------------+------------+

select manufacturer from gym where manufacturer like


'__________';
+--------------+

61
| manufacturer |
+--------------+
| ag fitline |
| fitexpress |
| fitexpress |
+--------------+

select manufacturer from gym where manufacturer not like


'a%';
+--------------+
| manufacturer |
+--------------+
| fitexpress |
| fitexpress |
+--------------+

select Unit_price from gym where manufacturer='avon fitness';


+------------+
| Unit_price |
+------------+
| 22500 |
| 19800 |
+------------+

select Prname from gym where manufacturer is null;


+-----------------+
| Prname |
+-----------------+
| vibro exerciser |
+-----------------+

select Prname,manufacturer from gym where manufacturer is


not null;
+-------------------+--------------+
| Prname | manufacturer |
+-------------------+--------------+
| cross trainer | avon fitness |
| treadmill | ag fitline |
| message chair | fitexpress |
| vibration trainer | avon fitness |
| bike | fitexpress |
+-------------------+--------------+

62
Program 24

63
create table charity(P_Id int,LastName varchar(15),FirstName
varchar(15),Adress varchar(25), City varchar(10), Contribution
decimal(6,2));
insert into charity values(1,'Bindra','Jaspreet','5B,Gomti
Nagar','Lucknow',3500.50);
insert into charity
values(2,'Rana','Monica','21A,Bandra','Mumbai',2768);
insert into charity values(3,'Singh','Jatinder','8,Punjabi

64
Bagh','Delhi',2000.50);
insert into charity values(4,'Arora','Satinder','K/1,Shere Punjab
Colony','Mumbai',1900);
insert into charity values(5,'Krishnan','Vineeta','A-75,Adarsh
Nagar',null,null);

select lower(FirstName) from charity;


+------------------+
| lower(FirstName) |
+------------------+
| jaspreet |
| monica |
| jatinder |
| satinder |
| vineeta |
+------------------+

select upper(LastName) from charity where City='Mumbai';


+-----------------+
| upper(LastName) |
+-----------------+
| RANA |
| ARORA |
+-----------------+

select P_Id,left(FirstName,3) from charity;


+------+-------------------+
| P_Id | left(FirstName,3) |
+------+-------------------+
| 1 | Jas |
| 2 | Mon |
| 3 | Jat |
| 4 | Sat |
| 5 | Vin |
+------+-------------------+

select concat(FirstName,LastName) from charity;


+----------------------------+
| concat(FirstName,LastName) |
+----------------------------+
| JaspreetBindra |
| MonicaRana |
| JatinderSingh |
| SatinderArora |
| VineetaKrishnan |
+----------------------------+

65
select length(Adress),P_Id from charity;
+----------------+------+
| length(Adress) | P_Id |
+----------------+------+
| 14 | 1 |
| 10 | 2 |
| 14 | 3 |
| 23 | 4 |
| 17 | 5 |
+----------------+------+

select right(City,2),right(P_Id,2) from charity;


+---------------+---------------+
| right(City,2) | right(P_Id,2) |
+---------------+---------------+
| ow |1 |
| ai |2 |
| hi |3 |
| ai |4 |
| NULL |5 |
+---------------+---------------+

select LastName,FirstName from charity where FirstName like


'_at%' or FirstName like '__at%';
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Singh | Jatinder |
| Arora | Satinder |
+----------+-----------+

select instr(LastName,'a') from charity;


+---------------------+
| instr(LastName,'a') |
+---------------------+
| 6|
| 2|
| 0|
| 1|
| 7|
+---------------------+

select FirstName,LastName from charity where FirstName like '%a';


+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Monica | Rana |
| Vineeta | Krishnan |
+-----------+----------+

66
select concat(rtrim(FirstName),ltrim(LastName)) from charity;
+------------------------------------------+
| concat(rtrim(FirstName),ltrim(LastName)) |
+------------------------------------------+
| JaspreetBindra |
| MonicaRana |
| JatinderSingh |
| SatinderArora |
| VineetaKrishnan |
+------------------------------------------+

select P_Id,LastName,round(Contribution) from charity;


+------+----------+---------------------+
| P_Id | LastName | round(Contribution) |
+------+----------+---------------------+
| 1 | Bindra | 3501 |
| 2 | Rana | 2768 |
| 3 | Singh | 2001 |
| 4 | Arora | 1900 |
| 5 | Krishnan | NULL |
+------+----------+---------------------+

select P_Id,LastName,truncate(Contribution,2) from charity;


+------+----------+--------------------------+
| P_Id | LastName | truncate(Contribution,2) |
+------+----------+--------------------------+
| 1 | Bindra | 3500.50 |
| 2 | Rana | 2768.00 |
| 3 | Singh | 2000.50 |
| 4 | Arora | 1900.00 |
| 5 | Krishnan | NULL |
+------+----------+--------------------------+

select LastName,Contribution,round(Contribution/10,2) from


charity;
+----------+--------------+--------------------------+
| LastName | Contribution | round(Contribution/10,2) |
+----------+--------------+--------------------------+
| Bindra | 3500.50 | 350.05 |
| Rana | 2768.00 | 276.80 |
| Singh | 2000.50 | 200.05 |
| Arora | 1900.00 | 190.00 |
| Krishnan | NULL | NULL |
+----------+--------------+--------------------------+

67
Program 25

1. Write a query to display the username in upper case and


lower case.
2. Write a query to display the last 2 letters of user
and first two letters of hash. 3. Write a query to dis-
play the year of oldest transaction.
4. Write a query to display the year of current transaction.
5. Write a query to display all the characters in user from 3 rd
place.
6. Write a query to display the length of hash.
7. Write a query to display the id and month name,dayname
from transaction date. 8. Write a query to display the
user,month name,yearname and year for all the users
where user name is ending with a.
9. Write a query to display the user and position of ‘a’
in user with alias ‘position’ 10.Write a query to display
the user and position of ‘ai’ in user with alias ‘posi-
tion’.

create table blockchain(id int,user varchar(15),value int,hash


varchar(10),transaction_date date);
insert into blockchain values(1,'Steve',900,'ERTYU','2020-09-19');
insert into blockchain values(2,'Meesha',145,'@345r','2021-03-23');

68
insert into blockchain values(3,'Nimisha',567,'#wert5','2020-05-06');
insert into blockchain values(4,'Pihu',678,'%rtyu','2022-07-13');
insert into blockchain values(5,'Kopal',768,'rrt4%','2021-05-15');
insert into blockchain values(7,'Palakshi',534,'wer@3','2022-11-29');

select lower(user),upper(user) from blockchain;


+-------------+-------------+
| lower(user) | upper(user) |
+-------------+-------------+
| steve | STEVE |
| meesha | MEESHA |
| nimisha | NIMISHA |
| pihu | PIHU |
| kopal | KOPAL |
| palakshi | PALAKSHI |
+-------------+-------------+

select right(user,2),left(hash,2) from blockchain;


+---------------+--------------+
| right(user,2) | left(hash,2) |
+---------------+--------------+
| ve | ER |
| ha | @3 |
| ha | #w |
| hu | %r |
| al | rr |
| hi | we |
+---------------+--------------+

select min(transaction_date) from blockchain;


+-----------------------+
| min(transaction_date) |
+-----------------------+
| 2020-05-06 |
+-----------------------+

select max(transaction_date) from blockchain;


+-----------------------+
| max(transaction_date) |
+-----------------------+
| 2022-11-29 |
+-----------------------+

select substr(user,3) from blockchain;

69
+----------------+
| substr(user,3) |
+----------------+
| eve |
| esha |
| misha |
| hu |
| pal |
| lakshi |
+----------------+

select length(hash) from blockchain;


+--------------+
| length(hash) |
+--------------+
| 5|
| 5|
| 6|
| 5|
| 5|
| 5|
+--------------+

select
id,monthname(transaction_date),dayname(transaction_date)
from blockchain;
+------+-----------------------------+---------------------------+
| id | monthname(transaction_date) | dayname(transaction_date)
|
+------+-----------------------------+---------------------------+
| 1 | September | Saturday |
| 2 | March | Tuesday |
| 3 | May | Wednesday |
| 4 | July | Wednesday |
| 5 | May | Saturday |
| 7 | November | Tuesday |
+------+-----------------------------+---------------------------+

select
user,monthname(transaction_date),year(transaction_date) from
blockchain where user like '%a';
+---------+-----------------------------+------------------------+
| user | monthname(transaction_date) | year(transaction_date) |
+---------+-----------------------------+------------------------+
| Meesha | March | 2021 |
| Nimisha | May | 2020 |
+---------+-----------------------------+------------------------+

70
select user,instr(user,'a') 'position' from blockchain;
+----------+----------+
| user | position |
+----------+----------+
| Steve | 0|
| Meesha | 6|
| Nimisha | 7|
| Pihu | 0|
| Kopal | 4|
| Palakshi | 2|
+----------+----------+

select user,instr(user,'ai') 'position' from blockchain;


+----------+----------+
| user | position |
+----------+----------+
| Steve | 0|
| Meesha | 0|
| Nimisha | 0|
| Pihu | 0|
| Kopal | 0|
| Palakshi | 0|
+----------+----------+

71
Program 26

Create Table Shoes


Write SQL query for the following:
Table name: shoes
Attributes: code-Primary key, type can be school,office,sports
Insert following records:

1. Display the type, minimum, maximum and average margin of


each type of shoes. 2. Display type and total quantity of each
type of shoes. Arrange it by descending order of total quan-
tity .
3. Display type and total quantity of each type of shoes. And dis-
play only those type where total qty is more than 1500.

72
4. Display type and total quantity of each type of shoes and
display only those type where average size is greater than 5.5.
5. Display type and total quantity of each type of shoes where
size is not equal to 6 and total quantity is greater than 1500.
6. Display the type, size, total count from shoes for each type
and with that each size.
(group by multiple columns)
7. Display the total stock value(cost*qty) of each type of
shoes. Arrange it in ascending order of total stock value
. Give alias name as ‘Total stock value’.
8. Display the count of total number of different types of
shoes available in shoes table.

create table shoes(code char(4) primary key,name


varchar(20),type varchar(20),size int(2),cost decimal(6,2),margin
decimal(4,2),qty int(4));
insert into shoes values(1001,'School Canvas','School',6,132.50,2,1200);
insert into shoes values(1002,'School Canvas','School',7,135.50,2,800);
insert into shoes values(1003,'School Canvas','School',8,140.75,2,600);
insert into shoes values(1011,'School Leather','School',6,232.50,2,2200);
insert into shoes values(1012,'School Leather','School',7,270,2,1280);
insert into shoes values(1013,'School
Leather','School',8,320.75,null,1100);
insert into shoes values(1101,'Galaxy','Office',7,640,3,200);
insert into shoes values(1102,'Galaxy','Office',8,712,3,500);
insert into shoes values(1103,'Galaxy','Office',9,720,3,400);
insert into shoes values(1201,'Tracker','Sports',6,700,null,280);
insert into shoes values(1202,'Tracker','Sports',7,745.25,3.5,null);
insert into shoes values(1203,'Tracker','Sports',8,800.50,3.5,600);
insert into shoes values(1204,'Tracker','Sports',9,843,null,860);

select type,min(margin),max(margin),avg(margin) from shoes


group by type;
+--------+-------------+-------------+-------------+
| type | min(margin) | max(margin) | avg(margin) |
+--------+-------------+-------------+-------------+
| Office | 3.00 | 3.00 | 3.000000 |
| School | 2.00 | 2.00 | 2.000000 |
| Sports | 3.50 | 3.50 | 3.500000 |
+--------+-------------+-------------+-------------+

select type, sum(qty) from shoes group by type order by sum(qty)


desc;
+--------+----------+
| type | sum(qty) |
+--------+----------+
| School | 7180 |
| Sports | 1740 |
| Office | 1100 |

73
+--------+----------+

select type, sum(qty) from shoes group by type having


sum(qty)>1500;
+--------+----------+
| type | sum(qty) |
+--------+----------+
| School | 7180 |
| Sports | 1740 |
+--------+----------+

select type, sum(qty) from shoes group by type having


avg(size)>5.5;
+--------+----------+
| type | sum(qty) |
+--------+----------+
| Office | 1100 |
| School | 7180 |
| Sports | 1740 |
+--------+----------+

select type, sum(qty) from shoes group by type having avg(size)!=6


and sum(qty)>1500;
+--------+----------+
| type | sum(qty) |
+--------+----------+
| School | 7180 |
| Sports | 1740 |
+--------+----------+

select type, size, count(*) from shoes group by type,size;


+--------+------+----------+
| type | size | count(*) |
+--------+------+----------+
| Office | 7 | 1|
| Office | 8 | 1|
| Office | 9 | 1|
| School | 6 | 2|
| School | 7 | 2|
| School | 8 | 2|
| Sports | 6 | 1|
| Sports | 7 | 1|
| Sports | 8 | 1|
| Sports | 9 | 1|
+--------+------+----------+

select (avg(cost)*avg(qty)) as 'Total stock value' from shoes


group by type order by 'Total stock value';

74
+-------------------+
| Total stock value |
+-------------------+
| 245715.5555550198 |
| 253244.4444437396 |
| 447868.7500000000 |
+-------------------+

select count(*) from shoes group by type;


+----------+
| count(*) |
+----------+
| 3|
| 6|
| 4|
+----------+

Program 27
Create tables(Emp and Dept) with following constraints AND in-
sert 5 records in emp table and dept table.
Table emp

empno int Primary key


ename char(20)

deptno int foreign key

sal int
Table dept
deptno int primary key

dname char(10)

location char(30)
Insert following Records:
EMP

+-----+-------+------+----------+

75
| eno | ename | dno | sal |
+-----+-------+------+----------+

| 101 | A | 10 | 10000.00 |
| 102 | B | 20 | 20000.00 |
| 103 | C | NULL | 30000.00 |

| 104 | D | 10 | 5000.00 |

| 105 | E | 30 | 50000.00

DEPT

+-----+-------+--------+
| dno | dname | loc |
+-----+-------+--------+

| 10 | FIN | NM |
| 20 | mkt | delhi |
| 30 | admin | nm |

| 40 | RES | Jaipur |

| 50 | Sales | Jaipur |
Write SQL commands to:
2. Display ename,dname and department number from both ta-
bles.

3. Display ename,dno,location where sal is between 1000 and


40000.

4. Display ename,dname and dno where sal is >30000 and location


is “Navi Mumbai”.

5. Display ename,dno,location where sal is between 1000


and 40000.Arrange sal in descending order.
6. Display ename,dname and dno where sal is <= 30000 and loca-
tion is “delhi” and dept is 10 or 20.
7. Display the details of emp table and dname from both
tables using equi join. 8. Display the Employee name ,loca-
tion and Salary of all the employees in Jaipur. 9. Write
down the output of the following:
select dname,count(*)

from emp,dept
where dept.deptno=emp.deptno
group by emp.deptno

76
create table emp(empno int primary key,ename char(20),deptno
int,sal int);
insert into emp values(101,'A',10,10000);
insert into emp values(102,'B',20,20000);
insert into emp values(103,'C',null,30000);
insert into emp values(104,'D',10,5000);
insert into emp values(105,'E',30,50000);
create table dept(deptno int primary key,dname
char(10),location char(30));
insert into dept values(10,'FIN','NM');
insert into dept values(20,'mkt','delhi');
insert into dept values(30,'admin','NM');
insert into dept values(40,'RES','Jaipur');
insert into dept values(50,'Sales','Jaipur');

select ename,dname,emp.deptno from emp,dept where


emp.deptno=dept.deptno;
+-------+-------+--------+
| ename | dname | deptno |
+-------+-------+--------+
|A | FIN | 10 |
|D | FIN | 10 |
|B | mkt | 20 |
|E | admin | 30 |
+-------+-------+--------+

select ename,emp.deptno,location from emp,dept where


emp.deptno=dept.deptno and sal>1000 and sal<40000;
+-------+--------+----------+
| ename | deptno | location |
+-------+--------+----------+
|A | 10 | NM |
|D | 10 | NM |
|B | 20 | delhi |
+-------+--------+----------+

select ename,dname,emp.deptno from emp,dept where


emp.deptno=dept.deptno and sal>3000 and location='NM';
+-------+-------+--------+
| ename | dname | deptno |
+-------+-------+--------+
|A | FIN | 10 |
|D | FIN | 10 |
|E | admin | 30 |
+-------+-------+--------+

select ename,emp.deptno,location from emp,dept where

77
emp.deptno=dept.deptno and sal>1000 and sal<40000 order by sal
desc;
+-------+--------+----------+
| ename | deptno | location |
+-------+--------+----------+
|B | 20 | delhi |
|A | 10 | NM |
|D | 10 | NM |

select ename,dname,emp.deptno from emp,dept where


emp.deptno=dept.deptno and sal<=3000 and location='delhi' and
emp.deptno= 10 or emp.deptno=20;
+-------+-------+--------+
| ename | dname | deptno |
+-------+-------+--------+
|B | FIN | 20 |
|B | mkt | 20 |
|B | admin | 20 |
|B | RES | 20 |
|B | Sales | 20 |
+-------+-------+--------+

select * from emp,dept where emp.deptno=dept.deptno;


+-------+-------+--------+-------+--------+-------+----------+
| empno | ename | deptno | sal | deptno | dname | location |
+-------+-------+--------+-------+--------+-------+----------+
| 101 | A | 10 | 10000 | 10 | FIN | NM |
| 104 | D | 10 | 5000 | 10 | FIN | NM |
| 102 | B | 20 | 20000 | 20 | mkt | delhi |
| 105 | E | 30 | 50000 | 30 | admin | NM |
+-------+-------+--------+-------+--------+-------+----------+

select ename,location,sal from emp,dept where


dept.deptno=emp.deptno and location='Jaipur';
Empty set (0.00 sec)

select dname,count(*) from emp,dept where


dept.deptno=emp.deptno group by emp.deptno;
+-------+----------+
| dname | count(*) |
+-------+----------+
| FIN | 2|
| mkt | 1|
| admin | 1|
+-------+----------+

78

You might also like