The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

CHIRAG JHA_XII E_PRACTICAL FILE FOR CBSE BOARD 2020-21 INFORMATICS PRACTICES PRACTICALS.

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by yash.deep0309, 2021-03-20 14:18:01

CHIRAG JHA_XII E_PRACTICAL FILE

CHIRAG JHA_XII E_PRACTICAL FILE FOR CBSE BOARD 2020-21 INFORMATICS PRACTICES PRACTICALS.

INFORMATICS
PRACTICES

PRACTICAL FILE

Subject Code: - 065
Name of Student: - Chirag Jha
Class: - 12th
Section: - E
Session: - 2020-21

Roll Number: - 26624666

1

INDEX

Chapter Chapter Name PageNo. Sign.
No
3-11
1 PYTHON SERIES 12-20
21-28
2 PYTHON DATAFRAME 29-44

3 VISUALIZATION

4. MySQL Queries

2

SERIES

Q1. Create a series in pandas
A1. Code:
import pandas as pd
data = [1, 2, 3, 4]
s = pd.Series(data)
print("Series")
print(s)
Output:
Series
01
12
23
3 4 dtype:
int64

Q2. Create a series from a python dictionary
A2. Code:
import pandas as pd
emp = {'empno' : 101, "empname" : "Ravi", "sal" : 40000}
s = pd.Series(emp)
print("Series")
print(s)
Output:
Series
empno 101
empname Ravi

3

sal 40000
dtype: object

Q3. Write a program to create two series and perform binary operations:
Addition, Subtraction, Multiplication and Division.
A3. Code:
import pandas as pd
s1 = pd.Series([2, 3, 5, 6], index = ['a', 'b', 'c', 'd'])
s2 = pd.Series([7, 5, 2, 8], index = ['a', 'b', 'd', 'f'])

#Add both series
print("Addition")
print(s1.add(s2, fill_value = 0), '\n')

#Subtract both the series
print("Subtraction")
print(s1.sub(s2, fill_value = 0), '\n')
#Multiply both the series
print("Multiplication")
print(s1.mul(s2, fill_value = 0), '\n')
#Divide both the series
print("Division")
print(s1.div(s2, fill_value = 0), '\n')
Output:
Addition
a 9.0
b 8.0
c 5.0
d 8.0
f 8.0 dtype:
float64

4

Subtraction
a -5.0
b -2.0
c 5.0
d 4.0
f -8.0 dtype:
float64
Multiplication
a 14.0
b 15.0
c 0.0
d 12.0
f 0.0 dtype:
float64
Division
a 0.285714
b 0.600000
c inf
d 3.000000
f 0.000000
dtype: float64

Q4. Write a program to show the usage of Arithmetic operators on series.
A4. Code:
#Operators in Series
import pandas as pd
s1 = pd.Series([2, 3, 5, 6], index = ['a', 'b', 'c', 'd'])
#Arithmetic Operators
print("Add a scalar value to the Series")
print(s1 + 5, '\n')
print("Subtract a scalar value from the Series")

5

