Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

5_6276252898103925673

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 61

Computer Science Practical File

Submitted by:
Student Name :
Class : XII - B
Subject : Computer Science (083)
INDEX
S.NO CONTENT
1 WAP to counts the number of alphabets, digits, uppercase, lowercase, spaces
and other characters (status of a string).
2 WAP to remove all odd numbers from the given list.

3 Program to enter any number and print factorial of this number. Also check
whether this number is prime number or not.
4 Program to find the max, min and mean of values from a inputted list.
5 Program to generate Fibonacci series.
6 Program to store student’s information in a dictionary and display
information on the basis of admission no.
7 Write a function roll_D (), that takes 2 parameters- the no. of sides (with
default
value 6) of a dice, and the number of dice to roll-and generate random roll
values
for each dice rolled. Print out each roll and then return one string “That’s
all”.
Example roll_D(6, 3)
⦿ 4
⦿ 1
⦿ 6

8 Write a program to swap the content with next value, if it is divisible by 7 so


that the resultant array will look like: 3,5,21,6,8,14,3,14.

9 Write a program to display those string which are starting with ‘A’ from the
given list
10 Write a program to show sorting of elements of a list step-by-step.

11 Write a user defined function to find the greatest common divisor between
two numbers.
12 Write a user defined function to display first n prime numbers.
13 Program to calculate income tax of an employee on the basis of basic salary
and total savings inputted by the user.
14 WAP to calculates the following using concepts of module:
1. Area of Circle
2. Circumference of a circle
3. Area of rectangle
4. Perimeter of a rectangle

15 WAP to print the number of occurrences of a substring into a line using built
in string function find().
16 Write a program to show and count the number of words in a text file
‘DATA.TXT’ which is starting with a word ‘The’, ‘the’.

17 Consider a binary file “Emp.dat” containing details such as empno: ename:


salary(separator‘:’). Write a python function to display details of those
employees who are earning between 20000 and 40000.

18 Write a program to enter the numbers and find Linear Search, Binary Search,
Lowest Number using array code with user defined functions.

19 Write a function to create a copy of file “test.txt” named as “file.txt” which


should convert the first letter of the file and the first alphabetic character
following a full stop into upper case.
20 Anant has been asked to display all the students who have scored less than 40
for Remedial Classes. Write a user-defined function to display all those
students who have scored less than 40 from the binary file “Student.dat” and
copy their records to new file “stud.dat”

21 Create a CSV file “Groceries” to store information of different items existing


in a shop. The information is to be stored w.r.t. each item code, name, price,
qty. Write a program to accept the data from user and store it permanently in
CSV file.
22 Write a menu-driven program implementing user-defined functions to
perform different functions on a csv file “student” such as:
(a) Write a single record to csv. (b) Write all the records in one single go onto
the csv.
(c) Display the contents of the csv file.
23 WAP to add, delete and display the records using list implementation
through stack.
24 Write a Program to show MySQL database connectivity in python to insert
records in a table and displaying records.

25 WAP with My SQL and Python connectivity for searching a record on a


condition from a table.

26 WAP with My SQL and Python connectivity for updating a record on a


condition from a table.

27 WAP with My SQL and Python connectivity for deleting a record on a


condition from a table.

28 WAP with My SQL and Python connectivity for showing records on a


condition from a table.

29-33 My SQL queries

Practical Assignment No.1


Program 1: WAP to count the number of alphabets, digits, uppercase,
lowercase, spaces and other characters (status of a string).
x=input('Enter a string:')

la,ua,d,s,c=0,0,0,0,0

#la=lowercase ua=uppercase d=digit s=space c=other characters

for i in x:

if i in 'qwertyuioplkjhgfdsazxcvbnm':

la=la+1

elif i in 'QWERTYUIOPLKJHGFDSAZXCVBNM':

ua=ua+1

elif i in '1234567890':

d=d+1

elif i==' ':

s=s+1

else:

c=c+1

print('No. of:')

