Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

MySQLWorksheet

-1
(
DDL–Dat
abaseRel
atedcommands)
1. I
fa database"Empl
oyee"exi
sts,whi
ch My
Sqlcommand hel
psy
ou t
o st
artwor
king i
nthat
dat
abase?
UseEmployee;

2. Wri
teMySqlcommandwi
l
lbeusedt
oopenanal
readyexi
sti
ngdat
abase"
LIBRARY"
.
UseLi
brar
y;

3. Wri
teMySqlcommandt
oopenanexi
sti
ngdat
abase.
Usedat
abasename;

4. WhatdoesSQLst
andf or?Whati
sMy SQL?
SQLStandsf
orst
ruct
ur edquer
ylanguage.
Mysqli
sanopensourceRDBMS( Relati
onalDat
abaseManagementSy
stem)

5. Wri
tet
woexampl
esofDBMSsof
tware.
SQLServ
er,
MySQL,Or
acl
e,I
ngr
es,Post
gres

6. Shar
mil
awantst
omaket
hedat
abasenamed‘
COMPANY’
act
ive.Wr
it
eMy
SQLcommandsf
ori
t.
UseCompany
;

7. WhatisMySQL?
Mysqlisanopensour
ceRDBMS(
Rel
ati
onal
Dat
abaseManagementSy
stem)

8. Whatist
herelat
ionshi
pbetweenSQLandMySQL?
SQLisalanguagetogivecommandsinMySQLoranyot
herRDBMSsof
twar
e.

9. Ment
ionanytwoexampl
eofcommonDat
abaseManagementSy
stem.
SQLServ
er,I
ngres,
Post
gres,
MySQL

10. SuggestArchanasuitabl
ecommandf orthefol
l
owingpurpose:
i
. Todi spl
ayt
hel i
stofthedat
abasealr
eadyexi
sti
nginMy SQL.
i
i. Touset hedatabasenamedCity.
i
ii
. Toremov ethepre-exi
sti
ngdat
abasenamedClient
s.
i
.ShowDat abases;
i
i.UseCity
;
i
ii
.Dropdat abasecli
ents;

11. Wri
tethecommandt odi
spl
ayt
henameoft
heact
ivedat
abase.
Sel
ectDatabase(
);

12. Wri
tethecommandt ocreat
eanewdat
abase“
School

Cr
eatedatabaseschool
;

1
I
nformati
csPr
acti
ces
MySQLWor ksheet
-2
(
DDL–Tabl
eRel
atedcommandsexcludi
ngAl
tert
abl
e)
1. Wr
it
eanSQLquer
ytocr
eat
ethet
abl
e'Menu'
wit
hthef
oll
owi
ngst
ruct
ure:

Createt ableMenu(
I
temCodev ar
char(5)Primarykey,
I
temnamev archar(20)
,
Categor yVarchar(20),
Pri
ceDeci mal(5,
2));
2. Canat abl ehavemul ti
plepr
imarykeys?Canithav
emulti
plef
orei
gnkey
s?
No,at ablecannothav emul t
ipl
eprimarykey
s.Therecanonl
ybeoneprimar
ykey
.
Yes,at ablecanamul ti
plef
oreignkeys.

3. I
naSt udentt
abl
e,outofRol
lNumber,Name,AddresswhichcolumncanbesetasPrimar
ykeyand
why?
Rol
lNumbercanbesetasPri
maryKeyastwostudentscannothaveasamer
ollnumber
.

4. Ms.Miranawantstoremov
etheenti
recontentofatabl
e"BACKUP"al
ongwi
thi
tsst
ruct
uret
orel
ease
t
hestoragespace.WhatMy
Sqlst
atementshouldsheuse?
DropTableBackup;

5. Writ
eMySql
commandt
ocr
eat
etheTabl
eSTOCKi
ncl
udi
ngi
tsConst
rai
nts.
Tabl
eSTOCK:

Creat
et ableStock(
I
dDeci mal (
4)Primar yKey,
NameVar char(
20) ,
CompanyVar char(20),
Pri
ceDeci mal(8)NotNul l
);
6. Writ
eonesi mi l
ari
tyandonedi f
fer
encebet
weenCHARandVARCHARdataty
pes.
Si
mi l
ari
ty:
Bothcharandv archarcanstoreal
phabet
saswellasnumber
s.Bot
hcanstor
esamet
ypeofv
alues.

Di
ffer
ence:
Chari
saf i
xedl
engt
hchar
act
erdat
aty
pewher
easv
archari
sav
ari
abl
elengt
hchar
act
erdat
aty
pe.

7. Saumyahadprevi
ouslycreat
edatablenamed‘Pr
oduct
’inadat
abaseusi
ngMySQL.Lat
eronshe
for
gotthet
ablest
ructur
e.Suggesthersui
tabl
eMySQLcommandt hr
oughwhi
chshecancheckt
he
str
uct
ureoftheal
readycreat
edtable.
Descr
ibeProduct
;

8. Roliwantst
oli
stthenamesofallt
hetabl
esi
nherdat
abasenamed‘
Gadget
s’.Whi
chcommand(
s)she
shouldusetogetthedesi
redr
esul
t.
UseGadgets;
2
Showt abl
es;
9. Name t
he SQLcommandsusedto:
(
i)Phy
sical
lydel
eteat
abl
efrom t
hedat
abase.
(
ii
)Di
spl
ayt
hest
ruct
ureofat
abl
e.
i) Dropt ablet abl enae;
ii
) Descr i
bet abl ename;
10. Writeonesi milari
tyandonedi ff
erencebet weenUNI QUEandPRI MARYKEYconst r
aint
s.
Similarity:
BothUni queandpr i
mar ykeyr estrictsdupl i
catev aluesinthef ield.
Dif
fer ence:
Uniqueal l
owsnul lv
al ueswher easPr imarydoesnotal lownul lv aluestobeinser
tedinthef
iel
d.
11. Anat tributeAofdat atypev archar (
20)hast hev alue“Ami t”.Theat tr
ibut
eBofdat aty
pechar(
20)has
val
ue” Karani
ta”.How manychar actersar eoccupi edi nat t ri
buteA ?How manychar acter
sare
occupi edinat t
r i
buteB?
Awi l
loccupy4char acterspace.
Bwi l
l occupy20char act erspace.
12. Mrs.Shar mai sthecl asst eacherofCl ass‘XIIA’ Shewant stocreat eat abl
e‘St
udent

tostor edet ai
l
sofhercl ass.
i
)Whi choft hef ol
lowi ngcanbet heat tri
butesofSt udenttable?
a)Rol lNob)“ Ami t”c)Named)25
i
i)Namet hePr i
mar ykeyoft het able‘Student’.Statereasonf orchoosi ngit.
i) RollNoandNamecanbet heat tri
butesofst udentt abl e.
ii
) RollNocanbecomet hepr imar ykeyoft hest udentt abl
east wostudentscannothavea
samer ol
lnumber .
13. WriteSQLquer ytocr eat eat able‘ Player’
wi t
ht hefollowingstruct ure:

CreatetablePlayer
(
Player
idintegerpri
marykey,
Namev ar
char (
50),
Heightint
eger ,
Weightinteger,
Datebir
thdat e,
Teamnamev ar
char(
50))
;
14. Anitahascr eatedt
hefol
lowi
ngt
abl
ewi
tht
hename‘
Order
’.

Oneoft
her
owsi
nser
tedi
sasf
oll
ows:

(
i)Whatisthedat
atypeofcol
umnsOrder
IdandOr
der
Dat
eint
het
abl
eOr
der?
(i
i)Ani
taisnowtr
yingtoi
nser
tthef
oll
owi
ngrow:

3
Wil
lshebeabl etosuccessfull
yinsertit?Gi
ver eason.
i
)Thedat at
y pefororder
IDf i
eldcanbeei t
hercharorv ar
char
Thedat at
y pefororder
Dat eisdat e
i
i
) Shewi l
lnotbeablet oinserttheabover ecor
dasshei si
nser
ti
nganul lval
ueintheorder
dat
e
fi
eldandt heorderdatefi
eldhav eanotnul lconstr
aintwhichcannotacceptnullv
alues.
15. Wri
teSQLquer yt
ocreateat able‘Event’
wit
ht hefoll
owingstruct
ure:
Fiel
d Type Const rai
nt
EventI
d Varchar(5) PRIMARYKEY
EventName Varchar(30) NOTNULL
Locati
on Varchar(50)
Cli
entID Int
eger
EventDate Date

Creat
et abl
eEv ent(
EventI
Dv archar (
5)PrimaryKey,
EventNamev archar(
30)notnull,
Locati
onv archar(50),
Clei
ntIDInteger,
EventDatedat e);
16. Observethegi ventablecaref
ull
yandanswert
hef
oll
owi
ngquest
ions:

i
.Namethecolumnt hatmighthaveaPr i
mar yKeyconstrai
nt.Justi
fyyouranswer.
i
i
.Namet hecol
umnt hatmighthaveaUni queconstrai
nt.Justi
fyyouranswer .
i. PanNomi ghthaveaPr i
mar yKeyconst r
aintast woper soncannothaveasamePan
Number .
ii
. PhoneNo mi ghthav eauni queconst rai
ntast wo personwi l
lbehav i
ngdif
fer
entmobil
e
number s.
17. “
ABC”EventManagementCompanyr equiresdataofeventsthataretobeor gani
zed.Wr
it
eSQLquer
y
t
ocreat
eat abl
e‘Event’wi
ththefol
lowingstruct
ure:

Cr
eat
etabl
eEv
ent
(

4
Event I
DI ntegerPr imaryKey ,
EventVar char (
50) ,
DateEv entDat e,
NumPer for mer sInteger );
18. suggesthersui tablecommandf orthefoll
owi ngpurpose:
i
. Todi splayt helistofthedat abaseal readyexisti
ngi nMy SQL.
i
i. Touset hedat abasenamedCi ty
.
i
ii. Tor emov ethepr e-exist
ingdat abasenamedCl ients.
i
v . Tor emov eallther ecordsoft hetablenamed“ Club”atonegoal ongwit
hitsstr
uct
ure
per manent ly.
i. Showdat abases;
i
i. UseCi ty;
ii
i. Dr opdat abaseCl i
ents;
i
v. Dr opt abl eClub;
19. Whi l
ecr eat i
ngat ablenamed“ Employ ee”,
Mr .Rishigotconf usedaswhichdatatypeheshoul
dchose
forthecol umn“ EName”outofcharandv archar.Helphimi nchoosingtheri
ghtdataty
petostore
empl oyeename.Gi vev ali
djustifi
cationforthesame.
ENamef ieldcanhav eav archarasadat atypeast woempl oy eeswil
lnotbehavi
ngasamel engthof
theirnames.

Inf
ormati
csPracti
ces
MySQLWor ksheet
-3
(
DDL–TableRel
atedcommands)
1. Sahilcreatedat abl
einMy
sql.Lat
eronhef oundthatther
eshoul
dhav ebeenanot
hercol
umni
nthe
table.Whichcommandshouldheusetoaddanothercol
umnt ot
hetabl
e?
Altertabl
et abl
enameaddf
iel
dnamedataty
pe(size)
;

2. Whilecreat
ingatabl
e'Cust
omer'
Simrit
af orgottosetthepri
marykeyf
orthetable.Giv
ethest
atement
whichsheshouldwri
tenowtosetthecolumn' Cust
iD'asthepr
imar
ykeyofthetable?
Al
tertablecust
omeraddprimar
ykey (
custid);

3. Kuhuhasal
readycr
eat
edat
abl
e‘Hospi
tal
’asshownbel
ow:

Nowshewant
stoaddanewcolumn‘Address’
tot
heabov
egi
vent
abl
e.Suggestsui
tabl
eMy
SQL
commandf
orthesame.
Alt
ert
abl
ehospit
aladdaddr
essvar
char
(30);