print(s1 - 2, '\n')
print("Multiplay a scalar value to the Series")
print(s1 * 2, '\n')
print("Divide the Series by a scalar value")
print(s1 / 2, '\n')
print("Exponential")
print(s1 ** 2, '\n')
print("Integer division")
print(s1 // 2, '\n')
Output:
Add a scalar value to the Series
a7
b8
c 10
d 11
dtype: int64
Subtract a scalar value from the Series
a0
b1
c3
d 4 dtype:
int64
Multiply a scalar value to the Series
a4
b6
c 10
d 12
dtype: int64
Divide the Series by a scalar value
a 1.0
b 1.5

6

c 2.5
d 3.0 dtype:
float64
Exponential
a4
b9
c 25
d 36 dtype:
int64 Integer
division
a1
b1
c2
d 3 dtype:
int64

Q5. Relational Operators on Series
A5. Code:
import pandas as pd
s1 = pd.Series([2, 5, 9, 12, 15, 13, 18, 20, 23, 40, 67, 87, 99])
print('Display all the numbers that are greater than 15')
print(s1[s1 > 15])
print("Display all the number that are greater than or equal to 12")
print(s1[s1 >= 12])
print("Display all the number that are less than 15")
print(s1[s1 < 15])
print("Display all the number that are less than or equal to 12")
print(s1[s1 <= 15])
print("Display all the numbers that are the multiple of 5")
print(s1[s1 % 5 == 0])
print("Display all the numbers that lie between 20 and 30")

7

print(s1[(s1>=20)&(51<=30)])
print("Display all the numbers that are not multiples of 3")
print(s1[s1 % 3!=0])
Output:
Display all the numbers that are greater than 15
6 18
7 20
8 23
9 40
10 67
11 87
12 99
dtype: int64
Display all the number that are greater than or equal to 12
3 12
4 15
5 13
6 18
7 20
8 23
9 40
10 67
11 87
12 99
dtype: int64
Display all the number that are less than 15
02
15
29
3 12
5 13

8

dtype: int64
Display all the number that are less than or equal to 12
02
15
29
3 12
4 15
5 13
dtype: int64
Display all the numbers that are the multiple of 5
15
4 15
7 20
9 40
dtype: int64
Display all the numbers that lie between 20 and
30 Series([], dtype: int64)
Display all the numbers that are not multiples of 3
02
15
5 13
7 20
8 23
9 40
10 67
dtype: int64

Q6. Indexing in series
A6. Code:
import pandas as pd
import numpy as np

9

d = np.array(['I', 'N', 'F', 'O', 'R', 'M', 'A', 'T', 'I', 'C', 'S'])
ser = pd.Series(d)
print('The Series is:')
print(ser)
#Display first 5 element using head()
print('First 5 elements of series:')
print(ser.head())
#Display first 3 elements
print("First 3 elements of the series:")
print(ser[: 3])
#Display last 4 elements
print('Last 4 elements of the series:')
print(ser[-4 :])
#Display last 6 elements using tail()
print('Last 6 elements of the series:')
print(ser.tail(6))
Output:
The Series is:
0I
1N
2F
3O
4R
5M
6A
7T
8I
9C
10 S
dtype: object
First 5 elements of series:

10

0I
1N
2F
3O
4R
dtype: object
First 3 elements of the series:
0I
1N
2F
dtype: object
Last 4 elements of the series:
7T
8I
9C
10 S
dtype: object
Last 6 elements of the series:
5M
6A
7T
8I
9C
10 S
dtype: object

11

DATAFRAME

Q7. Write a program to create Dataframe using Series
A7. Code:

import pandas as pd
import numpy as np
d = {"Empno" : pd.Series([101, 102, 103], index = ['a', 'b', 'c']), "Empname" :
pd.Series(["Rajeev", 'Rohan', 'Anuj', 'Vivek'], index = ['a', 'b', 'c', 'd']), 'Comm'
: pd.Series([20, 25, 30], index = ['a', 'b', 'c'])} df = pd.DataFrame(d)

print(df)
Output:

Empno Empname Comm
a 101.0 Rajeev 20.0
b 102.0 Rohan 25.0
c 103.0 Anuj 30.0
d NaN Vivek NaN

Q8. Write a program to create Dataframe from a list of Dictionaries
A8. Code:
import pandas as pd
d = [{'empno' : 101, 'empname' : 'Amit'}, {'empno' : 102, 'empname' : 'Manan', 'esal' :
5000}, {'empno' : 103, 'empname' : 'Garvit'}]
df = pd.DataFrame(d)
print(df)

Output:

12

empno empname esal
0 101 Amit NaN
1 102 Manan 5000.0
2 103 Garvit NaN

Q9. Write a program to create Dataframe to store student name, class,

marks and Phone number. Print the Dataframe.

A9. Code:

import pandas as pd

df = pd.DataFrame({'name' : ['Ravi', 'Rohan', 'Amit'], 'class' : [10, 9, 8], 'marks' : [18,

16, 15], 'Phone number' : [123, 456, 789]})

print('Dataframe')

print(df)

Output:

Dataframe

name class marks Phone number

0 Ravi 10 18 123

1 Rohan 9 16 456

2 Amit 8 15 789

Q10. Create a DataFrame NGOs and do the following questions
i) Display books and uniform column only
ii) The row where index = Odisha
iii) Rows where index = Andhra and UP
iv) All the rows of Andhra to MP
v) Columns Toys to Uniform for rows Andhra to MP
vi) Columns Toys and Uniform of Andhra and MP rows
vii) 1st to 3rd column of 2nd and 4th row