print('Alphabet:',la+ua,'\nLoewcase:',la,'\n Uppsercase:',ua)

print('Digits:',d,'\nSpaces:',s,'\nOther characters:',c)

Output

Practical Assignment No.2


Program 2: WAP to remove all odd numbers from the given list.
print('Enter no. to the list:')
l=[]

while True:

x=int(input('Enter:'))

l.append(x)

y=input('Do you want to add more?(y/n)')

if y=='n':

break

print('Original list:',l)

for i in l:

if i%2==0:

continue

else:

a=l.remove(i)

print('New list:',l)

Output

Practical Assignment No.3


Program 3: Program to enter any number and print factorial of this
number. Also check whether this number is a prime number or not.
x=int(input('Enter a no.'))

b=x

y=x//2

a=0

for i in range(2,y+1):

if x%i==0:

a=a+1

if a==0:

print('Its a Prime no.')

else:

print('Its a Composite no.')

f=1

for z in range(1,b+1):

f=f*z

print('Factorial of',b,'=',f)

Output

Practical Assignment No.4


Program 4: Program to find the max, min and mean of values from an
inputted list find the max, min and mean of values from an inputted
list.
print('Enter no. to the list:')

l=[]

ma=0

while True:

x=int(input('Enter:'))

l.append(x)

y=input('Do you want to add more?(y/n)')

if y=='n':

break

if l[0]>l[1]:

ma=l[0]

mi=l[1]

else:

ma=l[1]

mi=l[0]

for i in range(2,len(l)):

if l[i]>=ma:

ma=l[i]

for i in range(2,len(l)):

if l[i]<=mi:

mi=l[i]

print('Max value:',ma,'Min value',mi,end=' ')

s=0

for p in l:

s+=p

print('Mean:',s/len(l))
Output

Practical Assignment No.5


Program 5: Program to generate Fibonacci series.
x=int(input('Enter no. of terms:'))

print('Fibonacci Series:',end=' ')

a=0

b=1

c=a+b

print(a,b,end=' ')

for i in range(1,x-1):

print(c,end=' ')

a=b

b=c

c=a+b

Output

Practical Assignment No.6


Program 6: Program to store student’s information in a dictionary and
display information on the basis of admission no.
def detail():

while True:

a=int(input('Enter adm no:'))

n=input('Enter name:')

m=int(input('Enter marks:'))

q=input('Do you want to continue?(y/n):')

d[a]=[n,m]

if q=='n':

break

def show():

x=int(input('Enter adm no to search:'))

y=d[x]

print('Adm no:',x,'Name:',y[0],'Marks:',y[1])

d={}

while True:

print('1.Add student detail')

print('2.Show details')

print('3.Exit')

p=int(input('Enter choice:'))

if p==1:

detail()

elif p==2:

show()

elif p==3:

break
Output

Practical Assignment No.7


Program 7: Write a function roll_D (), that takes 2 parameters- the no.
of sides (with default value 6) of a dice, and the number of dice to roll-
and generate random roll values
for each dice rolled. Print out each roll and then return one string
“That’s all”.
Example roll_D(6, 3)
⦿ 4
⦿ 1
⦿ 6

def roll_D(side,dice):

import random

for i in range(dice):

x=random.randint(1,side)

print('Die',i+1,':',x)

print('That\'s all')

a=int(input('Enter no. of sides in a die:'))

b=int(input('Enter no. of die:'))

roll_D(a,b)

Output

Practical Assignment No.8


Program 8: Write a program to swap the content with next value, if it
is divisible by 7 so that the resultant array will look like:
3,5,21,6,8,14,3,14.
x=[3,21,5,6,14,8,14,3]

l=len(x)

i=0

print('Original list:',x)

while i<l:

if x[i]%7==0:

x[i],x[i+1]=x[i+1],x[i]

i=i+2

else:

i=i+1

print('New list:',x)

Output

Practical Assignment No.9


Program 9: Write a program to display those string which are starting
with ‘A’ from the given list
l=['Akshatt','Shubham','Adarsh','Gaurav','Aditya','Vivek']