4. Wri
teSQLcommandt or
emovecol
umnnamed‘
Hobbi
es’
from at
abl
enamed‘
Student
’.
Al
tert
abl
estudentdr
ophobbi
es;

5. Whil
ecreati
ngthetableStudentlastweek,Ms.Shar
mafor
gott
oincl
udet
hecol
umnGame_
Played.Now
wri
teacommand t oinserttheGame_ Play
edcolumnwit
hVARCHARdataty
peand30sizeint
othe
St
udenttabl
e?
Al
tertabl
estudentaddgame_ pl
ayedvarchar(
30);

6. Kunal
creat
edt
hef
oll
owi
ngt
abl
ewi
tht
hename‘Fr
iends’
:
Tabl
e:Fr
iends

Fr
i
endCode Name Hobbies
F101 Bi
j
oy Swi
mmi ng

5
F102 Abhi
nav Readi
ngbooks
F103 Jy
otsna Danci
ng
Now,Kunal
wantstodel
etet
he‘Hobbi
es’
col
umn.Wr
it
etheMy
SQLst
atement
Al
tert
abl
efri
endsdr
ophobbi
es;

7. Rashiwantst oaddanot hercol umn‘ Hobbies’withdat at


y peandsi zeasVARCHAR( 50)inthealr
eady
exi
sti
ngt able‘ Student ’
.Shehaswr i
tt
ent hefoll
owi ngst atement.Howev eri
thaserror
s.Rewri
tethe
corr
ectstatement .
MODI FYTABLESt udentHobbi esVARCHAR;
Alt
ertablest udentaddhobbi esv archar(50)
;
8. Ms.Shal i
nihasj ustcr eatedat ablenamed“ Empl oyee”containingcolumns
Ename,Depar tment ,Salary
.
Aft
ercreatingt het able,sher eali
zedt hatshehasf orgottent oaddapr i
mar ykeycol
umninthetabl
e.
Helpherinwr iti
ngSQLcommandt oaddapr i
mar ykeycol umnempi d.Alsostat
etheimpor
tanceof
Pri
marykeyi nat able.
Alt
ertableempl oyeeaddpr i
mar ykey (empid);

9. Whilecreati
ngat abl
e'Cust
omer'Simri
tawrongl
yaddedaprimarykeyconstr
aintt
othef
iel
d
“CUSTNAME” .Nowshewant st
or emovethepri
marykeyconst
raintf
rom t
hecustnamefi
eld.Hel
pher
i
nwr it
ingthecorrectcommand.
Alt
ertablecustomerasspr i
marykey(cust
name);

10. Mr.Akshathaveaddedanotnul l
constrai
nttothe“
name”f
ieldin“employees”tabl
e.Butnowhewant
s
t
or emovethatnotnul
lconstr
aint.Wri
tethecommandtodeletethenotnullconstr
aintf
rom name
fi
eld.
Alt
ertabl
eempl oyeemodifynamev archar
(30)nul
l;

6
I
nformat
icsPr
acti
ces
MySQLWor ksheet
-4
(
DML–INSERTINTOcommands)
1. Ramai snotabletochangeav
aluei
nacol
umnt
oNULL.Whatconst
rai
ntdi
dshespeci
fywhenshe
cr
eatedthetabl
e?
NotNull,
Pri
maryKey

2. Consi
dert
het
abl
eRESULTgi
venbel
ow.

Wr i
tecommandt oinsertanewr ow
6,"Mohan",500,
"Engli
sh",73,"
Second"
Inserti
ntoResul
tvalues(6,"Mohan",
500,
"Engl
ish"
,73,
"Second"
);

3. Consi
dert
heTabl
eSHOPPEgi
venbel
ow.

Toinsertanewr owinthetabl
eShoppe
'
110','
Pizza'
,'PapaJones'
,120,
"Kolkat
a",50.
0
I
nsertintoResultval
ues('
110'
,'
Pizza',
'PapaJones'
,120,
"Kol
kat
a",
50.
0);

4. HowisNULLval
uedif
fer
entf
rom 0(
Zer
o)val
ue?
Nul
lmeansnovaluewher
eas0isaval
ue.

5. Consi
dert
hef
oll
owi
ngt
abl
enamed"
GYM"

Addanewrowforanewit
em inGYM wi
ththedetai
ls:
"G107",
"Vi
broexer
ciser
”,21000,
“GTCFi
tness"
I
nser
tint
oResul
tval
ues(
"G107",
"Vi
broexerci
ser
”,21000,“
GTCFit
ness")
;

6. Whati
smeantbyNULLv
aluei
nMy
SQL?
Nul
lmeansnoval
ue

7. Rewr
it
ethef
oll
owi
ngSQLstat
ementaft
ercor
recti
ngerr
or(
s).Under
li
net
hecor
rect
ionsmade.
I
NSERTINSTUDENT(
RNO,MARKS)VALUE(5,
78.5);
I
NSERTINTOSTUDENT(
RNO,MARKS)VALUES( 5,
78.5)
;

8. Rewr
it
ethef
oll
owi
ngSQLstatementaf
tercor
recti
nger r
or(s)
.Under
li
net
hecor
rect
ionsmade.
I
NSERTINEMP(EMPNO,SALES)VALUE(100,20078.50);
I
NSERTINTOEMP(EMPNO,SALES)VALUES(100, 20078.50);

9. Char
vii
sinser
ti
ng“Shar
ma”inthe“LastName”col
umnoft
he“
Emp”t
abl
ebutaner
rori
sbei
ngdi
spl
ayed.
Writ
ethecorr
ectSQLstat
ement.
I
NSERTINTOEmp( ‘
Sharma’)
VALUES(LastName);
I
NSERTINTOEmp( Last
Name)VALUES( ‘
Shar
ma’);

10 Ani
tahascr
eat
edt
hef
oll
owi
ngt
abl
ewi
tht
hename‘
Order
’.
.

7
Oneoft
her
owsi
nser
tedi
sasf
oll
ows:

(
i)Whatisthedat
atypeofcol
umnsOrder
IdandOr
der
Dat
eint
het
abl
eOr
der?
(i
i)Ani
taisnowtr
yingtoi
nser
tthef
oll
owi
ngrow:

Will
shebeabletosuccessfull
yinserti
t?Givereason.
i)Thedatat
ypefororderIDfi
eldcanbeei t
hercharorvarchar
Thedatat
ypef ororderDateisdat
e
ii
)Shewi l
lnotbeabl
et oinserttheaboverecordassheisinser
ti
nganul lval
ueintheorderdat
efi
eld
andtheorder
dat efi
eldhav eanotnull
constrai
ntwhichcannotacceptnul
lvalues.

11 I
nt oday’
sdigit
izedworl
dwit
haneedt ost or
edataelectr
onical
l
y,i
tisv
eryi
mpor
tantt
ost
oret
hedat
ain
. t hedatabases.SQLisusedt
oint
eractwiththeDatabaseManagementSyst
em.
Classi
fythefoll
owingcommandsaccordingtot
heirtype:(
DDL/DML)
i.I
NSERTI NTO i
i.ALTERTABLE
i.DML ii.DDL

12 I
sNULLand0(
zer
o)same?Jusi
fyy
ouranswer
.
.
Nonulli
snotsameas0.Nullmeansnov al
ue.0isav alue.
Anynumeri
calcal
cul
ati
ononnullwil
lgi
venull
Anynumeri
calcal
cul
ati
onon0wi l
ldotheactualcal
culation.

13 Wr i
tet
hefullfor
msoft hef
oll
owi
ng:
. i.DDLii
.DML
i
.DataDefi
nit
ionLanguage
i
i
.DataManipulati
onLanguage

8
I
nfor
mat
icsPr
acti
ces
MySQLWorksheet
-5
(
DML–UPDATEandDELETEcommands)
1. Whati
sthepurposeofDROPTABLEcommandi nSOL?Howisitdif
fer
entfrom DELETEcommand?
Dr
optabl
edeletesthet
abl
ealongwi
ththest
ructur
e.Del
etedel
etestherecords.

2. I
nadat
abaset
her
ear
etwot
abl
es"
Product
"asshownbel
ow:

Wri
tethecommandToi ncreasethePr
iceofal
lthePr
oduct
sby20.
Updat
eProductsetpr
ice=price+20;

3. Wri
tetheUPDATEcommandt ochange“Shar
ma”to“Si
ngh”i
nthe“
Last
Name”col
umni
ntheEmpl
oyee
t
able.
Updat
eEmployeesetLast
name=”Si
ngh”wherel
ast
name=”Shar
ma”;

4. WhatistheuseofUPDATEstat
ementi nSQL?Howisi
tdi
ff
erentf
rom ALTERst
atement
?
Updatecommandupdatestherecords.
Al
tercommandmodi f
iest
hestructureoft
hetabl
e.

5. Consi
dert
hef
oll
owi
ngt
abl
enamed"
GYM"

Wri
tecommandTochangetheBrandnameto"Fi
tTrendIndia"oft
heit
em,
whoseI
CODEas"
G101"
.
Updat
eGym setbr
andname=”Fi
tTrendIndi
a”wher
eI code=”G101”
;

6. Wri
tetheUPDATEst
atementinMySQLtoincr
easecommi
ssi
onby100.
00i
nthe‘

Commi
ssi
on’
’col
umn
i
nthe‘Emp’t
abl
e.
Updat
eempsetcommi ssi
on=commissi
on+100.00;

7. Wri
tetwoexamplesofDMLcommandsofSQL.
I
nsert
,Updat
e,delet
e,sel
ect

8. I
nadat
abaset
her
ear
etwot
abl
es‘
CD’
and‘
TYPE’
asshownbel
ow:

9
Wri
teSQLst atementtochangethenameofSinger‘‘
SonviKumar’
’to‘

Sonv
iMehr
a’’i
nal
lthepl
aces
whereveri
toccursi
nCDt abl
e.
UpdateCDsetsinger=”
SonviMehra”wher
esi
nger=”
SonviKumar”;

9. Consi
dert
hef
oll
owi
ngt
abl
enamed“
GARMENT”
.

1)Wri
tecommandTochanget hecolourofgarmentwithcodeas116t
o“Or
ange”
.
2)Wri
tecommandt oi
ncreasethepriceofallXLgarmentsby10%
3)Wri
tecommandt odel
etetherecordwi t
hGCode“ 116”
1)UpdateGarmentsetcolour=”Orange”whereGcode=116;
2)UpdateGarmentsetprice=pri
ce+price*10/
100wher esize=”
XL”
;
3)Del
etefrom garmentwheregcode=116;
10. I
naDat
abase,t
herearetwotablesgivenbelow:

Wri
teSQLcommandtochangetheJOBI
Dto104oftheEmpl
oyeewi
thI
DasE4i
nthet
abl
e‘EMPLOYEE’
.
Updat
eemploy
eesetj
obid=104whereempl
oyeeI
D=”E4”
;

11. InMar
kscolumnof‘St
udent’t
able,
forRol
lnumber2,theClassTeacherenter
edthemarksas45.
Howevert
herewasatotali
ngerr
orandthestudenthasgothermarksincreasedby5.Whi
chMy SQL
commandshouldsheusetochangethemar ksin‘
Student
’tabl
e.
Updat
estudentsetmarks=marks-
5wherer ol
lnumber=2;

12. Chhavihascr
eat
edat abl
enamedOr
der
s,shehasbeenaskedtoincr
easet
hev
alueofacol
umnnamed
sal
esamountby20.Shehaswri
tt
ent
hef
oll
owingquer
yforthesame.

10
Al
tert
ableOrder
sAddsalesamount=sal
esamount
+20;
I
sitthecorr
ectquer
y?Just
ify.
Updateorder
ssetsalesamount=sal
esamount+20’