A10. Code:
import pandas as pd

13

df = pd.DataFrame({"Toys" : [7916, 8508, 7227, 7617], "Books" : [6189, 8208, 6149,
6157], "Uniform" : [610, 508, 611, 457], "Shoes" : [8810, 6798, 9611, 6457]}, index =
['Andhra', 'Odisha', "M.P.", "U.P."])
print('The Dataframe is:')
print(df, '\n')
#Display books and uniform column only
print("Books and the Uniform column
only:") print(df[['Books', "Uniform"]], '\n')
#Display a particular row
print('The row where index = Odisha: ')
print(df.loc['Odisha'], '\n')
#Display multiple rows
print("Rows where index = Andhra and
UP") print(df.loc[['Andhra', 'U.P.'],: ], '\n')
#Display list of rows
print('All the rows of Andhra to MP') print(df.loc['Andhra' :
'M.P.',:], '\n') #Display specific rows as well as columns
print('Columns Toys to Uniform for rows Andhra to MP')
print(df.loc['Andhra' : 'M.P.', 'Toys' : 'Uniform'])
print('Columns Toys and Uniform of Andhra and MP
rows') print(df.loc[['Andhra','M.P.'], ['Toys' , 'Uniform']], '\n')
#Using iloc

# Display 1st to 3rd column of 2nd to 4th
row print('1st to 3rd column of 2nd and 4th
row') print(df.iloc[1:4, 0:3])

Output:

The Dataframe is:

Toys Books Uniform Shoes

Andhra 7916 6189 610 8810

14

Odisha 8508 8208 508 6798
M.P. 7227 6149 611 9611
U.P. 7617 6157 457 6457

Books and the Uniform column only:

Books Uniform

Andhra 6189 610

Odisha 8208 508

M.P. 6149 611

U.P. 6157 457

The row where index = Odisha:
Toys 8508
Books 8208
Uniform 508
Shoes 6798
Name: Odisha, dtype: int64

Rows where index = Andhra and UP
Toys Books Uniform Shoes

Andhra 7916 6189 610 8810
U.P. 7617 6157 457 6457

All the rows of Andhra to MP

Toys Books Uniform Shoes

Andhra 7916 6189 610 8810

Odisha 8508 8208 508 6798

M.P. 7227 6149 611 9611

Columns Toys to Uniform for rows Andhra to
MP Toys Books Uniform

15

Andhra 7916 6189 610
Odisha 8508 8208 508
M.P. 7227 6149 611

Toys Uniform
Andhra 7916 610
M.P. 7227 611

1st to 3rd column of 2nd and 4th row
Toys Books Uniform

Odisha 8508 8208 508
M.P. 7227 6149 611
U.P. 7617 6157 457

Q11. Create a Dataframe and add, rename, modify delete columns from
the Dataframe
A11. Code:
import pandas as pd
df = pd.DataFrame({"Toys" : [7916, 8508, 7227, 7617], "Books" : [6189, 8208, 6149,
6157], "Uniform" : [610, 508, 611, 457], "Shoes" : [8810, 6798, 9611, 6457]}, index =
['Andhra', 'Odisha', "M.P.", "U.P."])
print('The Dataframe is:')
print(df, '\n')
#Add a column midday meal
print('Dataframe after adding a column middaymeal: ')
df['middaymeal'] = [4567, 231, 3344, 5432]
print(df)

#Rename an existing column
print('Dataframe after reanming the column Books to Expense on Books')
df.rename(columns = {'Books' : 'Expense on Books'}, inplace = True)

16

print(df)

#Delete a column
print("Dataframe after deleting the column middaymeal:")
df.drop("middaymeal", axis = 1, inplace = True)
print(df)

Output:
The Dataframe is:

Toys Books Uniform Shoes
Andhra 7916 6189 610 8810
Odisha 8508 8208 508 6798
M.P. 7227 6149 611 9611
U.P. 7617 6157 457 6457

Dataframe after adding a column middaymeal:

Toys Books Uniform Shoes middaymeal

Andhra 7916 6189 610 8810 4567

Odisha 8508 8208 508 6798 231

M.P. 7227 6149 611 9611 3344

U.P. 7617 6157 457 6457 5432

Dataframe after reanming the column Books to Expense on Books

Toys Expense on Books Uniform Shoes middaymeal

Andhra 7916 6189 610 8810 4567

Odisha 8508 8208 508 6798 231

M.P. 7227 6149 611 9611 3344

U.P. 7617 6157 457 6457 5432

Dataframe after deleting the column middaymeal:

Toys Expense on Books Uniform Shoes

Andhra 7916 6189 610 8810

Odisha 8508 8208 508 6798

17

M.P. 7227 6149 611 9611
U.P. 7617 6157 457 6457

Q12. Write a program to read a text file and open it in a text file

a. From given data set print first and last five rows

b. Find the most expensive car‟s company name and price

c. Print all Toyota car details
d. Sort all cars by price
e. Show average mileage and price of each car

A12. Code:

#Create a Dataframe from a csv file

import pandas as pd

df = pd.read_csv('Automobile.csv', sep = ',')

print(df)

#print first and last five rows

print('First five rows')

print(df.head(5))

print('Last five rows')

print(df.tail(5))

#most expensive car

print('Most expensive car company and price')

print(df[['company', 'price']][df.price == df['price'].max()])

Output:

index company body-style ... horsepower average-mileage price

0 0alfa-romero convertible ... 111 21 13495

1 1alfa-romero convertible ... 111 21 16500

2 2alfa-romero hatchback ... 154 19 16500

3 3 audi sedan ... 102 24 13950

4 4 audi sedan ... 115 18 17450

5 5 audi sedan ... 110 19 15250

6 6 audi wagon ... 110 19 18920

18

79 bmw sedan ... 101 23 16430
8 10 bmw sedan ... 101 23 16925
9 11 bmw sedan ... 121 21 20970
10 13 bmw sedan ... 182
16 30760

[11 rows x 10 columns]

First five rows

index company body-style ... horsepower average-mileage price

0 0alfa-romero convertible ... 111 21 13495

1 1alfa-romero convertible ... 111 21 16500

2 2alfa-romero hatchback ... 154 19 16500

3 3 audi sedan ... 102 24 13950

4 4 audi sedan ... 115 18 17450

[5 rows x 10 columns]

Last five rows

index company body-style ... horsepower average-mileage price

6 6 audi wagon ... 110 19 18920

7 9 bmw sedan ... 101 23 16430

8 10 bmw sedan ... 101 23 16925

9 11 bmw sedan ... 121 21 20970

10 13 bmw sedan ... 182 16 30760

[5 rows x 10 columns]
Most expensive car company and price

company price
10 bmw 30760 All
Toyota car details
Empty DataFrame
Columns: [index, company, body-style, wheel-base, length, engine-type, num-
of-cylinders, horsepower, average-mileage, price]

19

Index: []

Car details sorted on the price column

index company body-style ... horsepower average-mileage price

0 0 alfa-romero convertible ... 111 21 13495

3 3 audi sedan ... 102 24 13950

5 5 audi sedan ... 110 19 15250

79 bmw sedan ... 101 23 16430

1 1 alfa-romero convertible ... 111 21 16500

2 2 alfa-romero hatchback ... 154 19 16500

8 10 bmw sedan ... 101 23 16925

4 4 audi sedan ... 115 18 17450

6 6 audi wagon ... 110 19 18920

9 11 bmw sedan ... 121 21 20970

10 13 bmw sedan ... 182 16 30760

[11 rows x 10 columns]
Average mileage and price column

average-mileage price
0 21 13495
1 21 16500
2 19 16500
3 24 13950
4 18 17450
5 19 15250
6 19 18920
7 23 16430
8 23 16925
9 21 20970
10 16 30760

20

MATPLOTLIB

Q12. Write a program to plot the marks secured by a student in all
exams conducted in his school.
A12. Code:
import matplotlib.pyplot as plt
print('Enter the marks secured by the student')
marks = eval(input('Enter the marks: '))
plt.plot(marks)
plt.xlabel('Tests')
plt.ylabel('Marks secured')
plt.show()
Output:
Marks secured by the student
Enter the marks: 23, 53, 57, 67, 45, 45
Tests conducted throughout the year
Enter the name of the tests: 'ut1', 'ut2', 'Term1', 'ut3', 'ut4', 'Term 2'

21

Q13. First 10 terms of a Fibonacci series are stored in a list named Fib:
Fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Write a program to plot Fibonacci terms and their square-roots with
two separate lines on the same plot.

a. The Fibonacci series should be plotted as a cyan line
with „o‟ markers having size as 5 and edge-color as red

b. The square-root series should be plotted as black line with „+‟
markers having size 7 and edge color as red

A13. Code:
import matplotlib.pyplot as plt
import numpy as np
fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
sqfib = np.sqrt(fib)
plt.figure(figsize = (10, 7))
plt.plot(range(1, 11), fib, 'oc', markersize = 5, linestyle = 'solid', markeredgecolor = 'r')
plt.plot(range(1, 11), sqfib, 'k+', markersize = 7, linestyle = 'solid', markeredgecolor =
'r')
plt.show()

22

Q14. Here is the medals won by different countries in Commonwealth games
2018 stored in medals.csv. Convert the csv into Dataframe and write
programs to plot bar chart for the following

a) Medals won by Australia
b) Medals won by Australia and India
c) Represent the medals won by Australia with different colors
d) Medals won by top 4 countries
A14.
a. Code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv(“medals.csv”, index_col =
“Country”) g = df.loc[“Australia”, : ].tolist()
x = [“Gold”, “Silver”, “Bronze”, “Total”]
plt.bar(x, g)
plt.xlabel(“Medals won by Australia”)
plt.ylabel(“Medals won”)
plt.title(“AUSTRALIA MEDAL PLOT”)
plt.show()