for i in l:

if i[0] in 'A':

print(i)

Output

Practical Assignment No.10


Program 10: Write a program to show sorting of elements of a list
step-by-step.

print('1.Bubble sort')

print('2.Insertion sort')

print('3.Exit')

while True:

x=int(input('Enter choice:'))

if x==1:

l=[]

a=int(input('Enter no. of elements in a list:'))

for p in range(a):

b=int(input('Enter no.:'))

l.append(b)

print('Original list',l)

n=len(l)

for i in range(n-1):

for j in range(n-i-1):

if l[j]>l[j+1]:

l[j],l[j+1]=l[j+1],l[j]

print(l)

print('List after another pass',l)

if x==2:

l=[]

a=int(input('Enter no. of elements in a list:'))

for p in range(a):

b=int(input('Enter no.:'))

l.append(b)

print('Original list',l)

for i in l:

j=l.index(i)
while j>0:

if l[j-1]>l[j]:

l[j-1],l[j]=l[j],l[j-1]

print(l)

else:

break

j=j-1

print('List after another pass:',l)

if x==3:

break

Output

Practical Assignment No.11


Program 11: Write a user defined function to find the greatest
common divisor between two numbers.
def hcf(x,y):

if x>y:

s=y

else:

s=x

for i in range(1,s+1):

if x%i==0 and y%i==0:

ans=i

return ans

a=int(input('Enter 1 no:'))

b=int(input('Enter 2 no:'))

print('HCF is',hcf(a,b))

Output

Practical Assignment No.12


Program 12: Write a user defined function to display first n prime
numbers.
def primes():

p=int(input('Enter no. of primes to be calculated:'))

print('Prime no.:',end=' ')

j=0

x=2

while j!=p:

y=x//2

a=0

for i in range(2,y+1):

if x%i==0:

a=a+1

if a==0:

print(x,end=' ')

x=x+1

j=j+1

else:

x=x+1

primes()

Output

Practical Assignment No.13


Program 13: Program to calculate income tax of an employee on the
basis of basic salary and total savings inputted by the user.
b=int(input('Enter the basic salary:'))

s=int(input('Enter total savings:'))

if b<=250000:

tax=0

elif b<=500000:

if s>150000:

s=150000

tot_income=b-s-250000

tax=tot_income*0.05

elif b<=1000000:

if s>150000:

s=150000

tot_income_5slab=500000-s-250000

tot_income_20slab=b-500000

tax=tot_income_5slab*0.05+tot_income_20slab*0.02

else:

if s>150000:

s=150000

tot_income_5slab=500000-s-250000

tot_income_30slab=b-1000000

tax=tot_income_5slab*0.05+tot_income_30slab*0.03+100000

print('Total income tax to be paid=',tax)

Output
Practical Assignment No.14
Program 14: WAP to calculates the following using concepts of
module:
1. Area of Circle
2. Circumference of a circle
3. Area of rectangle
4. Perimeter of a rectangle
Module Circle:

import math

def area(r):

return math.pi*(r**2)

def circumference(r):

return 2*math.pi*r

Module Rectangle:

def area(l,b):

return l*b

def perimeter(l,b):

return 2*(l+b)

Main program:

import Circle

import Rectangle

while True:

print('1.Area of Circle')

print('2.Circumference of Circle')

print('3.Area of Rectangle')

print('4.Perimeter of Rectangle')

print('5.Exit')

x=int(input('Enter choice:'))

if x==1:

r=int(input('Enter radius of circle:'))


print('Area of circle:',Circle.area(r))

elif x==2:

r=int(input('Enter radius of circle:'))

print('The circumference is',Circle.circumference(r))

elif x==3:

l=int(input('Enter length:'))

b=int(input('Enter width:'))

print('The area is:',Rectangle.area(l,b))

elif x==4:

l=int(input('Enter length:'))

b=int(input('Enter width:'))