13. Consi
derthefol
l
owi
ngt
abl
e:
Tabl
e:PharmaDB

Wri
tecommandsi
nSQLt
oi ncr
easethepr
iceof“Amlodi
pine”by50.
Updat
ePhar
maDBsetpr
ice=pri
ce+50wheredrugname=”Amlodipi
ne”
;

11
I
nfor
mat
icsPr
acti
ces
MySQLWorksheet
-6
(
DML–SELECTcommand)
1. Pooja,
astudent
sofcl
assXI,cr
eatedatabl
e"Book"
.Pr
icei
sacolumnofthi
stabl
e.Tof
indt
hedet
ail
sof
bookswhosepri
ceshavenotbeenent
eredshewrotet
hefol
lowi
ngquer
y:
Sel
ect*fr
om BookwherePri
ce=NULL;
Sel
ect*fr
om bookwherepri
ceisnull
;

2. TheLast
Namecolumnofat abl
e"Dir
ect
ory"i
sgivenbelow:
Basedonthisinfor
mati
on,f
indtheoutputoft
hefoll
owi
ngquer
ies:
a)SELECTlastnameFROM Direct
oryWHEREl ast
nameli
ke"
_a%";

b)
SELECTl
ast
nameFROM Di
rect
oryWHEREl
ast
namenotl
i
ke"
%a"
;

A)Last
name
Batr
a
B)Last
name
Sehgal

3. Consi
dert
het
abl
eTEACHERgi
venbel
ow.Wr
it
ecommandsi
nSQLf
or(
1)t
o(3)andout
putf
or(
4)

i
.Todi spl
ayalli
nfor
mat ionaboutt eachersofPGTcat egor
y .
i
i
.Tol i
stthenamesoff emaleteacher sofHi ndidepar
tment .
i
i
i.Tolistnames,department sanddat eofhi ri
ngofalltheteachersinascendi
ngor
derofdat
eofj
oini
ng
i
v.SELECTDI STINCT(category)FROM t eacher;
i
.Select*from t
eacherwher ecategory =”
PGT” ;
i
i.sel
ectnamef rom gym wher egender =”
F”anddepar t
ment =”
Hindi”;
i
ii
.Selectname,department,hir
edat efrom teacherorderbyhi r
edate;
i
v.DISTINCT(Category)
TGT
PRT
PGT

4. Thel
tem_
NoandCostcol
umnaofatable"I
TEMS"aregivenbel
ow:
Basedont hi
sinf
ormat
ion,f
indtheout
putoft
hefol
l
owi
ngquer
ies:
a)SELECTCOST+100FROM I TEMSWHEREITEM_NO>103;
Ans.COST+100
6100
NULL

5. Consi
dert
het
abl
ePr
oject
sgi
venbel
ow.Wr
it
ecommandsi
nSOLf
ori
)toi
i
i)andout
putf
ori
v)

12
i
.Todi splayal linformat i
onaboutpr oj ectsof "
Medi um"Pr oj
Size
i
i.Tol i
stt hePr ojSizeofpr ojectswhosePr ojNameendswi thLITL.
i
ii
.Tol istID, Name, Size,andCostofal l t
hepr ojectsindescendingorderofStar
tDate.
i
v.SELECTDI STINCTPr ojSizeFROM pr oj
ect s
i
.Sel ect*f rom pr ojectswher epr oj
size=” Medi um” ;
i
i.Selectpr ojsi
zef rom project swher epr ojnamel i
ke“ %LITL”;
i
ii.SelectI D,proj
Name, projSize,costfr om pr oj
ectsor derbystartDat
edesc;
i
v .ProjSize
Medi um
Large
Smal l
6. TheMnameCol umnofat ableMember sisgi v
enbel ow:
Basedont heinformation,fi
ndtheoutputofthefoll
owingquer
ies:
(i
)SelectMnamef r
om member swheremnamel i
ke"%v ";
(i
i
)Sel ectMnamef rom member swher emnamel ike"%e%";

Ans.i)Mname
Hir
av
Rajeev
i
i)Mname
Sheetal
Rajeev

7. Sarthya,
astudentofclassXI,
createdatabl
e"RESULT".Gradeisoneofthecolumnofthistabl
e.Tof
ind
thedetail
sofstudentswhoseGradeshav enotbeenenter
ed,hewrotet
hef ol
lowingMySqlquery,
whi
ch
didnotgivethedesir
edresul
t.
SELECT*FROM Resul tWHEREGr ade="Nul
l"
;
HelpSarthyatorunthequerybyremov i
ngtheerror
sfr
om thequeryandwritethecor
rectQuery.
Select*from Resul
twhereGradeisnull;

8. Consi
dert
het
abl
eRESULTgi
venbel
ow.Wr
it
ecommandsi
nMy
Sql
for(
i)t
o(i
i
)

(i
)Tolistthenamesoft hosest udent
s,who
haveobt ai
nedDi visi
onasFI RSTinthe
ascendingor derofNAME.
(i
i)Todi splayar eportli
stingNAME,SUBJECT
andAnnualst i
pendr ecei
v edassumingthatt
he
sti
pendcol umnhasmont hlyst
ipend.

i
) Sel
ectnamefr
om Resultwheredi
visi
on=”
Fir
st”or
derbyname;
i
i) Sel
ectname,
subj
ect
,st
ipend*12fr
om Result
;

9. Mr.Janakisusingat ablewit
hf oll
owingcolumns:
Name, Class,Cour se_Id,Cour
se_ name
Heneedst odisplaynamesofst udents,
whohav enotbeenassi
gnedanystream orhavebeenassi
gned
Course_namet hatendswi th"economi cs".Hewrotethefol
lowingcommand,whi chdidnotgivethe
desi
redresult
.
SELECTName, ClassFROM St udent sWHERECour sename=NULLORCour sename=" %economi
cs";
HelpMr.Janakt or unt hequerybyr emov i
ngtheer
rorandwri
tethecor
rectquer
y.
SELECT Name,Cl ass FROM St udents WHERE Course name IS NULL OR Cour se name LIKE
"%economics";
13
10 Consi
dert
heTabl
eSHOPPEgi
venbel
ow.Wr
it
ecommandi nMy Sqlfor(
i)to(ii
)
.
(i
)Todisplaynamesoft hei t
emswhosenamest ar
ts
with'
C'inascendingorderofPr i
ce.
(i
i
)To di splay Code,I t
em name and Ci t
y ofthe
product
swhosequant ityislessthan100.

i
) Sel
ectIt
em f
rom shoppewhereit
em l
ike“
C%”orderbypr
ice;
i
i) Sel
ectcode,
i
tem,ci
tyfr
om shoppewhereqty
<100;

11 Whati
susedi
ntheSELECTcl
auset
oret
urnal
lthecol
umnsi
nthet
abl
e?
.
*(
ast
eri
sk)si
gn

12 I
nMySQL,SumitandFauzi
aar
egett
ingt
hefol
lowi
ngoutputsofI
temCodesf
orSELECTstat
ement
sused
. bythem onatabl
enamedITEM.(
Bot
hhaveusedtheSELECTstat
ementsonthesametabl
eITEM)
.
Sumit
’sOutput Fauzia’
sOutput
101 101
102 102
101 105
105 107
101
107

Whichextr
akey
wor
dhasFauzi
ausedwi
thSELECTst
atementt
ogett
heabov
eout
put
?
Di
stinct

13 Consi
derthetabl e‘PERSONS’ givenbel ow.Wr i
tecommandsi nSQLf or(i
)t o(i
v)andwr it
eoutputfor(v).
.
(i
)Di splaytheSur Names,Fi r
stNamesand
Cities ofpeopl er esidi
ng in Udhamwar a
city.
(i
i)Di splaythePer sonI ds( PI
D),cit
iesand
Pincodesofper sonsi ndescendingor der
ofPi ncodes.
(i
ii)DisplaytheFirstNamesandci t
iesofall
thef emal esget tingBasi csalari
esabov e
40000.
(i
v )DisplayFirstNamesandBasi cSalaries
ofal ltheper sonswhosef i
rst
namesst arts
with“ G”.
(
v) SELECTSur nameFROM Per sonsWher eBasicSalary>=50000;
i
) Selectsur name, f i
rstname, ci
tyfr om personswher eci ty=”Udhamwar a”;
i
i) Selectpid,city,pincodef r
om per sonsor derbypincodedesc;
i
ii
) Selectf i
rstname, ci
tyf rom personswher egender=” F”andbasi cSalar y>40000;4
i
v) Selectf i
rstname, basicSalaryfrom per sonswheref i
rstnamel ike“G%” ;
v) Sur name
Shar ma
Si
ngh
Alvi
s
14 Mr.Tondonisusi ngt ableEMPwi ththefollowingcolumns.
. ECODE, DEPT,ENAME, SALARY
Hewant stodi splayal linformat ionofempl oy
ees( fr
om EMPt able)inascendi ngor derofENAMEand

14
wit
hiniti
nascendi
ngor
derofDEPT.Hewr
otethef
oll
owi
ngcommand,whi
chdi
dnotshowt
hedesi
red
output
.
SELECT*FROM EMPORDERBYNAMEDESC, DEPT;
Rewr
it
etheabov
equer
ytogett
hedesi
redout
put
.
SELECT*FROM EMPORDERBYENAME,
DEPT;

15 Consi
dert
hef
oll
owingtabl
enamed"
GYM"wi
thdet
ail
saboutf
it
nessi
temsbei
ngsol
dint
hest
ore.Wr
it
e
. commandofSQLf or(
i)to(
ii
).

(i
)Todi splaythenamesofalltheit
emswhosenamestar
ts
with"A"
.
(i
i
)To di spl
ay ICODEs and I
NAMEs ofal
litems,whose
Brandnamei sReli
abl
eorCoscore.

i
) Sel
ecti
namefrom gy
m whereinameli
ke“ A%”
;
i
i) Sel
ectI
Code,
I
namef rom gy
m wherebrandnamei
n(“
Rel
iabl
e”,
”Coscor
e”)
;

16 Considert
hef ol
l
owingtabl
enamed'
SBOP"wi
thdet
ail
sofaccounthol
der
s.Wr
it
ecommandsofMy
Sql
. for(
i)to(i
i
) andout
putf
o r(
ii
i)
.

(i
)To di spl
ayAccount no,NameandDat eOf
openofaccount
holdershavi
ngt r
ansacti
onsmorethan8.
(i
i) To display allinf
ormati
on of account hol
der
s whose
tr
ansactionvalueisnotmenti
oned.
(i
ii
)SELECTNAME, BALANCEFROM SBOPWHERENAMELI KE
“%i”;

i
) Sel
ectAccountNo,
Name,
Dateof
openfr
om sbopwher
etr
ansact
ion>8;
i
i) Sel
ect*fr
om sbopwheret
ransact
ioni
snul
l;
i
ii
) Name Bal
ance
Mrs.Sakshi 45000. 00

17 Whenusingt heLIKEclause,whi
chwil
dcar
dsymbolrepresent
sanysequenceofnone,oneormor
e
. char
acter
s ?
%
18 Consi
derthetabl
eFLIGHTgi v
enbel
ow.Wri
tecommandsinSQLf or(
i)t
o(i
v)andout
putf
or(v)
.
.

(
i)Displaydetail
sofal lf
li
ghtsstart
ingf r
om Del hi.
(
ii
)Displaydetail
soff li
ghtsthathav emor ethan4numberoff l
ightsoperat
ing.
(
ii
i)Displayfli
ghtcodes,st ar
ti
ngpl ace,destination,numberoff li
ghtsindescendingorderofnumberof
f
li
ghts.
(
iv)Displaydesti
nationsalongwi t
hf li
ghtcodesofal lthedestinati
onsstart
ingwi t
h‘A’.
(
v)SELECTDI STINCT( NO_STOPS)FROM FLI GHT;
i
) Sel
ect*f rom fl
ightwher estart
=”Del hi”
;
i
i) Sel
ect*f rom fl
ightwher eno_fli
ghts>4;
i
ii) Sel
ectflcode,st
art,
desti
nat i
on,no_ f
li
ght sfrom fl
ightor derbyno_fl
ightsdesc;
i
v ) Sel
ectdest i
nati
on,fl
codef rom fl
ightwher edesti
nat ionlike“
A%” ;
v) NO_ STOPS
0
1
2
15
3