23

b. Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("medals.csv",index_col
= "Country")
g = df.loc["Australia",:].tolist()
i = df.loc["India",:].tolist()
x = np.arange(4)
plt.bar(x+0.00,g, color = "b",width = 0.25)
plt.bar(x+0.25,i, color = "g",width = 0.25)
plt.xlabel("Medals won by Australia and India")
plt.xticks(x,("Gold","Silver","Bronze","Total"))
plt.show()

c. Code:
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("medals.csv",index_col =
"Country") g = df.loc["Australia",:].tolist()
x = ["Gold","Silver","Bronze","Total"]
colors =
["Gold","Yellowgreen","Lightcoral","Lightskyblue"]
plt.bar(x,g,color = colors)
plt.xlabel("Medals won by Australia")
plt.ylabel("Medals won")
plt.title("AUSTRALIA MEDAL PLOT")
plt.show()

24

d. Code:
import matplotlib.pyplotk as plt
df = pd.read_csv("medals.csv",index_col =
"Country") df.sort_values(by = ["Total"],ascending =
False, inplace = True)
countries = df.index.values.tolist()
con = countries[0:4]

x = np.arange(4)
l = df.values.tolist()
plt.bar(x+0.00,l[0],color = "r",width = 0.25)
plt.bar(x+0.25,l[1],color = "b",width = 0.25)
plt.bar(x+0.50,l[2],color = "g",width = 0.25)
plt.bar(x+0.75,l[3],color = "y",width = 0.25)
plt.xlabel("medals won by Top 4 countries")

