Untitled 2 Ocr
Untitled 2 Ocr
FILE I: PROGRAM ON
FUNCTION DEFINING
Pt. W AP to define a function named greatest() which
receives three parameters the function returns the
greatest out of three. Also call the function.
def grcat(a,b,c):
if a>b and a>c:
x=a
elif b >a and b >c:
x=b
el~e:
X=C
return x
d =int(input('Enter 1st ntunber--'))
e=int(input('Enter 2nd nu1nber--'))
f int(input('Enter 3rd ntunber--'))
z=great(d,e,f)
print('Greatest nun1ber is--',z)
OUTPUT:
Enter 1st number--25
Enter 2nd number--55
Enter 3rd number--30
Greatest number is-55
OUTPUT:
(i)Enter number--5
Number is prime
(ii)Enter number--8
Number is not prime
a= int(input('Enter number--'))
z=fact_ value(a)
print('Factorial value--',z)
OUTPU'I':
Enter number--5
Factorial value- 120
def ar111(n):
nt=n
S=O
l1ile 11>0 :
, ,1
r = n %10
s =s+r**3
n =n //10
if S==nl:
print ('It is an At·mstrong number')
else:
print('It is 11ot a n Armstrong number')
a=int(input('Enter number--'))
z=arm(a)
OUTPUT:
Enter number--25
It is not an Armstrong number
Enter number--153
It is an Armstrong number
OUTPUT:
Enter number of natural nurnbers--5
Sum is- 15
x=eval(input('Enter List:'))
z=swap(x)
print ('Swapped List is:',z)
OUTPUT:
Enter List: [1,2,3,4,5,6]
Swapped List is: [2, 1,4,3,6,5]
P7, WAP to define a function reverse 0). '111e function
reverses the list passed as a parameter. Call the
function and dis la the reversed list.
f
,J, •f rc•v(I):
I>=[]
a = l,·11(1)
t, ,r i i11 r;11114l•(a- 1,- 1,-1):
J>=lfiJ
l>.appcnd(p)
r, ·I , 1r 11 I>
0l[[Pll1';
Enter List:[ 1,2,3,4,5,6]
llcversed List is:[6,5,4,3,2,1]
J
PS. WAP to define a function count O,n). The function
counts the occurrences of n in the list I and rettir11s it.
Call the function and display the total number of
occurrences. •I
.
d ef co unt(l,n):
C=()
a=le nO)
f<> r i in range(o,a):
if l[i] ==n:
C=C+l
pri nt(n, 'has occurred' ,c, 'times')
x= eval(input('enter List:'))
y=eval(input('enter number to be searched--'))
count(x,y)
OUTPUT:
Enter List:[11,22,25,24,36,40,22]
Enter number to be searched--22
22 has occurred 2 times
x=eval(input('Enter list:'))
y=sum (x)
OUTPUT:
Enter list:[14,20,25,22,10]
Sum of list elements-91
def greatestO):
g=o
a=lenO)
for i in range(o,a):
if l[i]>g:
g=l[i]
print('Greatest number--',g) ''
l
greatest(x)
0 UT: •'
>= 20000 A
10000 to 1 9 B
below 10000. C
I
I
I
z=int(input('Enter salary--'))
if Z>= 20000 :
x= ''A''
elif z>=10000 and a< 19999:
x= ''B''
else:
x= ''C''
print ('Employee belongs to',x,'category')
0 UT:
Enter salary--15000
Employee belongs to B category
.
!
float data type and calculate the co1n1nission and displ
ay
Sale Co0101ission
OUTPUT:
Enter sales an1ount--18000
Commission amount is--1440.0
h=soo
e I i f a> = 5 <><> ~• n d a< 1c>o c>:
b=:~oo
elst':
b=2oc>
)t"i 11t('Amou11t of fare--' ,b)
OUTPUT:
Enter distance(in l<llo1neters)--1100
Amount of fare--500
a =ope11(''n1e1110.L'<t'',''w'')
s=str(inpt1t('' enter string''))
a.write(s)
a.closeO
a=open('memo.txt', 'r')
s=a.read()
print('Content--' ,s)
0 UT:
Enter string--Rain goes to school
Content-- Ram goes to school.
P15. Read the text file inento.txt and count and display
the nuniber of vowels in it.
r - -- - - - -- - - - -- - - ~ - - -- - - -- -- - - - ----...
a =open('men10. txl', 'r')
s =a.read()
C=O
x=le11(s)
fc>1· i i11 ra11gc(o,x):
if s[i] ==''a'' <>r s[i ] ==''c'' <>r s[i] ==''i'' or s[i]==''o'' or s[i]==''u'':
C=C+l
D1·int(''Nt1111l1er of vowels i1re:'',c)
OUTPUT:
Number of vo\vels a1·e: 6
0 UT:
Number of alphabets--15
Number of digits--o
Number of symbols-1
P17. Write a 01enu driven progra1n with following
option 1. To write details of student to file 2. to display
all the details 3. to append details
a=open('stu.txt', 'w')
I
•
I
0 UT:
(i)Enter choice 1 or 2 or 3--1
Enter name,class and rollnumber respectively--
Harshita,12,1
(ii)Enter choice 1 or 2 or 3--2
Details of student:Harshita,12,1
(iii)Enter choice 1 or 2 or 3--3
Enter details of 2nd student--Alisha,12,2
Updated details: Harshita,12,1
Alisha,12,2
I
l
•
def pswd():
f =open ( '' password .txt'' , '' w '')
x=input(''Enter the password--'')
a =len(x)
c=''
fo1· i in ra11ge( o,a) :
0=01·d(x[i])
#pri11t(o)
c=c+cl11·(0+ 23)
pri11t('E11c1y1)ted l't1ssword--',c,end= ' ')
f.write(c)
f.close()
pswd()
OUTPUT:
Enter the password--Marial<Sp123
Encrypted Password--dxxHIJ
def pswdcrypt():
f= open(''password. bct'', ''r'')
x=f.read()
a=len(x)
c=''
for i in range(o,a):
o=ord(x[i])
#print(o)
c=c+chr( 0-23)
print('Decrypted Password--',c,end= ' ')
f.close()
l
pswdcrypt()
I
OUTPUT:
Dec~t_ed Password--Mariaksp123 --
OUTPUT:
I
a=open('student.txt','r')
s=a.read()
print(s)
a.close()
OUTPUT:
Abhay10A
Amit 11 C
Akshay12B
P22. Write a progrant to read a text file 01eino.txt and
display every alternate character in capital letter.
a=open('memo. txt','r')
s=a.read()
x=le11(s)
Sl= ''
for' i i11 ra11ge(o ,x,2):
s1=s[i]+s[i+1].upper()
PI'int('new: ',s1)
0 UT:
AbHaY 10 a
An1It 11 C
AkShAy 12 B
a=open ('memo.bct','r')
s=a.read()
s1 =s. title()
print(s1)
0 UT:
Ram Goes To School.
OUTPUT:
R
Amgoestoschool.
a=open('memo.txt','r')
s=''
C= O
,vhile True:
if s!='':
break '
I
C=C+ l
s=a.readline()
print(s,'is in line no.',c)
a.close()
a=open('memo.txt','a+')
fo1~i in range(o,3):
s=input('enter text')
a.write(s)
a.write('\n')
a.close()
OUTPUT:
Ram goes to School is in line number 1
He is a good boy is in line number 2
He studies Computer is in line number 3
I=open(''lower. txt'',''w'') I
I
OUTPUT:
E11ter cl1a1·actcr--a
Wa11t to e11te1· 11101·e cl1a1·nctc1·? y
E11ter cl1arncte1·--U
Wa11t to e11te1· 11101·e cl1a1·actc1·? Y
Enter cl1aracte1·--o
Wa11t to e11te1· 11101·e cl1aracter? y
Enter cl1a1·acte1·-- I<
Wa11t to enter mo1·e cl1a1·acte1·? Y
E11ter cl1aracter--4
Want to ente1· 1nore cl1aracter? y
Enter character ()
Want to enter more character? j
UK
Ao
4()
OUTPUT:
enter string 1-- It is 1·ai11ing ou tsidc.
enter stri11g 2--Lovcly W ea·tl1er
enter string 3--Time ·f or some p al<odas,
It is raining outside.
Lovely Weather
Tin1e for some palcodas.
a=ope11('letter .txt','r')
s=a.readline()
print(s)
s=a. read.line()
print(s)
s=a.readlineO
print(s)
OR
f= open('letter. txt','r')
S= '' ''
while s:
s.readline()
print(s)
.f [.] ,,,,
1 S 1 == :
breal<
f.close():
OUTPUT:
It is raining outside.
Lovely Weather.
OUTPUT:
It is raining outside.
a=open('letter. txt','r')
g=o
S= ,, ''
wl1ile True:
s=a.readline() I
j
x=len(s)
print('Number of characters--',x)
if x>g:
•
r - - - - - - - -- - - - - - - - - - -- - - -- - - -~-
g=x
Drint('lon ~est line:' ,g)
OU'fPUT:
Number of characters--22
Number of characters--15
Number of characters--23
Longest line: Time for some pakodas.
f= open('letlc1·. txt','1·')
s=f.read()
l=s.split()
Cl= O
C2= 0
10 1· i i11 l:
it' i = ='it' :
Cl=Cl+ l
elif i== 'It':
C2=C2+ 1
f.close()
0 UT:
It comes o times. •
it comes 1 times.
f= open('letter. txt','r')
s=f.read()
l=s.split()
for i in I:
a =len(i)
if a==3:
print(i)
f.close()
0 UT:
For
P33.Write a PrograD1 to read a text file letter.n.1: and
reverse and display eve1-y four letter word
f=open('lette1~.txt', '1·')
s =f. read()
l=s.split()
fo1· i i11 I:
if le11(i)==4 :
a =le11(l)
b=''
fo1· j in 1·a11ge(a -1,-1,-1) :
c=c+i[a]
print(c)
f.close()
OUTPUT:
emiTemos
def readfileO:
f= open('memo.txt', 'r')
s=f.read()
x=len(s)
C=O
for i in range(o,x):
if s[i]=='p' or s[i]=='P':
- - -- ------------
- - - - - - - - - - - - - - - - - - - - =~~ =="' =
c=c+ 1
Jlt·i11t('ll 011d 11 co111e',c,'ti1nes')
f.close()
rend file()
O\J'fPU'1::
P n11d p co111e 2 ti1ncs
oard ue ions
dl't· l)\11·etext():
f=ope11('MYNOTES.1 X'l~','1·') 1
s=a.read()
x=le11(s)
Sl=''
for i i11 ra11ge(o,x):
if s[i]=='l<':
Sl=Sl+'c'
elif s[i]=='K':
Sl=Sl+'C'
else:
Sl=Sl+s[i]
print(' corrected:',s1)
puretextO
0 UT:
I OWN A cu-·1·---E LI'ffLE CAR.
I CARE FOR THAT AS MY CI-IILD.
f=open('STORY.TXT','w')
l=['A lion','A tiger','There was','He is']
for i in 1:
f.write(i)
f. write('\n ')
f.close()
OUTPU'1::
(i)(tst choice) Number of Lines starting with A:2
(ii)(2nd choice)words with less than 4 characters--A
words with less tl1nn 4 cl1n1·acters--A
words with less thn11 4 cl1n1·nctcrs--was
words witl1 less thnn 4 c11n1·ncters--I-Ie
words witl1 less tl1n11 4 cl1nrnctcrs- is
def RevText():
f=ope11('Input.txt', 'r')
s=f.readline()
l=s.split()
c=''
for i in 1:
if i[o]=='I':
a=len(i)
for b in range(a-1,-1,-1):
c=c+i[b]
else:
•
C=C+l
print(c)
f.close()
g=open('Input. txt', 'w')
s='India Is my country'
g.write(s)
g.close()
RevText()
OU'I'PU'f:
aidnlslmy country
def ,vr():
f=ope11(' xyz.txt', 'w')
s=str(input('Write text:'))
f.write(s)
f.close()
def pr():
f=open('xyz. txt','r')
s=f.read()
print(s)
f.closeO
def app():
f=open('xyz.txt', 'a+')
s=f.write('content appended')
print(s)
f.close()
def upercase():
f=open('xyz.txt', 'r ')
s=f.read()
----
x=len(s)
fot' i i11 range( o,x):
A=s[i].upper()
p1·int(A,end=' ')
f.close()
OUTPUT:
(i)Ente1· a digit betwee11 1 a11d 4--1
Write t~\.1::Tl1is is Compute1· Lesson.
(ii)Enter a digit between 1 and 4--2
This is Computer Lesson.
(iii)Enter a digit between 1 and 4--3
This is Computer Lesson.content appended
(iv) Enter a digit between 1 and 4--4
TH IS IS COMPUTER LESSO N. C O NT ENT
APPENDED
1le8:bina 1les
P39.To show that print contniand can change printed v
alues in print co1ninand
import pickle
f =open ( ,, num.da t '' , '' wb'')
a=4
h=s
C=6
pickle.dump(a,f)
piclde.dump(b,f)
picltle.dump( c,f)
f.close()
OUTPUT:
956
import pickle
def bin():
f=open('emp.dat','wb')
l=[]
for i in range(o ,3):
a=int(input('Enter employee no.--'))
b=input('Enter name--')
c=int(input('Enter salary--'))
1.append(a)
l.append(b)
l.append(c)
picl<le.dumpO,O
f.close()
bin()
f=open('em p.dat', 'rb')
l=[]
l=picl<le.load(f)
print(l)
f.close()
OUTPUT:
(i)Enter en1ployee no.--1
Enter name--Alcsl1ay
Enter salary--10000
Enter en1ployee no.--2
Enter name--Abhay
Enter salary--12000
Enter employee no.--3
Enter name--Bhavesh
Enter salary-15000
(ii)[1, 'Akshay', 10000, 2, 'Abbay', 12000, 3, 'Bhavesh', 15000]
import pickle
f=open(' study1.dat', 'wb ')
l= □
for i in range(o,2):
a=int(input('enter rollno. '))
b=input('enter name')
c=float(input('enter marks'))
l.append(a)
l.append(b)
-------- - --
l.append(c)
pickle.dump(l,t)
f.close()
in11)ort pickle
f=open('study1 .dat', '1·b')
l=[]
l=pickle.load(t)
J)t·int(l)
f.close()
OUTPUT:
(i)Enter rollno.1
E11ter na m eAsl1111it
Enter n1a1·l<S80
Enter rollno.2
Enter name Brijesl1
Enter ma rl<S92.5
[1, 'Ashmit', 80.0 , 2, 'Brijesl1', 92.5]
lI
I
II
P42.Write a Function to read n111n1.dat and find the
s111n of even and odd nuntbers separately
/
def data2():
f= open('num1.dat', 'rb'~ ~-
s1=0
S2=0
fo r i in range(o ,10):
a=pickle.load(f)
if a%2==0 :
Sl=Sl+a
else:
S2=S2+a
print('Sum of odd--',s1)
print('Sum of even--',s2)
data2()
0 UT:
Sum of odd--138
Sum of even-48
in11Jort picltle
def data1():
f= ope11('nu1n1.dat', '1~b')
fo1· i i11 range(o ,10):
a =piclcle.load(t)
pri11t(a,end=' ')
f.close()
data1()
OUTPUT:
14 22 20 5 44 7 15 10 28 21
i111po1t picltle
def data():
f= open('num1.dat','wb')
for i in range(o ,10):
a=int(input('enter number'))
pickle.dump(a,f)
f.close()
data()
OUTPUT:
Enter number--14
Enter number--22
Enter number--20 -
Enter number--5
Enter number--44
Enter number--7
Enter number--15
Enter number--10
Enter number--28
Enter number--21
LE9:BIN _2NDSET
i1111)01·t picltle
d ef e111pl1():
f= open('en1p.dat', 'rb')
l=[]
t111:
,vl1ile True:
l=pickle.load(f)
printO)
except EOFError:
print('out of loop')
pass
f.closeQ
empl1()
0 UT:
[1, 'Aman', 9000, 'A']
[2, 'Anmol', 15000, 'A']
out of loop
P46.Write a Function to read the binary file emp.dat
and display the details of the elllployees who receive
salary inore than 10000.0
in1port pickle
def empl2():
f=open('emp.dat','rb')
l=[]
try:
,vl1ile True: •
l=pickle.load(t)
if 1[2] >10000:
pri11t(l)
except EOFE1~1"01·:
{Jass
f.close()
en1 12()
OUTPUT:
[2, 'Anmol', 15000, 'A']
in1port pickle
def employ():
f= open(' em p .dat', 'wb ')
l= □ •
I
try:
while True:
en=int(input('Enter employee number--'))
n=input('Enter name--')
s=int(input('Enter salary--'))
g=str(input('Enter grade--'))
I.a PJ)cnd (c11)
1.o ppe11d( 11)
l.a1>pe11d(s)
1.n ppe11d(g)
picl<lc.dt11n JJ(l, 0
I=[]
p L. str( i111>t1t ( 'cl<> y<>tl w ,1111 le> <'lll<'r· 111c,1·<' cl<·t.1i]s ·?:'))
i f J) r y C t
1
:
lll'(': 11,
(' X C' ( ' l)t J•:()JIJ•:1·1·<>1·:
I); l '-, ' ~
f.clos(~{)
c111ploy()
ou·rpu1":
E11tcr e1nployce 11t1111bc1·-- 1
Enter na111e--A111an
Enter sala1y--9000
E11ter grade--A
Do you want to enter more details?:y
Enter employee numbe1 --2 1
E11ter name--Anmol
E11ter salary-15000
Enter grade--A
Do you want to ent er more details?:n
I
Quantity
Rate
Menu
1. Enter new product details
2. Dis play the product according
to product number
3. Display details of product according to the date of
purchase
4. Display all the details
5. Delete a s ingle product
6. Update a product
7. EXIT
<lt•f· J)I'():~() :
t I'\'
•
:
f=<>1)c11('1)1·odt1ct.dttt', '1·b')
w=i111>ttt('E11tc1· date of pu1·cl1ase for the product whose <let
~lils to be dis played in ·fo rmat DD/ ~M/ --')
flag=<)
l =[]
\vl1 ile 1~1·ue:
l=piclde.load(f)
if l[2] ==W:
printO)
flag=l
f.close()
except EOFError:
if flag==O:
print('Product not found')
cxceJJt FileNotFou11dError:
print('File not found')
def pro4():
t1·y:
f=o en('product.dat', 'rb ')
p t·int('Displaying a ll product details')
l =[]
\V 11 iIe "f 1·t1c:
l=picl<le.load(O
printO)
cxcc1>t EOFError :
J)t\ SS
def pro6():
f=opc11('product.da t', 'r+ b')
e=int(i11pt1t('Enter prodt1ct nt1111bcr wl1osc details to be updat
ed--'))
L = []
A=int(i11put('E11ter new p1·odt1ct no.--'))
B=in1Jut('Entc1· nc\v product name--')
C=i11put('Enter pt1rcl1asc date--')
D=int(inpt1t('Enter qt1i111tity--'))
E=int(i11put('Enter new rate--'))
L.append(A)
L.append(B)
L.append(C)
L.append(D)
L.append(E)
l=[]
,vl1ile True:
x=f.tellO
l=pickle.load(O
if l[o]==e:
l=L
f.seek(x)
pickle.load(O
print('Updated')
f.close()
def pros():
import os
import pickle
try:
f= open('product.dat', 'rb ')
f1= open('product1.dat', 'wb')
--- ---- - -----------------
, vl1ilc 1,1·t1e:
z=int(input('''1)Enter 1 fo1· adding new p roduct d etails.
2)E11ter 2 fo1· displayi11g d e tails of product according to produ
ct nu1nber.
3)Enter 3 to searcl1 d etails for p1·oduct of particular date.
4)Enter 4 for displaying details of products.
s)Enter 5 fo1· deleting product of a particular product number
•
6)Enter 6 for updating details of ~-~oduct with specified produ
ct number.
7)Enter 7 for exiting file--''' ..
if Z== l :
pro1()
elif z== 2:
pro2()
elif z==3:
pro3()
elif z==4 :
pro40
elif Z==s :
pros()
elif z== 6:
pro6()
. . .
-- ---
-
t'lif' 1,cc7:
lll"('ll ),
~~ 1,1·i11t('l11v,1li<I')
OUTJ.>Ul'l
(i) 1)l.:11ter t f<>r lt<.l<.ling 110w pr·c>duct details.
2)E11tc1· 2 f<>r clis11loying <lotuils of product acco1·ding to product
11t1111be1·.
3)E11tcr 3 t<.) sct1rcl1 clcl11ils for p1·oduct of pn11:icular date.
4)E11tcr 4 fo1· displnyi11g dot11ils of JJrodttcts.
5)Ente1· 5 fot· deloti11g JJ1·odt1ct <>fa particular product number.
6)E11te1· 6 fo1· 11pdltli11g <lcti1ils of IJt·oduct witl1 specified product
11 t1111 bet',
7)E11te1· 7 for· cxiti11g filo--1
E11ter 1>1'0dt1ct 11t1111be1·--1
E11ter prodt1ct 11n111e--Soap
E11ter date of pu1·cl1ase in fo1--1nat DD/MM/ --10/02/2020
E11ter qunntity--10
E11ter rate--5
Do you want to add another p1·oduct?y or no:y
Enter product number--2
Enter product name--Powder
Enter date of purchase in format D / /MM --17/02/2020
Enter quantity--15
Enter rate--10
Do you want to add another product?y or no:no
number.
7)Enter 7 for ~'Xiting file--3
Enter date of purcl1ase for the product whose details to be
displayed in forn1at DD/MM -- I
I,
17/02/2020
[2, 'Powder', '17/02/2020', 15, 10]
number.
7)Enter 7 for exiting file--4
Displaying all product details
[1, 'Soap', '10/02/2020', 10, 5]
[2, 'Powder', '17/02/2020', 15, 10]
l=[] I
I I
,vl1ilc True: i I
a =str(inpttt('cntcr e1111>loycc no.'))
b =str(i11pt1t('cnte1· nt1111e'))
c=str(i11pt1t('e11tcr snli11)''))
l.append(a)
l.append(b)
l.append(c)
A.writerow(l)
I= []
p =i11put('do you ,va11t to enter anotl1er detail')
if p! ='y':
break
f.close()
f= open('student.csv', 'r')
A=csv.reader(f)
for B in A:
print(' '.join(B))
OU'fPUT:
(i)Enter employee number--1
Enter name--Chaitanya
Enter salary--20000
Do you want to enter another detail?:y
Enter employee number--2
Enter naine--Akshat
Enter salary--25000
Do you want to enter another detail?:n
(ii)E.no. name salary
1 Chirag 20000
2 Akshat 25000
-- -- ~-- -- --- - - - ~- - - --- - -- - ·-- --- - ----------- -
p o.
i111port csv
field = ['roll no' ,'na1ne','marlcs']
rows=[['1', 'abc','34.5'],['2','pqr' ,' 56.5'],['3', 'xyz', '98.5']]
f=open(''student.csv'', ''w'')
fp =csv.writer(f,delimiter= ',')
fp. writerow(field)
fp.writerows(rows)
f.close()
i111po1·t CSV
r =ope11(''stude11t.csv'', ''r'')
rs=csv.reade1·(r)
fo1· w i11 rs:
p1·int(' '.join(w))
r.close()
OUTPUT:
rollno name marks
1 abc 34.5
2 pqr 56.5
3 xyz 98.5
def PUSH_Fibo(N):
L= □
a=o '
b=l l
L.append(a)
L.append(b) •
C= O
- ---- ------ --- --- - ------ -
'I
print(L[j])
OUTPUT:
Enter the limit of Fibonacci Series:10
'
55 34 21 13 8 5 3 2 1 1 0 I II
I I
.I
'
I
'
def POP_NAME(N):
x=LEN(N)
k=o
if X == O :
print('Underflow')
else:
N.pop()
k= l
retu r·n k
L=eval(input('Enter List:'))
B=POP_ NAME(L)
print(A)
OUTPUT:
(i)Enter List:[1,2,3,4,5,6,7,8,9]
1
(ii)Enter List:[]
Underflow
0
P53. Write SQL queries for (i) to (iv) and find outputs for SQL queries (v)
to (viii), ,vhich nre bnsed on tl1e tables TRANSPORT nnd TRIP
TABLE: TRANSPORT
TCODE 1TYPE P13RKM
103 ORDINARY BUS 90
105 SUV 40
104 CAR 20
103 ORDINARY nus 90
101 VOLVO BUS 160
102 AC DELUXE nus 140
Note:
TABLE: TRIP
NO NAME ·roA"rE KM TCODE NOP
11 Thnish Khan 2015-12-13 200 101 32
13 Danish Sal,ai 2016-06-21 100 103 45
15 Ram Kumar 2016-02-13 350 102 .
~- -
LIi. "/ .
Note: j
'
• NO is Driver Number
• KM is Kilometer travelled
• NOP is number of travellers travelled in vehicle
• TDA TE is Trip Date
1. To display NO, NAME, TDA TE fro1n the table TRIP in descending order of
l
NO.
2. To display the NAME of the drive1·s fro1n the table TRIP who are traveling
by transport vehicle with code l O1 or 103.
3. To display the NO and NAME of those drivers ·fro1n the table TRlP who
travelled between '2015-02-10' and '2015-04-01'.
4. ·1·(J displny ull tl1c dct,,ils fr<l111 t,1lllc ·1·1{11> ir1 \vl1icl1 tt1c di~t,11,cc lr,1vc llcd is
r11orc tl1n11 I 00 KM ir1 nsccr1<.lir1g <lr<.lcr <lf' NC )I>
5. Sl~Ll~c ·1· (_'()l JN'I' c•), 'l'l'()l)I ~1:r(llll ·1·1t11,
GllOlJll l)Y 1'(.'()1)1 ~ I IAVNINC 1 (.'()lJr1' I' (•) .. I;
(,. Sl•:l..,l ~C'I' I>IS'l'IN(,'I' 'l'C()l)l •: f'r<>111 '1'1{11•:
7. Sl~l ~l ~t ~·1· A .·1·c~()IJI •:, NAMI ~. 'J"l' Yl>I ~
1: l{()M '1'1{11• A, ·1·t{ANSl,()lt'I' 11
WI 11~1{1~ A . ·1·c.~C>IJI ~-:, ll . ·1·t,()l)I ~ ANI> KM <' <JC);
8. Sl~l ~l: t··1· NAMI ~. KM • t•l ~l{KM
1:1{C>M ·1·1~11> A, 'l'l{ANSJ><>l{'I' ll
WI 11~1{1~ A. '1'('01)1 ~= ll . 'l'CC) l)I ~ANI) A. 'l'C(>IJJ ~= I ()5';
Ar1s,,·c1·:
PS4 Write SQL query to add a column total price witl1 datatype numeric
and size 10, 2 in a table product.
Answer:
ALTER TABLE PRODUCT ADD TOTAL PRICE NUMBER (10,2).
PSS Sonal needs to display name of teachers, who have ''0'' as the third
character in their name. She wrote the following query.
SELECT NAME FROM TEACHER WHERE NAME = ''$$0?'';
But the query is'nt producing the result. Identify the problem.
Answer:
The wildcards are incorrect. The corrected query is SELECT NAME FROM
TEACHER WHERE NAME LIKE' 0%'. I
I
•
Write SQL queries for:
Table: Applicants
ID Name Course Phone
1 l~vi BCA 421()716
2 Rita MBA 215611
3 Sunll MCA 321157
4 Kumar BCA 512771
Tobie: Councs
Nam~ Duration
MBA 2 ycnr 4(),000
MCA 3 yc11r 70,000
BCA 3 ear 20,000
1. l "'o display 11u111c, tee, gc11dc1·, .ioi11yct11· tlbot1t tl1c applica11ts, who have joined
bctbrc 20 10.
2. To display na111cs ol" ,lpJJlict,11ts, wl10 a1·c JJlaying fee more than 30000.
3. To display na111cs oJ"all applica11ts i11 ascending order of their joinycar.
4. l ~o displny tl1e year n11d tl1e total 11u111be1· of applicants joined in each YEAR
fro111 tl1c table APPLICANTS.
5. To display tl1e C_ID (i.e., Cot11·selD) a11d tl1e number of applicants registered
in tl1e course fron1 tl1e APPLICANTS and table.
6. To display tl1e applicant's nan1e witl1 tl1eir respective course's name from the
tables APPLICANTS and COURSES.
7. Give the output of following SQL state111ents:
o SELECT Name, Joinyear FROM APPLICANTS
WHERE GENDER= 'F' and C_ID= 'A02';
o SELECT MIN (Joinyear) FROM
APPLICANTS
WHERE Gender='m';
o SELECT A VG (Fee) FROM APPLICANTS
WHERE C- ID= 'AOT OR C - ID= 'A05';
o SELECT SUM- (Fee), C_ID FROM C_ ID
GROUPBYC- ID
HA YING COUNT(*)=2;
Answer:
1. SELECT NAME,FEE,GENDER,JOINYEAR
FROM APPLICANTS
WHERE J OINYE AR <2010
2. SELECT NAME FROM APPLICANTS WHERE FEE > 30000
3. SELECT NAME FROM APPLICANTS ORDERBY JOINYE AR ASC
4. SELEC~r YfJAI{ , COUN'l 1
1:l{OM
)•)
P56 Write SQL co111111nnds for tl1c queries (i) to (iv) 11nd output for (v) &
(viii) bnscd 011 11 t11blc COMPANY and CUSTOMER.
COMl,ANY
CID NAME CITY PRO DU CTNAME
111 SONY DELl·ll TV
222 NOKIA MUMUAI MOBILE
333 ONIDA DELHI TV
444 SONY MUMBAI MOBILE
555 BLACKBERRY MADRAS MOBILE
666 DELL DELl1I LAPTOP
/
CUSTOMER
I• • •
1. To display those company nrune which are having prize less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the prize by 1000 for those customer whose name starts with ,,S?
4. To add one more column totalprice with decimal] 10,2) to the table customer
5. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
·---- - - - - -- - - - - -
Ans,vcr:
P57 Answer the questions (o) and (b) on the basis of the following tables
SHOP andACCESSORIES.
Table: SHOP
Id ' SName Area
S001 ABC computronlcs er
5002 All lnfotech Media GK II
S003 Tech Shop er
S004 Geeks ll'Cno Soft Nel,ru Plac<?
5005 Hltech l ech Store Ncl,ru Pince
nble: ACCESSORIBS
No , Name Price Id
AOI Mother Boi\rd 12000 S01
A02 Hord Dbk 5000 S01
A03 Keyboard 500 S02
A04 Mouse 300 SOI
AOS Mother Board 1300(1 5()2
A06 Keyboard 400 S03
AO? LCD 6000 S04
T08 LCD 5500 sos
T09 Mouse 350 sos
TIO Hard Disk 4500 S03
I. To display Naine and Price of all the Accessories in ascending order of their
Price.
2. To display Id and SName of all Shop located in Nehru Place.
3. To display Minimum and Maximum Price of each Name of Accessories.
4. To display Name, Price of all Accessories and their respective SName where
they are available.
Answer:
(a)
1. Sl.:1. 1:(''I" Nutnc. l>ricc 1:ll(>M A('('l ·:SS()l{ll ~S ()l{l>l ~I{ llY J>ril:c A"c;
2. Sl~l. . 1:(,. 1. 11) SN~111,c 1:ll()M SI 1()1> WI 11 ~1{1 ·. Arc,, ··Ncl,rt1 l>l ,,cc··;
3. Sl: l.. l:c ·1· Nun1c, 111t1x (l>ricc); 111i11(l>ricc) 1:1t<>M A('C' l~S'-;() ltll ~S. Cir<>ltp l\y
Na,nc;
4. Sl: l,l:c ·1· Nt1111c,pricc, Stll\lllC 1:ll()M
A(,Ct":SS()l~ll :S, SI IC>J> WI 11~1{1 ~SI 1()1 ~
11)- A(~(,1:SSC>lt 11 ~S .11);
4
iI
I I
I
I I
I I
I
' I
I I
I
I
I I
I
I
I
'
I II
.I
' I
I '
: I
' i
I
. I
I :
I
I
I
I
I
I
I
-