19 Whatwi
l
lbet
heout
putoft
hef
oll
owingqueriesonthebasi
sofEmploy
eetabl
e:
.
(i
)SelectSalar
y+100fr
om Empl
oyeewhereEmpI
d='
A002'
;

i
) Sal
ary+100
NULL

20 Pr
anay
,whoi
sanI
ndi
an,
creat
edat
abl
enamed“
Fri
ends”t
ost
orehi
sfri
end’sdet
ail
.
. Table“Fri
ends”isshownbel
ow.
Writ
ecommandsi nSQLf
or(i
)to
(i
i
i)andoutputf
or(iv
).

i
.Todispl
ayl
i
stofal
lfor
eigner
f
ri
ends.
i
i
.Tol i
stname, cit
yandcount ryindescendingor derofage.
i
i
i.Toli
stnameandci tyofthosef ri
endswhodon’ thaveanemai li
d.
i
v.Sel
ectname, countr
yfrom fr i
endswhereage>12andnamel ike‘
A%’;
i
. Selct*from f
riendswher ecountr ynotin(“I
ndi
a”);
i
i. Selectname,city,
countryfrom f
riendsor derbyagedesc;
i
ii
. Selectname,cityfrom fr
iendswher eemai l_
idisnull
;
i
v. Name Count ry
Ali
ce USA
Angel USA
AlexenderAust r
alia

21 Considert
hef
oll
owingtabl
enamed“
GARMENT”
.Wr
it
ecommandofSQLf
or(
i)
. t o(
iv)andout
putfor(
v)to(vi
i
).
(i
)Todispl
aynamesoft hosegar
ment
sthat
areav
ail
abl
ein‘
XL’
size.
(i
i)Todisplaycodesandnamesoft hosegarments
thathavetheirnamesst art
ingwith‘
Ladies’
.
(i
ii
)Todisplaygar mentnames, codesandpr i
cesof
thosegarment sthathave
pri
ce int he r ange 1000. 00 to 1500.00 (bot
h
1000.00and1500. 00included).

(
iv)SELECTGNAMEFROM GARMENTWHERE
SI
ZEI
N(‘
M’,
‘L’
)ANDPRI
CE>1500;
i
) Selectgnamef r
om garmentwheresi
ze=”XL”
;
i
i) Selectgcode,
gnamef r
om garmentwheregnameli
ke“Ladi
es%”
;
i
ii
) Selectgname,gcode,
pri
cewherepri
cebetween1000.
00and1500.
00
i
v) Gname
Jeans

22 Consi
dert
het
abl
e‘empsal
ary
’.
.
Toselecttupl
eswithsomesalary,Si
ddhar
thhaswr
it
ten t
hefol
lowi
nger
roneous
SQL
st
atement:
SELECTID,Sal
aryFROM empsalar
yWHERESal ar
y=somethi
ng;

16
SELECTID,SalaryFROM empsal
aryWHERESalar
yisnotnul
l;
23 Considert
hetable‘Empl
oyee’
.
. Writ
etheSQLcommandtoobtai
nthef
oll
owi
ngout
put:

Sel
ectdi
sti
nctLocat
ionf
rom empl
oyee;

24 Table“
Emp”i sshownbel ow.WritecommandsinSQLf or(i
)to( i
i
i)andoutputfor( i
v)and(v)
. and( vi
)
i.Todisplayli
stofallemployeesbelow25y earsold.
ii
. To l i
st names and r espect
ive sal
ari
es i n
descendingor derofsalar
y.
iii
.Tol i
stnamesandaddr essesoft hosepersons
whohav e‘ Delhi
’i
ntheiraddress.
iv.SELECTName,Sal aryFROM Empwher esalary
between50000and70000;
v.SELECTName, phonefrom empwher ephonelike
‘99%’;
i
. Sel
ect*from empwher eage<25;
ii
. Sel
ectname, sal
aryfrom emporderbysal
arydesc;
i
ii
. Sel
ectname, addresswhereaddressl
ike“
%Del hi
%”;
i
v. Name salary
Si
ddharth 62000
Karan 65000
v. Name Phone
Chavi 99113423989
Raunaq 99101393576

25 Mrs.Senent
eredthefoll
owingSQLstat
ementt
odi
splayallSal
espersonsoftheci
ti
es“Chennai

. and‘Mumbai
’fr
om thetabl
e‘Sal
es’
.
Scode Name Ci
ty SELECT * FROM Sal es WHERE
101 Aakri
ti Mumbai Ci
t y
= ‘
Chennai
’ANDC i
ty=‘
Mumbai’
;

102 Aman Chennai


103 Bani
t Del
hi
104 Fauzi
a Mumbai

Rewri
tethecor
rec
tst
atement
,ifwr
ongorwr
it
estatementiscor
rec
t.
SELECT*FROM SalesWHERECit
y=‘
Chennai
’ORCity=‘Mumbai’
;

26 WritecommandsinSQLfor(i
)to(ii
i
)andoutputfor(
iv)
.
. Tabl
e :Stor
e
StoreI
d Na me Locati
on Ci t
y NoOfEmploy
ees Dat
eOpened Sal
esAmou nt
S101 Pl anet
fashi
on Karol
Bagh Delhi 7 2015-
10-
16 300000
S102 Tr ends Nehru Mumbai 11 2015-
08-
09 400000
Nagar
S103 Vogue Vi
kas Delhi 10 2015-
06-
27 200000
Vi
har

17
S104 Supe r
fashi
on Def ence Delhi 8 2015-02-18 450000
Colony
S105 Rage Bandr a Mumbai 5 2015-09-22 600000
(i
)Todi splayname, l
ocati
on, cit
y,SalesAmountofst oresindescendingor derofSalesAmount.
(i
i)Todi splaynamesofst oresal ongwi thSalesAmountoft hosest oresthathave‘fashi
on’anywher
ein
thei
rstorenames.
(i
ii
)Todi splaySt or
esnames,Locat i
onandDat eOpenedofst orest hatwereopenedbef ore1stMarch,
2015.
(i
v)SELECTdi stinctci
tyFROM st ore;
i) Selectname,l
ocat ion,
cit
y,salesamountfrom Storeor derbysalesamountdesc;
ii
) Selectname,salesamountf r
om storewher enamel ike“%Fashi on%”;
ii
i) Selectname,l
ocat ion,
dateOpenedf r
om storewheredat eOpened<” 2015-03-
01”;
iv
) CITY
Delhi
Mumbai

27 WhichclausewouldyouusewithSel
ectt
oachi evethefoll
owing:
. i.Toselecttheval
uesthatmat
chwithanyvalueinal i
stofspecif
iedv
alues.
i
i.
Usedt odispl
ayunr
epeatedval
uesofacolumnf rom atable.
i
.IN
i
i.DISTI
NCT

28 Consi
derthefol
l
owi
ngt
abl
e:
. Table:PharmaDB
Wr i
tecommandsi nSQLf or( i)to(ii
i)andoutput
for(i
v):
i
.Toi ncreaset hepr i
ceof“Aml odipi
ne”by50.
i
i.Todi splayallt hosemedi cineswhosepr iceisin
therange100t o150.
i
ii.To di splay t he Dr ug I D,Dr ugName and
PharmacyNameofal lt
her ecor dsindescending
orderoft heirprice.
i
v .SELECTRxI D, DrugName, Pr icefr
om
PharmaDBwherePhar macyNameIN(“RxPar macy”
,“Raj Medicos” );
i
. Updat ePharmaDBsetpri
ce=price+50wheredr ugname=” Aml odipine”;
ii
. Sel ect*from pharamdbwherepricebetween100and150;
i
ii
. Sel ectDrugI
D,Drugname,
pharmacy Namef r
om phar maDBor derbypr i
cedesc;
i
v. RXI D DrugName pri
ce
R1000 Aml odipine 100.00
R1001 Par acetamol 15.00
R1004 Lev ocit
rezi
ne 110.00

29 Writ
eSQLst
atementt
hatgi
vest
hesameout
putasthefoll
owi
ngSQLst
atementbutuses‘
I
N’key
wor
d.
. SELECTNAMEFROM STUDENTWHERESTATE=‘ VA’;
SELECTNAMEFROM STUDENTWHERESTATEI N(‘
VA’);

30 Whichoneoft hef ol
l
owi ngSQLquerieswil
ldisplayallEmployeerecordscontai
ningthewor
d“Ami
t”
,
. regardlessofcase(whetheritwasst
oredasAMI T,Amit,oramitet
c.)?
(
i)SELECT*f r
om Empl oyeesWHEREEmpNamel ikeUPPER‘ %AMIT%’ ;
(
ii
)SELECT* fr
om Empl oyeesWHEREEmpNamel ike‘%AMIT%’ or‘
%AMI T%’OR‘%ami t
%’;
(
ii
i)SELECT*f r
om Empl oyeesWHEREUPPER( EmpName)l i
ke‘%AMI T%’;
(
ii
i)SELECT*f rom EmployeesWHEREUPPER( EmpName)l i
ke‘%AMI T%’;

31 Wr
it
eAnswert
o(i
).Wr
it
eSQLquer
iesf
or(
ii
)to(
vii
).
.

18
Not e:ColumnsSI DandDOBcont ainSal esPer sonI dandDat aofBi r
threspecti
vel
y.
(i
)Wr i
tethedat atypesofSI DandDOBcol umns.
(i
i)DisplaynamesofSal espersonsandt heirSal ari
eswhohav esalariesinther ange30000. 00to
40000.00
(i
ii
)Tol i
stNames,Phonenumber sandDOB( DateofBi r
th)ofSalespersonswhower ebor nbefor
e1st
Nov ember,1992.
(i
v)Todi splayNamesandSal ari
esofSal esper sonsi ndescendingorderofsalary.
(v)Todisplayareasinwhi chSal esper sonsar ewor king.Dupli
cateAreasshouldnotbedi splayed.
(vi
)Todi splaySID,Namesal ongwi thSalari
esi ncreasedby500.( I
ncreaseof500i sonlytobedi splay
ed
andnott obeupdat edint het able)
(vi
i)TodisplayNamesofSal esper sonswhohav et hewor d‘Kumar’anywher ei
ntheirnames.
i
. Thedat atypeofSI Di sei t
hercharorv ar char
ii
. Sel ectname,salaryf r
om sal espersonwher esalar
ybetween30000. 00and40000. 00;
iii
. Sel ectname,phone, dobf rom sal espersonwher edob<”1992-11-01”;
iv. Sel ectname,salaryf r
om sal espersonor derbysal arydesc;
v. Sel ectdist
inctareaf rom sal esperson;
v i
. Sel ectsid,
name,sal ary+500f r
om sal esper son;
vii
. Sel ectnamef rom sal esper sonwher enamel i
ke“%Kumar %”;

32 Writethefoll
owi ngstatementusing‘OR’l
ogical
operator:
. SELECTf i
rst_name, last_name,
subjectFROM student
detai
lsWHEREsubj
ectI
N(‘
Maths’,‘
Sci
ence’
);
Select f i
rst_name, l ast_
name, subj ect from st udent
Detai
ls Where subject=”
Maths” or
subject=”
Science” ;

33 Consi
dert
heTabl
e“Gy
m”shownbel
ow.Wr
it
ecommandsi
nSQLf
or(
i)t
o(v
i):
.