Q16. Write a program in python to plot a histogram for the grouped

frequency table of the performance of the students that occurred in a class

Percentage Frequency
<33 2

33-50 4

50-60 10
60-75 12

75-85 15

>=85 17

A16. Code:
import matplotlib.pyplot as plt
hect = [20,40,52,72,80,90]
maco = [10,33,50,60,75,85,95]
rob = [2,4,10,12,15,17]

25

weights = rob, edgecolor = "yellow")
plt.xlabel("Marks obtained")
plt.ylabel("Frequency")
plt.title(“Histogram”)
plt.show()

Q17 Read the total profit of each month from the sales_data.csv and show it

using the histogram to see most common profit ranges.

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("sales_data.csv")
profitList = df["total_profit"].tolist()
labels = ["Low","Average","Good","Best"]
profit_range = [150000,175000,200000,225000,
250000,300000,350000]
plt.hist(profitList,profit_range, label= "Profit Data")
plt.xlabel("Profit Range in Dollars")
plt.ylabel("Actual Profit in Dollars")
plt.legend(loc = "upper left")
plt.xticks(profit_range)
plt.title("Profit Data")
plt.show()

Q18 Write a program in python to plot a histogram with random numbers
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100)
plt.hist(x,color = "green",edgecolor = "black",

26

label = "Random Numbers")
plt.legend(loc = "upper left")
plt.show()