print('The perimeter of rectangle',Rectangle.perimeter(l,b))

elif x==5:

break

else:

print('Invalid')
Output
Practical Assignment No.15
Program 15: WAP to print the number of occurrences of a substring
into a line using built in string function find().
x=input('Enter any string:')

y=input('Enter the sub string to be searched:')

c=0

i=0

while i<len(x):

if x.find(y,i)!=-1:

m=x.find(y,i)

i=m+1

c=c+1

else:

i=i+1

print('The substring occurs',c,' times in the string.')

Output
Practical Assignment No.16
Program 16: Write a program to show and count the number of words
in a text file ‘DATA.TXT’ which is starting with a word ‘The’, ‘the’.
Data.txt:

The sky is blue.

I live in Delhi.

The flowers are blooming.

Its Sunday afternoon.

Main Program:

f=open('Data.txt','r')

a=f.readlines()

i=0

while i<=len(a)-1:

c=0

if a[i][0] in 'Tt' and a[i][1] in 'hH' and a[i][2] in 'Ee':

print(a[i])

for j in a[i]:

if j==' ':

c=c+1

print('No. of words in this sentence starting with The:',c+1)

i+=1

f.close()

Output
Practical Assignment No.17
Program 17: Consider a binary file “Emp.dat” containing details such
as empno: ename: salary(separator‘:’). Write a python function to
display details of those employees who are earning between 20000
and 40000.
import pickle

f=open('Emp.dat','rb')

x=pickle.load(f)

print(x)

for y in x:

if y['Salary']>=20000 and y['Salary']<=40000:

print('Records with salary between 20000 and 40000 are:')

print(y)

f.close()

Output
Practical Assignment No.18
Program 18: Write a program to enter the numbers and find Linear
Search, Binary Search, Lowest Number using array code with user
defined functions.
def linear_search():

l=[]

x=int(input('Enter no. of values for list:'))

for i in range(x):

y=int(input('Enter no:'))

l.append(y)

print('Original list:',l)

n=int(input('Enter no. to search:'))

f=0

p=0

while p<len(l):

if l[p]==n:

f=1

p=p+1

break

else:

p=p+1

if f==1:

print(n,'is present at position',p)

else:

print(n,'not found')

def binary_search():

l=[]

x=int(input('Enter no. of values for list:'))

for i in range(x):

y=int(input('Enter no:'))

l.append(y)
print('Original list:',l)

l.sort()

print('Sorted list:',l)

n=int(input('Enter no. to search:'))

mid=len(l)//2

low=0

high=len(l)-1

while l[mid]!=n and low<=high:

if n>l[mid]:

low=mid+1

else:

high=mid-1

mid=(low+high)//2

if low>high:

print('Element is at None')

else:

print('Element is at',mid+1)

def lowest_no():

print('Enter no. to the list:')

l=[]

while True:

x=int(input('Enter:'))

l.append(x)

y=input('Do you want to add more?(y/n)')

if y=='n':

break

if l[0]>l[1]:

mi=l[1]

else:

mi=l[0]

for i in range(2,len(l)):
if l[i]<=mi:

mi=l[i]

print('Lowest no:',mi)

while True:

print('1.Linear search\n2.Binary search\n3.Find lowest no.')

q=int(input('Enter choice:'))

if q==1:

linear_search()

elif q==2:

binary_search()

elif q==3:

lowest_no()

else:

break

Output
Practical Assignment No.19
Program 19: Write a function to create a copy of file “test.txt” named
as “file.txt” which should convert the first letter of the file and the
first alphabetic character following a full stop into upper case.
test.txt:

i live in new delhi.i am studying python.i am in class XII

Main code:

def capital():

f1=open('test.txt','r')

f2=open('text.txt','w')

while True:

line=f1.readline()

if not line:

break

line=line.lstrip()

line=line.rstrip()

l=len(line)

s=''

s=s+line[0].upper()

i=1

while i<l:

if line[i]=='.':

s=s+line[i]

i=i+1

if i>=l:

break