(i
)TodisplayMname, Age,
FeeGivenofthosemember swhosef eei sabove12,
000.
(i
i
)TodisplayMcode, Mname,Ageofall f
emalemember softheGy m wi
thageindescendi ngorder
.
(i
i
i)Tolistnamesofmember sandt heirdateofadmissionoft hosemember swhoj oinedaf t
er31st
December, 2015.
i
v)Todisplayt heMname,FeeGi
v enofallt
hosemember softheGy m whoseageislesst han40andar e
monthl
yt y
pemember soft
heGy m.
(v
)Todisplaynamesofmember swhohav e‘mit
’anywhereintheirnames.Forexample: Ami t
,Samit.
(v
i)Todisplaytypesofmembershipsavail
abl
e.Dupli
cat
ev al
uesshoul dnotbedispl
ayed.
i
) Selectmname,age,feegi
venf r
om gym wherefeegiven>12000;
19
i
i) Sel
ectmcode,
mname, agefrom gym wheregender=”Female”orderbyagedesc;
i
ii
) Sel
ectmname,dtAdmi tfr
om gy m wher
edtAdmit>”2015-12-31”
;
i
v) Sel
ectmname,feegi
venf rom gym whereage<40andt ype=”Monthly
”;
v
) Sel
ectmnamef rom gym wher emnamel i
ke“%mi t
%”;
v
i) Sel
ectdi
sti
ncttypefrom gym;

34 Consi
dert
hef
oll
owi
ngt
abl
e:
. Wr i
tecommandsi nSQLf or( i)to(i
v)andout putfor
(v):
i. Todi splaythedet ailsofal lthosestudent swho
haveI Past heiropt i
onal subj ect.
ii
. Todi splayname, stream andopt i
onal ofall
thosest udentswhosenamest art
swi t
h‘A’.
ii
i. Togi veani ncreaseof3i ntheav er
ageofal l
thosest udentsofhumani ti
essect i
onwhohav e
Mat hsast heiropt i
onal subj ect.
iv. Todi splayanamel istofal lt
hosestudent s
whohav eav eragemor ethan75.
v
. Selectnamef rom studentswher
eopt ional I
N( ‘
CS’,’
IP’
);
i
. Select*f r
om studentwhereopt i
onal=”IP”;
i
i. Selectname, st
ream,opti
onalfr
om st udentwher enamel ike“A%” ;
i
ii
. Updat estudentsetaverage=average+3wher estream=” Humani ties” ;
i
v. Selectnamef r
om studentwhereav erage>75;
v
. Name
Shrishti
Aditya
RituRaj
Saumy a
Ashut osh
Aman

20
Infor
mat
icsPr
acti
ces
MySQLWor ksheet
-7
(
Singl
eRowFuncti
ons)
1. Wr i
tetheoutputofthefoll
owi ngSQLquer i
es:
a)SELECTROUND( 6.5675,2); 6.
57
b)SELECTTRUNCATE( 5.
3456, 1); 5.
3
c)SELECTDAYOFMONTH( '
2009- 08-
25'
); 25
d)SELECTMI D(
'Cl
ass12',2,3); l
as
2. Wr i
tetheoutputofthefoll
owi ngSQLquer i
es:
(
i) SELECTI NSTR(‘UNI CODE’,

CO’); 4
(
ii
) SELECTRI GHT( ‘
Informati
cs’,
3); i
cs
3. Statedif
fer
encebetweendat efuncti
onsNOW()andSYSDATE()ofMy
Sql
.

Now(
) Sy
sdat
e()

Nowdispl
aysthedat
eandt
imeatt
he Sysdat
e()di
spl
aysthedateandti
meatthe
begi
nni
ngofthecommand. exactt
imeoftheexecut
ionofthecommand.

Ital
way sdi
spl
aysthesamedat
eand I
tdi
splay
stheexactdat
eandt
imeatwhi
chi
t
timeinasingl
esqlcommand.No i
sexecut
edwit
hinthesi
ngl
ecommand.
matterhowmanyt i
mesiti
sbeing
executed.

Exampl
e:

4. Nameaf
unct
ionofMy
Sql
whi
chi
susedt
oremov
etr
ail
i
ngandl
eadi
ngspacesf
rom ast
ri
ng.

t
ri
m

5. Consi
dert
hef
oll
owi
ngt
abl
enamed'
SBOP"wi
thdet
ail
sofaccounthol
der
s.Wr
it
eout
put

(i
) SELECT ROUND(
Bal
ance,
-
3) FROM SBOP WHERE
Account
No=”
SB-
5”;

Ans.i
)
ROUND(Bal
ance,
-3)
63000

6. Writ
etheoutputofthefoll
owingSQLqueries:
(
i)SELECTRIGHT(‘sof
tware’,2); r
e
(
ii
)SELECTINSTR( ‘
twel
v e’
,
‘l
v’); 4
(
ii
i)SELECTDAYOFMONTH( ‘
2014-03-
01’
); 1
(
iv)SELECTROUND( 76.987,2); 76.
99
7. Writetheoutputofthef oll
owi ngSQLqueries:
i
.SELECTI
NSTR(‘
I
NTERNATIONAL’
,‘
NA’)
; 6
i
i
.SELECTLENGTH(CONCAT(‘
NETWORK’
,’
ING’
))
; 10
21
i
i
i.
SELECTROUND(
563.
345,
-
2); 600
i
v.SELECTDAYOFYEAR(‘
2014-01-30’
); 30
8. Pranay,whoisanIndi
an,createdat ablenamed“ Fr
iends”t
ost
orehi
sfr
iend’
sdet
ail
.
Tabl
e“Fri
ends”i
sshownbel
ow.Wr i
teoutputf
or(
i)and(i
i)
.

i
.Sel
ectucase(
concat
(name,
”*
”,
ci
ty)
)fr
om f
ri
endswher
ecount
ryl
i
ke‘
Denmar
k’;
i
i.Sel
ectmi
d(name,1,
4)as“UI
D”fr
om f
ri
endswher
ecount
ryl
i
ke‘
USA’
;
i. ucase(concat(
name,”
*”,
cit
y))
CHARLES*COPENHAGEN
JETTE*NYKOBING
ii
. UI
D
Ali
c
Ange

9. Wr i
tetheoutputofthef oll
owi ngSQLquer ies:
i
)SELECTTRUNCATE( 8.975, 2)
; 8.
97
i
i)SELECTMI D(
‘HONESTYWI NS’,
3,
4); NEST
i
ii
)SELECTRI GHT(CONCAT( ‘PRACTI CES’,

INFORMATICS’
),
5); ATI
CS
i
v)SELECTDAYOFMONTH( ‘2015-01-16’
); 16
10. Wr i
tetheoutputofthef oll
owi ngSQLquer ies:
(
i)SELECTMI D(‘
BoardExami nati
on’,
2,4)
; oar
d
(
ii)SELECTROUND( 67.246,2) ; 67.
25
(
iii
)SELECTI NSTR(‘I
NFORMATI ONFORM’ ,‘
FOR’); 3
(
iv)SELECTDAYOFYEAR( ‘
2015- 01-
10’); 10
11. Wr i
teoutput
.
Tabl
e:St
ore
Stor
eIdNa me Locat i
on Cit
y NoOf
Employ
eesDat
eOpenedSal
esAmou n
S101 Pl
ane t
fashi
o Kar olBaghDel
hi 7 2015-
10-
16 t 300000
S102 T
nrends Nehru Mumbai 1 2015-
08-
09 400000
Nagar 1
S103 Vogue Vi
kas Del
hi 1 2015-
06-
27 200000
Vi
har 0
S104 Superf
ashion Defence Delhi 8 2015-
02-
18 450000
Colony
S105 Rage Bandra Mumbai 5 2015-
09-
22 600000
(
i)SELECTName,l
engt
h(name)
,lef
t(name,
3)FROM St
orewher
eNoOf
Empl
oyees<3;
i
) EmptySet

12. Writ
et heoutputofthefol
l
owingSQLquer ies:
SELECTPOW( INSTR(‘
My_Database’
,
’_’
),
2); 9
13. Considerthetablegi
venbelow:
Writ
eout put
.

(
i)SELECTName,
LENGTH(
Name)FROM Sal
esper
son;

Ans.
Name Lengt
h()
AmitKumar 10
Deepi
kaShar
ma 14
Vi
naySri
vast
av 15
KumarMehta 11
RashmiKumar 12
14. I
dent
if
ySi
ngl
eRowf
unct
ionsofMy
SQLamongstt
hef
oll
owing:
TRI
M()
,MAX(
),COUNT(
*),
ROUND(
)
Tr
im(
)andRound(
)ar
esi
ngl
erowf
unct
ions

22
15. Consi
dert
heTabl
e“Gy
m”andwr
it
eout
put

(
i)SELECTMI
D(Mname,
1,
2)f
rom Gy
m;

Ans.
MID(
Mname,
1,
2)
Am
Ra
Ge
Fa
Sa
La
Su
Mi
Da
Aj
16. Observethegivent ablenamed“ Loan”car ef
ull
yand
predi
cttheoutputoft he
fol
lowi
ngquer i
es:
i
. SELECT concat (l
ef t
(fi
l
e_no,
2),
ri
ght
(cust _name, 2)
)AS“ ID”f r
om loan
whereBank=' I
CUCILt d.
';
i
i. selectround(loan_ amt-
l
oan_amt *10/ 100)As" Discounted
Payment "from loanwher e
l
oan_amt>700000;
i
) I
D
23mi
43et
i
i) Di
scountedPayment
728888
671164

I
nfor
mat i
csPr
acti
ces
MySQLWor ksheet-
8
(
Aggregat
eFuncti
ons)
1. Consi
dert
het
abl
eTEACHERgi
venbel
ow.Wr
it
ecommandsi
nSQLf
or(
1)andout
putf
or(
2)t
o(5)

i
.Tocountt henumberoft eachersinEngli
shdepart
ment.
i
i
.SELECTMAX( Hiredate)FROM Teacher;
i
i
i.SELECTDI STINCT(category)FROM teacher;
i
v.SELECTCOUNT( *)FROM TEACHERWHERECat egor
y=" PGT"
v
.SELECTGender ,
AVG(Salary)FROM TEACHERgr oupbyGender;
i
.Selectcount (
*)from teacherwheredepar t
ment=”
Engl
ish”;
i
i.Max(Hiredate)
1994-09-02
i
ii
.Di sti
nct(Category)
TGT
PRT
PGT
i
v Count (
*)
1
v
. Gender avg(salary
)
M24000
F 24500

23
2. Thel
tem_
NoandCostcol
umnofat
abl
e"I
TEMS"ar
egi
venbel
ow:
Basedonthi
sinf
ormati
on,f
indtheout
putoft
hef
oll
owi
ngquer
ies:
a)SELECTAVG(COST)FROM ITEMS;
b)SELECTCOST+100FROM I TEMSWHEREITEM_NO>103;

A) AVG(Cost)
5000
B) Cost+100
6100
Null

3. "Pri
ncipaiName"isacolumninatable"School
s".TheSOLqueri
es
SELECTcount (*)FROM School
s;
and
SELECTcount (Princi
pal
)FROM schools;
Givether esul
t28and27r espect
ivel
y.Whatmaybet hepossi
blereasonfort
his?Howmanyr
ecor
dsar
epr
esenti
n
thetable-27or28?
Thepossi bler
easoncouldbethatoneofthevalueinPr
inci
pal
fiel
dwillbeNULL
Therear e28records

4. Consi
dert
het
abl
ePr
oject
sgi
venbel
ow.Wr
it
ecommandsi
nSOLf
ori
)andout
putf
ori
)toi
i
i)