Q19 Write a program to draw a
histogram of the total columns of
the medals.csv
import matplotlib.pyplot as
plt import pandas as pd
df = pd.read_csv("medals.csv")
data = df["Total"]
bins = 7
plt.hist(df["Total"],bins,facecolor="red",
edgecolor="black",label="Total Medals")
plt.legend(loc="upper right")
plt.show()

Q 20 Write a program to draw a histogram to plot the marks secured in two
subjects by the students

a) Vertical Histogram
b) Horizontal Histogram
c) Overlapping Vertical Histogram

a)

import matplotlib.pyplot as plt
eng = [77,65,82,99,57,65,42,31,69,83]
hind = [56,32,14,78,65,98,57,49,27,68]
plt.hist([eng,hind],5,edgecolor =
"black", label = ["English","Hindi"],alpha
= 0.5) plt.legend(loc = "upper right")
plt.show()

27

b)
import matplotlib.pyplot as plt
eng = [77,65,82,99,57,65,42,31,69,83] hind =
[56,32,14,78,65,98,57,49,27,68]
plt.hist([eng,hind],5,edgecolor = "black",orientation
= "horizontal",label = ["English","Hindi"],alpha =
0.5) plt.legend(loc = "upper right")
plt.show()

c)
import matplotlib.pyplot as plt
eng = [77,65,82,99,57,65,42,31,69,83]
hind = [56,32,14,78,65,98,57,49,27,68]
plt.hist(eng,5,facecolor = "red",edgecolor = "black",label = "English")
plt.hist(hind,5,facecolor = "yellow",edgecolor = "black",label = "Hindi")
plt.legend(loc = "upper right")
plt.show()

28

MY SQL

ACCNO CUST_NAME LOAN_AMOUNT INSTALMENTS INT_RATE START_DATE INTEREST

1 R.K. GUPTA 300000 36 12 2009-07-19

2 S.P. 500000 48 10 2008-03-22

SHARMA

3 K.P. JAIN 300000 36 2007-03-08

4 M.P. YADAV 800000 60 10 2008-12-06

5 S.P. SINHA 200000 36 13 2010-01-03

6 P. SHARMA 700000 60 13 2008-03-05

7 K.S. DHALL 500000 48 2008-06-05

Consider the database LOANS with the following table LOAN_AMOUNTS
Write SQL commands for the tasks 1 to 35 and write the output for the
SQL commands 36 to 50

1) Create the database LOANS.

mysql> CREATE DATABASE LOANS;

2) Use the database LOANS.

mysql> USE LOANS;

3) Create LOAN_ACCOUNTS and Insert values in it.

mysql> CREATE TABLE LOAN_ACCOUNTS(ACCNO INTEGER PRIMARY
KEY,CUST_NAME VARCHAR(15),INSTALMENTS INTEGER,INT_RATE
DECIMAL(5.2),START_DATE DATE,INTEREST DECIMAL(5.2));

29

mysql> INSERT INTO LOAN_ACCOUNTS VALUES(1,"R.K.
GUPTA",300000,36,12.00,"2009-07-19",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(2,"S.P.
SHARMA",500000,48,10.00,"2008-03-22",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(3,"K.P.
JAIN",300000,36,NULL,"2007-03-08",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(4,"M.P.
YADAV",800000,60,10.00,"2008-12-06",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(5,"S.P.
SINHA",200000,36,12.50,"2010-01-03",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(6,"P.
SHARMA",700000,60,12.50,"2008-03-05",NULL);
mysql> INSERT INTO LOAN_ACCOUNTS VALUES(7,"K.S.
DHALL",500000,48,NULL,"2008-06-05",NULL);

4) Display the details of all the loans
mysql> SELECT * FROM LOAN_ACCOUNTS;

30

5) Display the Accno., cust_name and loan_amount of all the loans
mysql> SELECT ACCNO, CUST_NAME, LOAN_AMOUNT
FROM LOAN_ACCOUNTS;