s=s+line[i].upper()

else:

s=s+line[i]

i=i+1

f2.write(s)
f1.close()

f2.close()

capital()

Output
Practical Assignment No.20
Program 20: Anant has been asked to display all the students who
have scored less than 40 for Remedial Classes. Write a user-defined
function to display all those students who have scored less than 40
from the binary file “Student.dat” and copy their records to new file
“stud.dat”
def main():

import pickle

f=open('Student.dat','rb')

f1=open('Stud.dat','wb')

x=pickle.load(f)

print('All records:',x)

print('Student to go for extra classes:')

for y in x:

if y[2]<=40:

a=y

pickle.dump(a,f1)

print(a)

f.close()

f1.close()

main()

Output
Practical Assignment No.21
Program 21: Create a CSV file “Groceries” to store information of
different items existing in a shop. The information is to be stored
w.r.t. each item code, name, price, qty. Write a program to accept the
data from user and store it permanently in CSV file.
import csv

f=['Code','Name','Price','Quantity']

rows=[]

while True:

c=input('Enter code:')

n=input('Enter name:')

p=int(input('Enter price:'))

q=int(input('Enter quantity:'))

r=[c,n,p,q]

rows.append(r)

x=input('Enter more?')

if x in 'Nn':

break

filename='Groceries.csv'

with open(filename,'w',newline='')as y:

w=csv.writer(y)

w.writerow(f)

for i in rows:

w.writerow(i)
Output
Practical Assignment No.22
Program 22: Write a menu-driven program implementing user-
defined functions to perform different functions on a csv file
“student” such as:
(a) Write a single record to csv.
(b) Write all the records in one single go onto the csv.
(c) Display the contents of the csv file.
def onebyone():
import csv
fields=['Name','Class','Marks']
rows=[['Aman',12,95],
['Aditya',11,93],
['Adarsh',12,90],
['Gaurav',11,91],
['Vivek',12,92]]
with open('student.csv','w',newline='')as f:
w=csv.writer(f)
w.writerow(fields)
for i in rows:
w.writerow(i)
print('File written one by one')
def onego():
import csv
fields=['Name','Class','Marks']
rows=[['Aman',12,95],
['Aditya',11,93],
['Adarsh',12,90],
['Gaurav',11,91],
['Vivek',12,92]]
with open('student.csv','w',newline='')as f:
w=csv.writer(f)
w.writerow(fields)
w.writerows(rows)
print('File written in one go')
def display():
import csv
f=open('student.csv','r')
r=csv.reader(f)
for i in r:
print(i)
f.close()
while True:
print('1.Write records one by one')
print('2.Write records in one go')
print('3.Display')
print('4.Exit')
p=int(input('Enter choice:'))
if p==1:
onebyone()
elif p==2:
onego()
elif p==3:
display()
elif p==4:
break
else:
print('Invalid')

Output
Practical Assignment No.23
Program 23: WAP to add, delete and display the records using list
implementation through stack.
l=[]

while True:

print('1.Add Record')

print('2.Delete Record')

print('3.Display')

print('4.Exit')

x=int(input('Enter choice:'))

if x==1:

a=input('Enter a record:')

l.append(a)

elif x==2:

if l==[]:

print('Empty record')

else:

print('Record deleted:',l.pop())

elif x==3:

i=len(l)

print('Stack is:')

while i>0:

print(l[i-1])

i=i-1

elif x==4:

break

else:

print('Invalid')
Output
Practical Assignment No.24
Program 24: Write a Program to show MySQL database connectivity in
python to insert records in a table and display records.
def insert():

import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('insert into student values(1,"Adarsh",17,85)')

x.execute('insert into student values(2,"Aditya",18,92)')

x.execute('insert into student values(3,"Aman",16,95)')

x.execute('insert into student values(4,"Gaurav",18,90)')

x.execute('insert into student values(5,"Shubham",17,91)')

d.commit()

print('Records inserted!!')

def show():

import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('select*from student')

a=x.fetchall()