i
.Tocountt henumberofpr oj
ectsofcostlessthan100000.
i
i
.SELECTSUM( Cost)FROM pr oj
ects;
i
i
i.SELECTPr ojSize,COUNT( *
)FROM Pr oj
ectsGROUPBYPr ojSi
ze;
i
.Selectcount (*)from pr
ojectswher ecost
<100000;
i
i. Sum( cost )
980000
i
ii
. Projesize count(
*)
Medi um 3
Large 2
Smal l 1

5. Consi
dert
het
abl
eRESULTgi
venbel
ow.Wr
it
eout
put

(i
)SELECTAVG(Sti
pend)FROM EXAM WHEREDI VI
SION=
"
THIRD”;
(
ii
)SELECTCOUNT(DISTINCTSubject
)FROM EXAM;
(
ii
i)SELECTMIN(Aver
age)FROM EXAM WHERESubj ect
=
"
Engli
sh";

i
) AVG(Sti
pend)
475
i
i) Count(
dist
inctsubj
ect
)
6
i
ii
) Min(
Average)
38

24
6. Whati
sthepur
poseofORDERBYcl
ausei
nMy
Sql
?Howi
sitdi
ff
erentf
rom GROUPBYcl
ause?

Orderbydisplay
sther
ecor
dsi
nascending/
descendingorderoffi
eld.
GroupBy,groupsther
ecor
dsaccor
dingtoafiel
dandt henfindsthemaxi
mum orminimum orcount
sorf
indsthe
sum oraverage.
7. ConsidertheTableSHOPPEgivenbel
ow.Wri
tecommandinMy Sql
for(
i)andoutputfor(i
i
)to(
ii
i)
.

(i
)Tocountdisti
nctCompanyfrom thetabl
e.
(
ii
)SelectCount
(di
sti
nct(
City
))from Shoppe;
(
ii
i)Sel
ectMIN(Qty)fr
om Shoppewher eCit
y="Mumbai
";

i
) Sel
ectcount(di
sti
nctcompany
)fr
om shoppe;
i
i
) Count(
dist
inct(
cit
y))
3
i
i
i) Min(
Qty)
56

8. Consi
dert
het
abl
e‘PERSONS’
giv
enbel
ow.Wr
it
ecommandsi
nSQLf
or(
i)t
o(i
v)andwr
it
eout
putf
or(
i)t
o(i
i
i).

(i
) SELECTSUM(Basi
cSalary)FROM PersonsWhere
Gender
=’F’
;
(i
i
)SELECTGender,MIN(BasicSal
ary
)FROM Persons
GROUPBYgender;
(i
i
i)SELECTGender
,Count(*
)FROM Per sonsGROUP
BYGender;

i
) SUM(Basi
cSalary
)
132000
i
i) GenderMIN(BasicSal
ary
)
F 40000
M 33000
i
ii
) GenderCount(
*)
F 3
M 4

9. Therei
sacolumnHOBBYi naTabl eCONTACTS.Thef
oll
owi
ngt wostat
ementsar
egi
vi
ngdi
ff
erentout
put
s.What
maybethepossi
blereason?
SELECTCOUNT(*)FROM CONTACTS;
SELECTCOUNT(HOBBY) FROM CONTACTS;
Thepossi
bler
easoncoul dbethatsomeoftheval
uesi
nthehobbyfi
eldwoul
dbeNULL.

10. Consi
derthefoll
owi
ngtabl
enamed"GYM"wi
thdet
ail
saboutf
it
ness
i
temsbeingsoldint
hest
ore.Wr
it
eout
put

(i
)SELECTCOUNT(DISTI
NCT(BRANDNAME)
)FROM GYM;
(
ii
)SELECTMAX(PRICE)FROM GYM;

Ans.i)COUNT( DISTI
NCT(BRANDNAME))
6
i
i) Max(Pri
ce)
30000
11. Considert
hefoll
owingtabl
enamed'SBOP"wi
thdet
ail
sofaccount
hol
ders.Wri
teout
put.

(
i)SELECTCOUNT(
*)FROM SBOP;

25
Ans. COUNT(*
)
5
12. Gi
ven‘
Empl
oyee’
tabl
easf
oll
ows:

Whatval
ueswi
ll
thef ol
l
owingstatement
sret
urn?
SELECTCOUNT(*)FROM Employee;
SELECTCOUNT(Commi ssi
on)FROM Employ
ee;

Ans.Count(
*)
3
Count
(Commissi
on)
1
13. Consi
dert
hetabl
eFLI
GHTgivenbel
ow.Wr
it
eout
put
.

(i
)SELECTMAX(
NO_FLI
GHTS)FROM FLI
GHT;
(i
i
)SELECTSTART,COUNT(
*)FROM FLIGHTGROUPBY
St
art
;

i
) MAX(NO_FLI
GHTS)
7
i
i) START COUNT(
*)
Del
hi 3
Mumbai 2
Kanpur 1
I
ndore 1

14. Whatwi
l
lbet
heout
putoft
hef
oll
owi
ngquer
iesont
hebasi
sofEmpl
oyeet
abl
e:

(
i)
Sel
ectav
g(Sal
ary
)fr
om Empl
oyee;
(
ii
)Sel
ectSal
ary
+100f
rom Empl
oyeewher
eEmpI
d='
A002'
;

i
) Avg(
Salary
)
5300
i
i) Sal
ary+100
NULL

15. Consi
dert
hef
oll
owi
ngt
abl
enamed“
GARMENT”
.Wr
it
eout
put

(
i)SELECTCOUNT(
DISTI
NCT(
SIZE)
)FROM GARMENT;

(
ii
)SELECTAVG(
PRI
CE)FROM GARMENT;

Ans.i)COUNT(DISTI
NCT(
SIZE)
)
3
ii
) AVG( PRICE)
1800
16. Considert
hetabl
e‘Teacher
’gi
venbel
ow.
Whatwi l
lbetheoutputoft
hefol
lowi
ngquer
iesonthebasi
sof
theabovetabl
e:

(
i)
Selectcount
(Depart
ment)fr
om Teacher
;
(
ii
)Sel
ectcount(
*)fr
om Teacher;

Ans.count(
Department)
2
count(*
)
3
17. (
i) Namet woAggregate(
Group)f
unct
ionsofSQL.
(i
i) Considert
hetabl
e:
Tabl
e:Compa ny Whatout
putwil
lbedi
spl
ayedbyt
hef
oll
owi
ng
SI
D SALES SQLstat
ement?

26
S101 20000 SELECTAVG(
SALES)FROM Company
;
S103 NULL AVG(SALES)
S104 10000 15000
S105 15000

18. Consi
derthetabl
e‘Hot
el’
giv
enbel
ow:
Tabl
e: Hotel Mr .Vinaywant edtodi splayav
eragesal
aryof
EMPID Categor
y Salar
y eachCat egory.Heent eredthefoll
owi
ngSQL
statement.Identify er
ror(s)and Rewri
tethe
E101 MANAGER 60000
correctSQLstatement .
E102 EXECUTI
VE 65000 SELECTCat egor y
,Salar
yFROM Hot el
E103 CLERK 40000 GROUPBYCat egory;
E104 MANAGER 62000
Sel
ect categor
y,av
g(sal
ary
)fr
om hot
elgr
oup by
E105 EXECUTI
VE 50000 cat
egor
y ;
E106 CLERK 35000

19. Explai
nwhythefol
lowi
ngqueri
esgivedi
ff
erentout
put
sonexecut
ion:
i
.SELECTCOUNT( ENAME)FROM EMP;
Output:5
i
i.SELECTCount
(*)FROM EMP;
Output:8
Theenamefiel
dinempt abl
ewillbehav
ing3NULLv al
ues

20. Kunalhasent eredt hef oll


owingSQLcommandonTabl e‘
STUDENT’ t
hathasTotal
Marksasoneoft hecolumns.
SELECTCOUNT( *)FROM STUDENT;
Theout putdisplayedi s20.
Then,Kunal enterst hef ol
lowingcommand:
SELECTCOUNT( *)FROM STUDENTWHERETot al
Mar ks<100;
Theout putdisplayedi s15.
Then,Kunal enterst hef ol
lowingcommand:
SELECTCOUNT( *)FROM STUDENTWHERETot al
Mar ks>=100;
Hepredi ct
stheout putoft heabov equeryas5.Doyouagr eewi t
hKunal ?Giv
ereasonforyouranswer .
Yes,theout putf orsel ectcount(*)fr
om studentwher etotal
Marks>=100wi l
lbe5ast hereare20st udent
soutof
which15hav et otalMar kslessthan100so5st udentswi l
lhaveat otal
Marksgreat
erthanorequalt o100
21. Considerthet ablegi v
enbel ow:
Writ
ecommandf or( i
)andout putfor(i
i)
(i
)Todi spl
ayAreaal ongwit
hnumberofSal espersonswor ki
ngin
thatarea.
(i
i)SELECTAr ea,COUNT( *)FROM SalespersonGROUPBYAr ea
HAVI NGCOUNT( *)>1;

i
)Selectar
ea,
count
(*)fr
om sal
esper
songr
oupbyar
ea;
i
i)Area Count(
*)
Nort
h 2
South 2

22. Obser vethegi


v entablenamed“Loan”car
eful
lyand
predicttheoutputoft he
fol
lowingqueries:
selectcount(
fi
le_no)-count
(l
oan_amt)f
rom l
oan;

count
(fi
le_
no)
-count
(l
oan_
amt
)
1

27
I
nfor
mati
csPracti
ces
MySQLWor ksheet
-9
(
Joins)
1. I
nadat
abaset
her
ear
etwot
abl
es'
Cust
omer
'and'
Bil
l
'asshownbel
ow:

(
i)Howmanyr owsandhowmanycol umnswill
bether
eintheCart
esianpr
oductoftheset
wot
abl
es?
(
ii)Whi
chcolumninthe'Bi
ll
't
abl
eistheforei
gnkey?
i
)Therewil
lbe5columnsand15r owsintheCart
esi
anproductoftheset
wotables.
i
i)Cust
IDi
nt hebi
llt
ableisafor
eignkey

2. Consi
dert
het
abl
esHANDSETSandCUSTOMERgi
venbel
ow:

Withreferencetothesetables,Writecommandsi nSQLfor(i
)and(i
i
)andout putf
or(
ii
i)bel
ow:
(
i)DisplaytheCust No,CustAddressandcorrespondi
ngSetNameforeachcustomer.
(
ii
)Displayt heCustomerDet ail
sforeachcustomerwhousesaNoki ahandset.
(
ii
i)selectSetNo,SetNamef r
om Handsets,customerwher
eSetNo=Set CodeandCustAddr
ess='
Del
hi'
;
i
) Selectcustno,custaddress,set
name
from handsets,customer
wher esetcode=setno;
i
i) Selectcustomer .
*
from handsets,customer
wher esetcode=setnoandset namelike“Noki
a%”;
i
ii
) SetNo SetName
N2 Nokia3G
B1 BlackBerry

3. I
nadat
abaset
her
ear
etwot
abl
es"
Company
"and"
Model
"asshownbel
ow:

(
i)Identi
fytheforei
gnkeycolumninthet
abl
eModel.
(
ii)Checkev er
yvalueinCompiDcolumnofbot
hthetabl
es.Doyoufi
ndanydi
scr
epancy
?
i
)ComI Di stheforei
gnkeyinmodeltabl
e
i
i)Thediscrepancyisav al
ue4inthecompIDfi
eldofModelt
able.

4. Consi
dert
het
abl
esDOCTORSandPATI
ENTSgi
venbel
ow:

28
W1thr eferencet otheset ables,wntecommandsm SQLf or(
1)and( II
)andout
putf
or(i
ii
)below:
(
i)Displayt hePat No, PatNameandcor r
espondingDocNamef oreachpati
ent
(
ii
)Displayt helistofal lpatientswhoseOPD_ Daysar
eMWF.
(
ii
i)selectOPD_ Day s,Count (*)fr
om Doct or
s,Pati
ent
swherePat i
ents.
Depar
tment=Doctor
s.Depar
tmentGr
oupby
OPD_ Day s;
i
) Sel ectPat No, patName, DocName
Fr om Doct ors, Pati
ent s
Wher eDoct ors.DocI D=Patient
s.DocID;
i
i) Sel ectPat i
ent s.*
Fr om Doct ors, Pati
ent s
Wher eDoct ors.docI D=Patient
s.DocIDamdOPD_ Days=”MWF” ;
i
ii
) OPD_ Days Count (
*)
TTS 2
MWF 3

5. I
nadat
abaset
her
ear
etwot
abl
es"
Product
"and"
Cli
ent
"asshownbel
ow:

Wr i
tethecommandsi nSQLquer iesforthef oll
owing:
(i
)Todi splaythedet ail
sofProductwhosePr iceisint
herangeof40and120(
Bot
hvaluesincl
uded)
(i
i)Todi spl
aytheCl ientName,Cityfr
om t abl
eCl i
entandProduct
NameandPri
cef
rom tabl
eProduct
,wi
tht
hei
r
correspondingmat chingPI D.
(i
ii
)Toi ncreasethePr iceofallt
hePr oductsby20.
i) Sel ect*
From Pr oduct
wherepr i
cebetween40and120;
ii
) Sel ectcli
entName,city,
productName,pri
ce
From Pr oduct,
Cli
ent
Wher ePr oduct.
P_ID=Cli
ent.P_ID;
ii
i) Updat eProduct
Setprice=pr i
ce+20;

6. I
na.Dat
abaseSchool
ther
ear
etwot
abl
esMemberandDi
vi
sionasshowbel
ow.

(i
)Identi
fythef oreignkeyinthetableMember
.
(i
i)Whatout put,youwi l
lget ,whenanequi-
joinquer
yisexecut
edt
ogett
heNAMEf
rom MemberTabl
eand
correspondi
ngDi vNamef r
om Di v
isi
ontabl
e?
i) DivNoi saforeignkeyinmembert abl
e
ii
) Name Div
name
Shankhy a Media
Sunish Dance

7. I
naDat
abaset
her
ear
etwot
abl
es:

29
Tabl
eITEM:

Writ
eMy Sql queriesf orthefoll
owi ng:
(
i)Todi splayI Code,I Nameandcor respondingBr
andofthoseIt
ems,whosePr i
ceisbet
ween20000and45000
(
bothv aluesi ncl
usiv e).
(
ii
)Todi splayI Code, Pr i
ceandBName, oftheit
em whi
chhasINameas"Tel
evi
sion".
(
ii
i)Toincr easet hepr iceofalltheI temsby15%.
i) SelectI Code,I
Name, Brand
Fr om Item, Br
and
Wher eItem. I
code=Br and. I
Codeandpr i
cebet
ween20000and45000;
ii
) SelectI Code,Price, Brand
Fr om Item, Br
and
Wher eINameI N(“Telev ision”);
ii
i) Updat eItem
SetPr i
ce=Pr i
ce+Pr ice* 15/100;

8. I
naDat
abaset
her
ear
etwot
abl
es:
Tabl
eMAGAZI
NE:

(i
)Whi chcolumncanbesetast hePRI MARYKEYi ntheMAGAZI NEt able?
(
ii
)Whi chcolumni nthe‘MAGAZI NE’ t
abl eist heforei
gnkey ?
(
ii
i)Howmanyr owsandcol umnswi l
l bet herei ntheCar tesianproductoft heabov e2t abl
es.
(
iv)Writecommandi nSQLt odi spl
ayt hemag_ code,Mag_ Titl
eandcor r
espondi ngtypesforal
ltheMagazi
nes.
(
v)Wr i
tetheout put:
(vi
) Select Mag_ Code, Mag_ Titl
e, Number _ of_Pages, Ty pe Fr om MAGAZI NE,
MAGTYPE Wher e
Magazi ne.Mag_Category =Magt ype.Mag_Cat egor yandTy pe=’Spir
itual’
;
i) Mag_ Codecanbesetaspr i
mar ykeyi nmagazi netable
ii
) Mag_ categoryisthef oreignkeyi nmagazi net able
ii
i) IntheCar tesi
anpr oductoft heset wotablest herewi l
lbe6col umnsand16r ows.
iv
) SelectMag_ Code, Mag_ Tit
le, Type
From Magazi ne,magTy pe
Wher emagazi ne.mag_ cat egory=magTy pe.cat egory;
v) Mag_ Code Mag_ Titl
e Number _of
_ Pages Type
1 GoodDeeds 60 Spir
it
ual

9. I
naDat
abaseKamat
aka_
Sangam t
her
ear
etwot
abl
eswi
tht
hei
nst
ancesgi
venbel
ow:

Wri
teSQLqueri
esf
orthefoll
owing:
(
i)Tocounthowmanyaddressesar
enothavingNULLval
uesintheaddresscol
umnofstudent
stabl
e.
(
ii
)Todispl
ayName,Cl
assfrom STUDENTtableandt
hecorr
espondingGradefr
om SPORTStabl
e.

30
(
ii
i)Todispl
ayNameoft hestudentandt heircorr
espondi
ngCoachnamesf
rom STUDENTSandSPORTSt
abl
es.
i) Selectcount (*)
from st
udent s
whereaddr essisnotnul l
;
ii
) SelectName, Cl
ass,Grade
From students,sports
Wherest udents.admno=spor ts.admno;
ii
i) SelectName, Coachname
From students,sports
Wherest udents.admno=spor ts.admno;

10. InaDatabaseMult
ipl
exes,t
her
earetwotabl
eswi
tht
hef
oll
owi
ngdat
a.Wr
it
eMy
SQLquer
iesf
or(
i)t
o(i
i
i),whi
ch
arebasedonTi
cketDet
ail
sandAgent
Detai
ls:

(i
)Todi splayTcode, NameandAnameofal lt
her ecordswher ethenumberoft i
cketssoldismorethan5.
(i
i
)Todi splayt otalnumberoft icketsbookedbyagent“ Mr.Ay ush”
(i
i
i)Todi splayAcode, Anameandcor respondi ngTcodewher eAnameendswi t
h“ k”.
(i
v)Wi t
hr eferencet o“ Ti
cketDetail
s”t able,whichcol umni sthepr i
mar ykey?Whi chcolumnistheforei
gnkey
?
Givereason( s)
i
) SelectTCode, Name, AName
From Ti cketDetai
ls,Agent Det ai
ls
Wher eA_ Code=Acodeandt i
cket s>5;
i
i) Selectsum( Tickets)
From Ti cketDetai
ls,Agent Det ai
ls
Wher eA_ Code=AcodeandaName=” Mr .Ayush”;
i
ii
) SelectAcode, AName, Tcode
From Ti cketDetai
ls,Agent Det ai
ls
Wher eA_ Code=AcodeandAnamel ike“k%”;
i
v) InTicket Detai
lsTCodei sPr i
mar yKeyandA_ codeisforeignkeyast woticket
scannothavesame
no.wher eastheAgentcodei .e.A_Codei sreferencingit
sv al
uesfrom theAcodefiel
din
Agent Det ai
lstabl
e.
11. I
nadat abaset herear etwotables‘CD’ and‘ TYPE’ asshownbel ow:

(i
)Namet hePrimarykeyin‘‘
CD’
’tabl
e.
(i
i
)Namet heforeignkeyin‘‘
CD’
’tabl
e.
(i
i
i)Writ
et heCar di
nalit
yandDegreeof‘
‘TYPE’’
table.
(i
v)Checkev eryv alueinCATEGORY columnofbot hthetabl
es.Doy ouf
indanydi
scr
epancy?St
atet
he
di
screpancy.
i
) Code
i
i) Categor y
i
ii
) Cardinali
ty=4andDegr ee=2oft heTy petabl
e
i
v) Yes,thedi scr
epancyi
sav al
ue77i ncategoryfi
eldi
nCDt abl
e.

13. Consi
dert
het
abl
es‘
Fl
ight
s’&‘
Far
es’
giv
enbel
ow:
Fli
ght
s
FNO SOURCE DEST NO_
OF_
FL NO_
OF_
STOP

31
IC301 MUMBAI BANGALORE 3 2
IC799 BANGALORE KOLKATA 8 3
MC101 DELHI VARANASI 6 0
IC302 MUMBAI KOCHI 1 4
AM812 LUCKNOW DELHI 4 0
MU499 DELHI CHENNAI 3 3
Fares
FNO AIRLINES FARE TAX
IC301 I ndi
anAi r
li
nes 9425 5
IC799 Spi ceJet 8846 10
MC101 DeccanAi r
li
nes 4210 7
IC302 JetAi rways 13894 5
AM812 I ndi
anAi r
li
nes 4500 6
MU499 Sahar a 12000 4
Withrefer
encetothesetables,wri
tecommandsinSQLf or(i)and(i
i
)andoutputf
or(i
i
i)bel
ow:
i
.Todi spl
ayfl
ightnumber,source,ai
rl
inesoft
hosefli
ghtswherefar
eislesst
hanRs.10000.
i
i.TocounttotalnoofIndi
anAi rl
i
nesflight
sst
art
ingfrom v
ariousci
ti
es.
i
ii
.SELECTFLI
GHTS.
FNO,NO_
OF_
FL,AI
RLI
NESFROM FLI
GHTS,
FARESWHEREFLI
GHTS.
FNO=FARES.
FNOAND
SOURCE=’
DELHI’
;
i
) Sel
ectFli
ghts.FNO,Source,
air
li
nes
Fr
om Fli
ghts,Fares
WhereFli
ghts.FNO=Fares.
FNOandf ar
e<10000;
i
i) Sel
ectSum( No_of_
Fl)
Fr
om Fli
ghts,Fares
WhereFli
ghts.FNO=Fares.
FNOandai r
li
nes=”
Indi
anAi
rl
ines”
;
i
ii
) FLIGHTS.
FNO NO_
OF_
FL AIRLINES
MC101 6 DeccanAir
li
nes
MU499 3 Sahara

14. At
abl
eSTUDENThas5r
owsand3col
umns.Tabl
eACTI
VITYhas4r
owsand2col
umns.Whatwi
l
lbe
t
hecar
dinal
i
tyanddegr
eeoft
heCar
tesi
anpr
oductoft
hem ?
Thedegreewil
lbe5
Thecardi
nal
it
ywillbe20

15. Consi
dert
hef
oll
owi
ngt
abl
enamed“
GARMENT”
.

Whati
sthedegr
eeandcar
dinal
i
tyof‘
Gar
ment
’tabl
e?

Degree=5
Cardi
nali
ty=6

16. I
naDat
abase,
ther
ear
etwot
abl
esgi
venbel
ow:

Wr i
teSQLQuer i
esforthefoll
owing:
(i
)Todi splayemployeeids,namesofempl oyees,j
obidswithcor
respondingj
obti
tl
es.
(i
i)Todi splaynamesofempl oyees,sal
esandcor respondi
ngjobt i
tleswhohaveachi
evedsalesmor
ethan
1300000.
(i
ii
)Todi spl
aynamesandcor respondi
ngj obti
tl
esoft hoseempl oyeewhohave‘SI
NGH’(anywher
e)i
nthei
r
names.
(i
v)Identi
fyforei
gnkeyi nthetableEMPLOYEE.
i
) SelectEmployeeID,Name, JobI
D,JobTitl
e
From Employee,Job
WhereEmpl oyee.j
obid=j
ob.j
obid;

32
i
i) Sel
ectName, Sal
es,Jobti
tl
e
Fr
om Employee,Job
WhereEmployee.j
obid=j
ob.
jobi
dandsal
es>1300000;
i
ii
) Sel
ectName, Jobti
tl
e
Fr
om Employee,Job
WhereEmployee.j
obid=j
ob.
jobi
dandnamelike“%Si
ngh%”
;
i
v) JobI
D