6) Display the details of all the loans with less than 40 instalments
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE INSTALMENTS<40;

7) Display the Accno and loan_amount of all the loans started before 01-
04-2009
mysql> SELECT ACCNO, LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE START_DATE < "2009-04-01";

31

8) Display the Int_Rate of all the loans started after 01-04-2009
mysql> SELECT INT_RATE FROM LOAN_ACCOUNTS
WHERE START_DATE > "2009-04-01";

9) Display the details of all the loans whose rate of interest is NULL mysql>
SELECT * FROM LOAN_ACCOUNTS WHERE INT_RATE IS NULL;

10) Display the details of all the loses whose rate of interest is not NULL
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE INT_RATE IS
NOT NULL;

32

11)Display the amount of various loans from the table Loan_accounts. A
loan_amount shoult appear only once.
mysql> SELECT DISTINCT(LOAN_AMOUNT) FROM LOAN_ACCOUNTS;

12)Display the number of instalments of various loans from the table
Loan_Accounts. An instalment should appear only once.
mysql> SELECT INSTALMENTS, COUNT(INSTALMENTS)
FROM LOAN_ACCOUNTS GROUP BY INSTALMENTS;

13)Display the details of all loans started after 31-12-2008 for which the
number of instalments are more than 36
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE START_DATE > 2008-
12-31 AND INSTALMENTS > 36;

33

14)Display the Cust_Name and Loan_Amount for all the loans which do not
have number of instalments 36
mysql> SELECT CUST_NAME, LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE NOT INSTALMENTS = 36;

15)Display the Cust_Name and Loan_Amount for all the loans for which the
Loan_Amount is less than 500000 or int_rate is more than 12
mysql> SELECT CUST_NAME, LOAN_AMOUNT FROM LOAN_ACCOUNTS
WHERE LOAN_AMOUNT < 500000 OR INT_RATE > 12;

16) Display the details of all the loans which started in the year 2009
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE
YEAR(START_DATE) = 2009;

34

17)Display the details of all the loans whose Loan_Amount is in the range
400000 to 500000
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE
LOAN_AMOUNT >=400000 OR LOAN_AMOUNT <= 500000;

18)Display the details of all the loans whose rate of interest is in the range
11% to 12%
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE INT_RATE
> INT_RATE*0.11 OR INT_RATE < INT_RATE*0.12;

35

19)Display the Cust_Nmae and Loan_Amount for all the loans for which the
number of instalments are 24,36 or 48(using IN operator)
mysql> SELECT CUST_NAME, LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE INSTALMENTS IN (24,36,48);

20)Display the details of all the loans whose Lona_Amount is in the range
400000 to 500000(using between)
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE
LOAN_AMOUNT BETWEEN 400000 AND 500000;

21)Display the details of all the loans whose rate of interest is in the range
11% to 12%(using between)
mysql> SELECT * FROM LOAN_ACCOUNTS WHERE INT_RATE
BETWEEN INT_RATE*0.11 AND INT_RATE*0.12;

36

22)Display the accno, custname, and loan amount for all the loans for which
the cust_name ends with “Sharma”
mysql> SELECT ACCNO,CUST_NAME,LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE CUST_NAME LIKE "%SHARMA";

23)Display the accno, custname, and loan amount for all the loans for which
the cust_name ends with “a”
mysql> SELECT ACCNO,CUST_NAME,LOAN_AMOUNT
FROM LOAN_ACCOUNTS WHERE CUST_NAME LIKE "%A";

24)Display the accno, custname, and loan amount for all the loans for which
the cust_name contains “a”
mysql> SELECT ACCNO,CUST_NAME,LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE CUST_NAME LIKE "%A%";

37