for b in a:

print(b)

while True:

print('1.Insert values')

print('2.Show details')

p=int(input('Enter choice:'))

if p==1:
insert()

elif p==2:

show()

else:

break

Output
Practical Assignment No.25
Program 25: WAP with My SQL and Python connectivity for searching
a record on a condition from a table.
import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('select*from student where Rno=3')

a=x.fetchall()

for y in a:

print(y)

Output
Practical Assignment No.26
Program 26: WAP with My SQL and Python connectivity for updating a
record on a condition from a table.
import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('update student set Marks=95 where Rno=5')

d.commit()

Output
Practical Assignment No.27
Program 27: WAP with My SQL and Python connectivity for deleting a
record on a condition from a table.
import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('delete from student where Rno=3')

d.commit()

Output
Practical Assignment No.28
Program 28: WAP with My SQL and Python connectivity for showing
records on a condition from a table.

import mysql.connector

d=mysql.connector.connect(host='localhost',\

user='root',passwd='1234',\

database='school')

x=d.cursor()

x.execute('select*from student where Age=17')

a=x.fetchall()

for y in a:

print(y)

Output
My SQL Queries:
Practical Assignment No.29

Program 29: Consider the following tables STORE and SUPPLIERS.


Write SQL commands.
Table: Store

ITEMNO. ITEMS SCODE QTY RATE LASTBUY


2005 Sharpener classic 23 60 8 2009-01-31
2003 Ball Pen 0.25 22 50 25 2010-02-01
2002 Gel Pen Premium 21 150 12 2010-02-24
2006 Gel Pen Classic 21 250 20 2009-03-11
2001 Eraser Small 22 220 6 2009-01-19
2004 Eraser Big 22 110 8 2009-12-02
2009 Ball Pen 0.5 21 180 18 2009-11-03

Table: Suppliers

SCODE SNAME
21 Premium stationery
23 Soft plastics
22 Tetra supply
TO CREATE TABLE:

create table store


-> (Itemno integer(4),
-> Item varchar(20),
-> Scode integer(2),
-> Qty integer(3),
-> Rate integer(2),
-> LastBuy date);

create table suppliers


-> (Scode integer(2),
-> Sname varchar(20));

TO INSERT DATA :