17. Consi
dert
het
abl
esgi
venbel
ow.
Sal
esper
son Or
der
s

i.TheSalesper sonId col umni nt he" Sal esper son"t ablei sthe Pr i
mar yKey .
The
SalespersonIdcol umni nt he" Or ders"t ablei saFor eignKEY.
i
i.Cant he‘SalespersonId’ besetast hepr i
mar ykeyi nt able‘Or ders’.Giver eason.
Ii.Nosal esper sonI Dcannotbesetast hepr imar ykeyi nor derstabl east wosal espersoncangivemul t
iple
orders,sot heor der edwi llbepr imar ykeyandsal esper sonI Dwi llbet hef or
eignkeywhichwi l
lrefer
it
sval uesf rom t hesal esper soni df ieldofsal esper sont abl e.
18. Wi t
hreferencet otheabov egi vent ables, Writecommandsi nSQLf or( i
)and
(
ii)andout putfor(iii
)below:
i
. Todi splaySal esper sonI D, names, orderidsandor deramountofal lsalespersons.
i
i. Todi splaynames, sal esper sonsi dsandor deri dsoft hosesal esper sonswhosenamesst ar
twith‘A’
andsal esamounti sbet ween15000and20000.
i
ii
. SELECT Sal esper son. Salesper sonI d,name,age,amount FROM Sal esperson,or
ders WHERE
Salesper son.salesper sonI d=Or der s.salesper sonIdANDAGEBETWEEN30AND45;
i. SelectSal esper son. sal esper sonID, Name, OrderID
From Sal esper sons, order s
Wher eSal esper son. sal esper sonID=Or ders.Salesper sonID;
i
i. SelectSal esper son. sal esper sonID, Name, OrderID
From Sal esper sons, order s
Wher eSal esper son. sal esper sonID=Or ders.Salesper sonID
andnamel i
ke“ A%”andamountbet ween15000and20000;
ii
i. Salesper son. Salesper sonId name age amount
2 Suni l 34 54000
5 Chr is 34 24000

19. Consi
dert
het
abl
esgi
venbel
ow:
Tabl
e:Facul
ty
Teacher
Id Name Addr
ess St
ate PhoneNumber
T101 Sav
itaShar
ma A-
151,Adar
sh Del
hi 991019564
Nagar
T102 DeepakGhai K-
5/52, Vi
kas Mumbai 893466448
Vi
har
T103 MahaLakshmi D-
6 Del
hi 981166568
T104 Si
miAr
ora Mumbai 658777564
Table:
Cour
se
Cour
seI
d Subject Teacher
Id Fee
C101 I
ntr
oduc
tor
yMat
hemat
ics T101 4500
C103 Phy
sics T101 5000
C104 I
ntr
oduc
tor
yComput
erSci
ence T102 4000
C105 Adv
anceComput
erSci
ence T104 6500
(
i)Whichcolumnisusedtor
elat
ethetwotabl
es?
(
ii)I
sitpossibl
etohaveapr
imarykeyandaforei
gnkeybot
hinonet
abl
e?Just
if
yyouranswerwi
tht
hehel
pof
t
ablegivenabove.

33
i
) TeacherI
D
i
i) Yes,i tispossi blet ohavebot hPrimar yandf orei
gnkeyi nonet abl
e.ForExampl ei ntheabovecour
se
tableCourseIDi sapr i
mar ykeyandt eacheridistheforeignkey .
20. Wi t
hrefer encetotheabov egiventables, writ
ecommandsi nSQLf or(i)and(i
i)
andout putf or(i
ii
):
(i
)Todi spl ayCourseId,Teacher I
d,NameofTeacher ,PhoneNumberofTeacher sli
vinginDelhi.
(i
i)Todi splayTeacher I
D, NamesofTeacher s,SubjectsofallteacherswithnamesofTeacher sstar
tingwi
th‘
S’
.
(i
ii
)SELECTCour seI
d, Subject,
Course. TeacherId,
Name, PhoneNumberFROM
Facult
y,Cour seWHEREFacul ty.
Teacher Id=Cour se.TeacherIdANDFee>=5000;
i
) Selectcour sed,Cour se.TeacherID,Name, PhoneNumber
From Faculty,Course
WhereFacul ty.TeacherID=Cour se.TeacherIDandst ate=”Delhi
”;
i
i) SelectFacul ty.
Teacher ID,Name, Subject
From Faculty,Course
WhereFacul ty.TeacherID=Cour se.TeacherIDandnamel ike“S%”;
i
ii
) CourseId Subject Course.TeacherId Name PhoneNumber
C105 Adv anceComput erScience T104 SimiAr ora 658777564
21. Considert hetablesgivenbel owwhi char el
inkedwi t
heachot herandmai ntai
nsref
er ent
iali
ntegr
ity
:
Table:SAP

Tabl
e:St
ore

Wi
thref
erencet ot heabov egi vent ables, writ
ecommandsi nSQLf or(i)and( i
i)andout putfor(ii
i
)bel ow:
i. Todi splayt heI temCode, ItemNameandRecei vedDat eofal lt
hei t
ems.
ii
. Todi splaySAPI D,ItemName, It
emSt orageLocationofal lthei temswhoseRecei veddat ei saf
ter2nd
May2016.
ii
i. SELECT SAPI D,I
temName, STOREI D FROM SAP, StoreWHERESAP. It
emCode=St ore.I
temCodeAND
St oreLocat ion=“ HauzKhas”
iv. Whatwi llbet hedegr eeandcar dinalit
yoft hecar tesianpr oductf ormedwhi l
ecombi ningboththe
abov egiv ent ables‘ SAP’ and‘ Store’ ?
v. Sangeet ai snotabl et oaddanewr ecordi nthetabl e‘Store’throught hefol
lowingquer y
:
I
nser tint ost orev alues( 1206,1006, ’KarolBagh’ ,‘
2016/ 07/25’);
Ident if
yt heer r
ori fther eisany
i
. Sel ectSAP. ItemCode, ItemName, Recei v edDate
Fr om SAP, Stor e
Wher eSAP. I
temCode=St ore.ItemCode;
ii
. Sel ectSapI D, It
emName, st oreLocat ion
Fr om SAP, Stor e
Wher eSAP. I
temCode=St ore.ItemCodeandRecei v edDat e>”2016- 05-02”;
i
ii
. SAPI D ItemName STOREI D
S1001 Recei ver 1201
S1004 Inverter 1204
i
v. Thedegr eewi l
lbe8
Thecar dinalitywi llbe25
v. Shewi l
lnotbeabl et oinser ttherecor das1006I temCodei snott her eintheitemCodef i
eldi
nSAP
tabl eandsi nceI temCodei nst oretabl eisaf oreignkeysoi twi l
lreferitsvaluef r
om t heitemcode
fieldofSAPt able.

34
I
nfor
maticsPracti
ces
MySQLWor ksheet-
10
(
Transact
ion)
1. Whi
chcommandi
susedi
nMy
Sql
tomaket
hechangesi
ndat
abaseper
manent
?

Commi
t

2. Gi
veonedi
ff
erencebet
weenROLLBACKandCOMMI
Tcommandsusedi
nMy
Sql
.

Rol
lbackrever
sesallt
hechangesbei
ngdoneusi
nginsert
,updateordel
eteaf
terstar
ti
ngthet
ransact
ion.
Commi tsav
esallt
hechangesbeingdonebyI
nsser
t,Updateordel
eteaft
erst
arti
ngthetr
ansact
ion.
3. At
abl
enamed‘
GAMES’
hast
hef
oll
owi
ngcont
ent
s:

Writ
etheoutputt
hatwi
l
lbedisplay
edbystatements(i)and(
ii
).
SELECT*FROM GAMES;
SETAUTOCOMMI T=0;
I
NSERTI NTOGAMESVALUES( 105,
'CHESS’
,
2,9000);
ROLLBACK;
SAVEPOINTS1;
SELECT*FROM GAMES; -
--
--
--
--
--
-(i
)
I
NSERTI NTOGAMESVALUES( 108,
'LAWNTENNI S’,
4,
25000);
SAVEPOINTS2;
I
NSERTI NTOGAMESVALUES( 109,
'CRI
CKET’,
11,20000);
ROLLBACKTOS2;
SELECT*FROM ITEM;--
--
--
--
--
--(
ii
)
i
)
GCode GameName Number_
of_
Play
ers Pri
zeMoney
101 Carom Board 2 5000
102 Badminton 2 12000
103 TableTenni
s 4 8000
i
i)
GCode GameName Number_
of_
Play
ers Pri
zeMoney
101 Carom Board 2 5000
102 Badminton 2 12000
103 TableTenni
s 4 8000
108 LawnTennis 4 25000

4. Consi
dert
heSt
utabl
e

Thef
oll
owi
ngSQLquer
iesar
eexecut
edont
heabov
etabl
e

I
NSERTINTOStuVALUES(5,
'
Gagan'
);
COMMIT;
UPDATEStuSETname='
Abhi'
WHERERoll
no=4

SAVEPOI
NTA;
I
NSERTINTOSt
uVALUES(
6,'
Chri
s'
);
SAVEPOINTB;
I
NSERTINTOSt
uVALUES(
7,'
Babi
ta'
);
SAVEPOINTC;

ROLLBACKTOB;
Whatwil
lbet
heoutputoft
hef
oll
owi
ngSQLquer
ynow:
SELECT*FROM St
u;

Rol
lNo Name
1 Ashi
2 Bi
mmi
4 Abhi
6 Chri
s
35
5. Gi
venbel
owi
sthe‘
Stu’
tabl
e:
RNO NAME
1 Ami
t
2 Bhi
shm
Thefol
l
owingst atement
sareenter
ed:
SETAUTOCOMMI T=0;
I
NSERTI NTOStuVALUES( 5,‘
Rahul
’);
COMMI T;
UPDATESt usetname=‘Rahul
iy
a’whereRno=5;SAVEPOINTA;
I
NSERTI NTOStuVALUES( 6,‘
Cri
sti
na’)
;SAVEPOINTB;
I
NSERTI NTOStuVALUES( 7,‘
Fauzi
a’)
;SAVEPOINTC;
ROLLBACKTOB;
Nowwhatwi l
lbet heout
putofthefol
lowi
ngstatement?
SELECT*FROM Stu;
RNo Name
1 Amit
2 Bhi
shm
5 Rahuliya
6 Cri
stina

6. Geet anj
alihadcreat
edat able“
Cust
omer”i
nthedatabase“Test”
.Immedi at
elyaf
tert hesuccessf
ulcreat
ionofthe
database,she wr otet he Rol
l
backcommand to undo the creat
ion ofthe table.Did she executerol
lback
successfully?Expl
ain.
No, Shedi dnotexecutethecommandsuccessf
ull
yasr ol
lbackcommandonl yr
ev ersesthechangesdoneusi ng
INSERT, UPDATEorDELETE.

7. Gi
venbel
owi
sthe‘
Depar
tment
’tabl
e:
SETAUTOCOMMI T=0;
UPDATEDepar t
mentSETDEPNAME=‘ OFFI
CE’WHEREDEPNAME=

ADMI N’
;
I
NSERTI NTODepartmentVALUES(104,‘
HRD’)
;
UPDATEDepar t
mentSETDEPNAME=‘ FRONTOFFICE’WHERE
DEPNAME=‘ RECEPTION’;
COMMI T;
DELETEFROM DepartmentWHEREDEPNAME=‘ FRONTOFFICE’
;
ROLLBACK;
SELECT*FROM Department;
Whatwillbet
heoutputoft
heabovegivenSELECTstat
ement?
DepCode DepName
101 OFFI
CE
102 FRONTOFFI
CE
103 PERSONNEL
104 HRD

36

You might also like