25)Display the accno, custname, and loan amount for all the loans for which
the cust_name do not contain “P”
mysql> SELECT ACCNO,CUST_NAME,LOAN_AMOUNT FROM
LOAN_ACCOUNTS WHERE CUST_NAME NOT LIKE "%P%";

26)Display the accno, custname, and loan amount for all the loans for which
the cust_name contains “a” as the second character
mysql> SELECT ACCNO,CUST_NAME,LOAN_AMOUNT
FROM LOAN_ACCOUNTS WHERE CUST_NAME LIKE "_A%";

27)Display the details of all the loans in ascending order of their
Loans_Amount
mysql> SELECT * FROM LOAN_ACCOUNTS ORDER BY LOAN_AMOUNT;

38

28) Display the details of all the loans in descending order of their Start_date
mysql> SELECT * FROM LOAN_ACCOUNTS ORDER BY START_DATE
DESC;

29) Display the details of all the loans in ascending order of their
Loans_Amount and within Loan_Amount in the descending order of
their Start_Date.
mysql> SELECT * FROM LOAN_ACCOUNTS ORDER BY
LOAN_AMOUNT, START_DATE DESC;

39

30) Put the interest rate 11.50% for all the loans with null int_rate
mysql> UPDATE LOAN_ACCOUNTS SET INT_RATE = 11.50
WHERE INT_RATE IS NULL;

31)Increase the interest rate by 0.5% for all the loans for which the loan
amount is more than 400000
mysql> UPDATE LOAN_ACCOUNTS SET INT_RATE =
INT_RATE+0.5 WHERE LOAN_AMOUNT > 400000;

32) For each loan replace interest with
(Loan_Amount*intrate*instalments)*12*100
mysql> UPDATE LOAN_ACCOUNTS SET INTEREST =
(LOAN_AMOUNT*INT_RATE*INSTALMENTS)*12*100;

33) Delete the record of all the loans whose start date ids before 2007
mysql> DELETE FROM LOAN_ACCOUNTS WHERE YEAR(START_DATE)
< 2007;

34) Delete the records of all the loans of “K.P. Jain”
mysql> DELETE FROM LOAN_ACCOUNTS WHERE CUST_NAME =
"K.P. JAIN";

40

35) Add another column Category of type CHAR(1) in the loan table
mysql> ALTER TABLE LOAN_ACCOUNTS ADD CATEGORY CHAR(1);

WRITE THE OUTPUTS: -

36) SELECT CUST_NAME
LENGTH(CUST_NAME),LCASE(CUST_NAME),UCASE(CUST_NAME)
FROM LOAN_ACCOUNTS WHERE INT_RATE < 11.00;

37) SELECT LEFT(CUST_NAME,3), RIGHT(CUST_NAME,3),
SUBSTR(CUST_NAME,1,3) FROM LOAN_ACCOUNTS WHERE INT_RATE
> 10.00;

38)SELECT RIGHT(CUST_NAME,3), SUBSTR(CUST_NAME,5) FROM
LOAN_ACCOUNTS;

41

39) SELECT DAYNAME(START_DATE) FROM LOAN_ACCOUNTS;

40)SELECT ROUND(INT_RATE*110/100,2) FROM LOAN_ACCOUNTS WHERE
INT_RATE>10;

41) SELECT POW(4,3), POW(3,4);

42

42) SELECT ROUND(543.5694,2), ROUND(543.5694), ROUND(543.5694,-1);
43) SELECT TRUNCATE(543.5694,2), TRUNCATE(543.5694,-1);
44) SELECT LENGTH(“PROF. M. L. SHARMA”);
45) SELECT CONCAT(“SHEIKH”,”HAROON”)”FULL NAME”;

46) SELECT YEAR(CURDATE()), MONTH(CURDATE()),DAY(CURDATE());

43

47)SELECT DATE(CURDATE()),
DAYOFMONTH(CURDATE()),DAYNAME(CURDATE());

48) SELECT LEFT (“UNICODE”,3), RIGHT(“UNICODE”,4);
49) SELECT INSTR(“UNICODE”,”CO”), INSTR(“UNICODE”,”CD”);
50) SELECT MID(“INFORMATICS”,3,4), SUBSTR(“PRACTICES”,3);

44


Click to View FlipBook Version