insert into store values (2005,'Sharpener Classic',23,60,8,'2009-01-


31');
insert into store values (2003,'Ball Pen 0.25',22,50,25,'2010-02-01');
insert into store values (2002,'Gel Pen Premium',21,150,12,'2010-02-
24');
insert into store values (2006,'Gel Pen Classic',21,250,20,'2009-03-
11');
insert into store values (2001,'Eraser Small',22,220,6,'2009-01-19');
insert into store values (2004,'Eraser Big',22,110,8,'2009-12-02');
insert into store values (2009,'Ball Pen 0.5',21,180,18,'2009-11-03');

insert into suppliers values (21,'Premium Stationer');


insert into suppliers values (23,'Soft Plastics');
insert into suppliers values (22,'Tetra supply');
Table:

(i). To display details of all the items in the store table in ascending order of Lastbuy.
Select * from store
Order by LastBuy;

(ii). To display itemno and item name of those items from store table whose rate is more than
15 rupees.
Select * from store
Where rate>15;
(iii). To display the details of those items whose supplier code is 22 or quantity in store is more
than 110 from table store.
Select * from store
Where scode=22 or qty>110;

(iv). To display minimum rate of items for each supplier individually as per scode from the table
store.
Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=21;

Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=22;

Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=23;

Practical Assignment No.30


Program 30: Write SQL commands for (i) to (v) on the basis of relation
given below.
TABLE: BOOKS

Book_id Book_name Author_name Publisher Price Type Qty


s
k0001 Let us C Sanjay Epb 450 Comp 15
Mukharjee
p0001 Genuine J. Mukhi First publ. 755 Fiction 24

m0001 Mastering C++ Kantkar Epb 165 Comp 60

n0002 VC++ Advance P. Purohit Tdh 250 Comp 45

k0002 Programming Sanjeev First publ. 350 Fiction 30


with Python
TABLE: ISSUED

Book_id Qty issued


L02 13
L04 5
L05 21
TO CREATE TABLE :

Create table books


(book_id varchar(10),
Book_name varchar(20),
Author_name varchar(20),
Publishers varchar(10),
Price integer(3),
Type varchar(10),
Qty integer(2));
Create table issued
(Book_id varchar(5),
Qty_issued integer(2));

TO INSERT DATA :

Insert into books values('k0001','Let us C','Sanjay Mukharjee','Epb' ,


450,'Comp',15);
Insert into books values('p0001','Genuine','J. Mukhi','First publ',
755,'Fiction',24);
Insert into books values('m0001','Mastering C++','Kantkar', 'Epb',
165,'Comp',60);
Insert into books values('n0001','VC++ Advance','P. Purohit', 'Tdh',
250,'Comp',45);
Insert into books values('k0002','ProgramingwithPython','Sanjeev',
'First publ',350,'Fiction',30);

Insert into issued values('L02',13);


Insert into issued values('L04',5);
Insert into issued values('L05',21);
Table:

(i). To show the books of First publ written by P. Purohit.


Select book_name from books Where publishers = ‘first publ’ And
author_name = ‘p.purohit’;

(ii) To display cost of all the books published for FIRST PUBL.
Select price,book_name from books
Where publishers = ‘first publ’;

(iii) To display the BOOK_NAME and price of the books, more than 3 copies of which have been
issued.
Select book_name , price from books
Where qty > 3;
(iv) To show total cost of books of each type.
Select type,sum(price) from books
Where type = ‘Comp’;

Select type,sum(price) from books


Where type = ‘Fiction’;

(v) To show the details of the costliest book.


Select * from books
Where price = (select max(price) from books) ;
Practical Assignment No.31
Program 31: Write SQL commands for (i) to (v) on the basis of relation
given below.
Table: Company

Comp_id Comp_name Comp_HO Contact_person


1 Titan Okhla C.B. Ajit
2 Ajanta Najafgarh R.Mehta
3 Maxima Shahdara B.Kohly
4 Seiko Okhla R.Chadha
5 Ricoh Shahdara J.Kishore

Table: Model

Model_id Comp_id Cost Date_of_manufacture


T020 1 2000 2010-05-12
M032 4 7000 2009-04-15
M059 2 800 2009-09-23
A167 3 1200 2011-01-12
T024 1 1300 2009-10-14
TO CREATE TABLE :

create table company


(comp_id integer(2),
comp_name varchar(15),
comp_ho varchar(20),
contact_person varchar(20));

create table model


(model_id varchar(10),
comp_id integer(2),
cost integer(5),
date_of_manufacture date);

TO INSERT DATA :

insert into company values(1,'Titan','Okhla','C.B.Ajit');


insert into company values(2,'Ajanta','Najafgarh','R.mehta');
insert into company values(3,'Maxima','Shahdara','B.Kohly');
insert into company values(4,'Seiko','Okhla','R.Chadha');
insert into company values(5,'Ricoh','Shahdara','J.Kishore');

insert into model values('T020',1,2000,'2010-05-12');


insert into model values('M032',4,7000,'2009-04-15');
insert into model values('M059',2,800,'2009-09-23');
insert into model values('A167',3,1200,'2011-01-12');
insert into model values('T024',1,1300,'2009-10-14');

Table:

(i) To display the details of all the models in the Model table in ascending order of
DateOfManufacture.
select * from model order by Date_Of_Manufacture;
(ii)To display the details of those models manufactured in 2011 and whose Cost is below 2000
select * from model where year(Date_Of_Manufacture) = 2011 and cost <
2000;

(iii) To decrease the cost of all the models in Model table by 15%.
Update Model
set Cost = Cost – 0.15*Cost;

(iv) To show the number of distinct comp_ho from company table.


Select COUNT(DISTINCT Comp_HO) from Company;

(v) Show the company name and contact person from company table where comp_ho is okhla.
Select comp_name,contact_person from company

Where comp_ho = ‘Okhla’;


Practical Assignment No.32
rogram 32: Write SQL commands for (i) to (v) on the basis of relation
given below.
Table: CARDEN

Ccode Car_name Make Colour Capacity Charges


501 A-Star Suzuki Red 3 14
503 Indigo Tata Silver 3 12
502 Innova Toyota White 7 15
509 SX4 Suzuki Silver 4 14
510 S class Mercede Red 4 35
s

TO CREATE TABLE :
Create table carden
(Ccode integer(4),
Car_name varchar(10),
Make varchar(15),
Colour varchar(10),
Capacity integer(2),
Charges integer(3));

TO INSERT DATA :
Insert into carden Values(501,’A-Star’,’Suzuki’,’Red’,3,14);
Insert into carden Values(503,’Indigo’,’Tata’,’Silver’,3,12);
Insert into carden Values(502,’Innova’,’Toyota’,’White’,7,15);
Insert into carden Values(509,’SX4’,’Suzuki’,’Silver’,4,14);
Insert into carden Values(510,’S class’,’Mercedes’,’Red’,4,35);

Table:

(i) To display the names of all the silver-coloured cars.


SELECT CarName FROM carden WHERE Color LIKE ‘Silver’;

(ii) To display name, make and capacity of cars in descending order of their sitting capacity.
SELECT CarName,Make,Capacity FROM carden ORDER BY Capacity DESC;

(iii) To display the highest charges at which a vehicle can be hired from CARDEN.
SELECT MAX(Charges) FROM carden;
(iv) Display the number of different MAKE from carden table.
SELECT COUNT(DISTINCT Make) FROM CARDEN;

(v) Display the car_name where capacity of car is 4.


SELECT CarName FROM CARDEN WHERE Capacity=4;
Practical Assignment No.33
Program 33: Write SQL commands for (i) to (v) on the basis of relation
given below.

Table: Orders

Order_id P_name Qty Rate Sale_date Discount


1001 Pen 10 20 2019-10-05
1002 Pencil 20 10 2019-10-21
1003 Book 10 100 2019-11-02 50
1004 Eraser 100 5 2019-12-05 25
1005 Copy 50 20 2019-12-10

TO CREATE TABLE :
Create table Orders
(Order_id integer(5),
P_name varchar(10),
Qty integer(4),
Rate integer(4),
Sale_date date,
Discount integer(3));

TO INSERT DATA :
Insert into Orders Values(1001,’Pen’,10,20,’2019-10-05’,Null);
Insert into Orders Values(1002,’Pencil’,20,10,’2019-10-21’,Null);
Insert into Orders Values(1003,’Book’,10,100,’2019-11-02’,50);
Insert into Orders Values(1004,’Eraser’,100,5,’2019-12-05’,25);
Insert into Orders Values(1005,’Copy’,50,20,’2019-12-10’,Null);
Table:

(i) Write SQL query to display Pname, Quantity and Rate for all the orders that are either Pencil
or Pen.
SELECT P_name, Qty, Rate FROM Orders WHERE P_name IN(‘Pencil’,’Pen’);

(ii) Write SQL query to display the orders which are not getting any Discount.
SELECT * FROM Orders WHERE Discount is NULL;

(iii) Write SQL query to display the Pname, Quantity and Sale_date for all the orders whose total
cost (Quantity * Rate) is greater than 500.
SELECT P_name, Qty, Sale_date FROM Orders WHERE Qty * Rate > 500;

(iv) Write SQL query to display the orders whose Rate is in the range 20 to 100.
SELECT * FROM Orders WHERE Rate BETWEEN 20 AND 100;
(v) Write SQL query to display the minimum quantity, maximum
quantity and total quantity of Orders.
SELECT MIN(Qty), MAX(Qty), SUM(Qty) FROM Orders;

You might also like