0 ratings0% found this document useful (0 votes) 48 views13 pagesSQL Notes3 QA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
=e
MySQL Functions and
Querying using SQL
In this Chapter..
«# Types of SQL Functions
+ Mathematical Functions
“MySQL fumetion performs a pre-defined task and returns a
single value such as a numerical, string or date/time value
Functions operate on zero, one, two or more values provided
to them, these values are called parameters or arguments
‘MySQL support number of built-in functions.
To use any function, we need to specify the column to which
the function should be applied
eg, SELECT function(colum_nane)
ROW table_rane:
To specify multiple columns, you ean write as fllows:
eg. SELECT *, function(cotunn_nane
FROW tabTe_rane:
for SELECT coluan_pamel, functicncolum_pane2)
FROM table rane: 7
Types of SQL Functions
SQL provides two types of functions, which are as fallows
(i) Single-row Funetions This type of funetion work with
asingle-row at atime,
totus a result for each row ofthe table, on
which the query is performed. Examples of
single-row functions inchide CHAR(,CONCAT),
INSTR), et.
(i) Multiple-row Funetions This type of function work
with data of multiple-rows ata time and return a single
output value.
Examples of multiple-row functions include SUMO,
AVG), COUNT), ete
« String/Text Functions
+ Date/Time Functions
Mathematical Functions
MySQL provides a number of fimetions used for performing,
mathematical calculations on database. Mathematical
functions are very important in SQL to implement different
mathematical concepts in queries: Some mathematical
finetions are explained with examples as follows:
POWER(/POW()
eturns the value of a number raised to the
number.
This funtion
power of anol
‘Syntax POKER(H.N) or POW(M.W
where, Mis the base
xl N isan exponent,
eg. mysql> SELECT POWER(2,2)
The above query produces the following output:
raysql> St
The above query produces the fallowing output
ELECT POWER( 30.0.2)
‘The above query produces the following output
|/CBSE_TG)ROUND()
“his function rounds up the number tothe upwards ot
downwards to the nearest whole number,
Syntax AONDCH.NY or ROUNOCHD
where, M i the number to be rounded off nd N i the
dhumber of places to which the number should be rounded
Off IN isnot specified, then itis assumed zero (0)
eg mysql> SELECT ROUNOCA 65,1)
“The above query produces the following output
mysql> SELECT ROUND (5.43)
‘The above quety produces the following output
mysql> SELECT ROUND (5.43)
‘The above query produces the following output:
mysql> SELECT ROWND (1.645,0)
‘The ubove query produces the following output
¥ pauwo(1 648.0
mysql> SELECT ROWND (3.45.-1)
‘The above query produces the following output:
| Rounoea.45, —2)
MopQ,
‘This fuetion retums the remainder ofa number dividing by
another number.
Syntax WOD(Dividerd, Divisor)
eg mysql> SELECT H00(11, 4
‘The above query produces the fllowing output
| voor?
CBSE Term | Informatics Practices Xil
saysq]> SFLECT MOD(— 25.7)
‘The above query produces the following output
rnysql> SELECT MOD(25.4.7)
‘The above query produces the following output:
String/Text Funct
‘The string/text functions of SQL are used to extrac, change,
format or alter character strings. They avcept a character
string as an input anxl provides character sting or numeric
values as an output, Some stringftext functions are explained
with example as follows
UPPER(/UCASE()
‘This fanetion converts the characters of a string into the
uppercase characters.
Syntax UPPER(str/column_nane)
for UCASE(str/colun_nane)
eg mysql SELECT UPPER( ‘nystring')
The above query produces the following output
UPPERC aystring®
avsTRns
Table : Ite
joode Desep ou Sold
101 Stationary ‘700 20
102 Food 1200 40
103 Sports 000 500
104 Food 00 120
105 700 740
6.8, Write a query to display the item deseription
"uppercase letter fom table Item
nysql> SELECT UPPER(Desco) FROM Tten:
Te above query produces the following output:
F uepencoescp) |
STATIONARY‘CBSE Term II| Informatics Practices XII
‘The ubove query produces the following output
{ease coesep) |
‘SraTionaay
ooo
2087S
Foun)
S?o8rs
LOWER(/LCASE()
‘This function converts the charactors of an argument string,
to the lowercase characters. The return value has the same
data type as the argument.
Symtax LOWER(Str/colum_nane)
or LCASE(str/cotunn _nane)
eg. nysel> SELECT LOWER( MYSQL")
‘The above query produces the following output
eg. Write a query to display the item deseription In
lowercase letter from table Item.
mysql> SELECT LOwER(Descp) FROM Item:
‘The above query produces the following output:
Lowenco
‘Stationary
food)
Sports
fod
Esports
or mysql> SELECT LCASE(Desep) FROM Item:
‘The above query produces the following output:
{ UCASE(oesep) |
‘Stationary
food
ports
| toca i
SUBSTRING(/SUBSTR(/MID(),
“This funtion etums the substring specified number of
characters) from a particular position of a ven string.
Syntax SuBSTR(str eo unr_pare, pos, len)
or SUBSTRINS(Str/column_pane. pos. er)
or “MD(str/eolum_pane, 0s, en)
where, srs string fom which a substring f returned, pos
isan integer indicating the string position and Tenis an
integer indicating the length ofthe substring.
eg. aysql> SELECT SUBSIR
(CEASYCALCULATION’ ,11)
‘The above query produces the following output:
F susstac exsvcatcutarion®,$,20) |
ALCULATION
nysql> SELECT SUBSTR
(EASVCALCULATION' 7)
‘The above query produces the following output
Waite the output of the following command,
mysql> SELECT SUBSTR(Descp,3.4) FROM Item WHERE
(ovie700:
The above query produces the following output
Write the output of the following command.
mysql> SELECT MID(" Informatics Practices’ 2,8)
‘The above query will produce the following outputs
Wirt the output ofthe following command.
mysql> SELECT UPPER(MID(Descp.2.8)) FROM Tten
WHERE Icode=101
‘The above query will produce the following output:
Jurrencniocdesep,2.60)1
TaTTONaR
LENGTH(
This function returns the length ofthe string in bytes, It
Includes the count of bank spaces in the string
Syntax LENGTH (string/colum_nene)
eg, mysql> SELECT LENGTH( ‘easycalcul:
Above query produces the following output
Write a query to display the item code and length of each,
item's descriptionsysql> SFLECT Tcode. LENGTH(Descp) FROM Ttem:
Above query produces the following output:
LENeriiesep)
LEFTO,
This function etums a speciied numberof characters from
the left of the string, This function returns NULL, any
argument is NULL
Syntax LEFT(string, Tength)
eg. ysql> SELECT LEFT( India’ .3)
Above query produces the following output:
EmpNo ‘EmpAdd EmpSal
EOL Jalpur -45000.00
02 ent 0000.00
E03 Jabalpur 6000.00
E04 Delhi ‘0000.00
cog. Write a query to view first three characters of employee's
name from the table Empdetai.
sysql> SFLECT LEFT(EnpNane,3) FROM EmpdetaiT
Above query produces the follow
LEFTCEnptane,3)
1goutput
ia
RIGHT)
Working of this function i just reverse of LEFT function. It
relumsw specified numberof characters from the right ofthe
String, This fnction returns NULL, fany argument i
NULL
Syntax 81657 strong. Jongh)
cog. sysql> SELECT RIGHT Inda’.29
Above query produces the following output
CBSE Term I! | Informatics Practices Xil
RUGATC India" 3)
eg, Write a query to view last three characters of emplayee’s
address from the table Empdetail.
nysq]> SELECT RIGHT (Emptdd,3) FROM Enpdetail
Above query produces the folowing output:
INSTRO,
‘This fanetion takes a string and a substring of it as arguments
and returns an integer which indicates the position of the
first occurrence of the substring within the string,
Syntax INSTR(or_str/colunn_nane, sub_str)
where, ori_stris the string to be searched and sub, st is the
string to be searched from ori_st.
ce, mysql> SELECT INSTR("firstexan',‘e")
Above query produces the following output:
INSTRCTirstexan’"e")
ost
nysql> SELECT EmpName, INSTR(EmpNane, °4") FROM
Empdetait
WHERE EnmpSal BETWEEN $0000 AND 60000)
Above query produces the following output:
nae
inal
vi
pane, "172
TRIM
This fimetion i sed to return string ater removing all
prefix or ufc spaces Bom the given sing
‘Syntax TRIM([{BOTH|LEADING| TRAILING} Cremstr] FROM]
serves lum. ano)
Here, BOTH indicates the prefixes and suffices from both
Jeft and right are to be removed.
LEADING indicates only the leading prefixes are to be
TRAILING indicates only the tiling suffixes aro tobe
removed‘CBSE Term II| Informatics Practices XII
remstr is the string to be removed. Its optional, ifuot
specified and spaces are removed.
FROM itis keyword
str isa string from where remstr isto be removed,
eg. Case I Ifno specifier is given, BOTH is assumed and
the strings trimmed from both end.
rnysql> SELECT TRIM(® India
Above query produces the following output:
Paine Inge |
dia
Case 2 Ifleading specifier is given, then the prefix partis
trimmed
mysql> SELECT TRIM(LEADING * |
FROM IHF tIndial 11")
Above query produces the following output:
Fram
Tawiceapine “1° Heningiat 11)
Indiat tt
Case 3 IF trailing specifier is given, then the suffix part fs
trimmed,
mysql> SELECT TRIM (TRAILING *!
Titindia? 1!"
Above query produces the following output:
FROM
FRON" 111 1Ongiatlii") |
| TRIMGRAT ane
iiitnaia
LTRIM)
‘This function removes the leading spaces fom the characters
of wating passed as an argument. Spaces inthe middle or
trang spaces are not rennoved.
Syntax (TRIM(str/colunn_rone)
eg.mysql> SELECT LIRIN'TE 16 a string
Ahove query produces the following output
Pia Oa
argurent)
is 9 string argument? |
Te is a string argunent
RTRIM()
‘This function removes the trlling space from the characters
ofa string passed as an argument. Spaces in the middle or
leading spaces are not removed,
Symtax RTRIM(Str/colum_nare)
eg. mysql> SELECT RTRIMC'TE is a string
Above query produces the following output:
argurent)
Rain Cit 1s 2 string argument
Te 1s. a string argunent
Date/Time Functions
MySQL stores date in datetime format, representing the
century, month, year, day and hours. The date and tne
functions are used to perform operations on the date
data stored in the database. The default date format is
YAY-MM-DD in MySQL.
Some date/time functions are explained with examples as
follows
DATE),
‘This function returns only DATE part from the given
dlateytime argument.
Syntax Date( SELECT OATE(*2021-09-30 20:29:18")
Above query produces the following output:
DATEC “2021-09-30 20:29:13"?
21-09-30
MONTH)
‘This fanetion returns the MONTH pat from the date
argument within a range of Ito 12 (January to December)
and it returns 0 if MONTH part ofthe date contains NULL.
Syntax MONTH(date/colum_nane)
ce. mysql> SELECT MONTH 2021-09-30")
Above query produces the following output:
wowTic 2021-09-20")
Coach, id Couchname Age Sports Dateofapp Fay Sex
1 KUKRAA 35 KARATE 1996.03.27 1000.00 _M
2 RAVINA S41 KANATE 1998-01-20 130000 F
3__KARAN 84 SQUASH 198808-19 200000 M7
Waite a query to display the month of all applicants whose
age is 35,
nysql> SELECT MONTH(Oateafapp) FROM Club WHERE
A
Above query produces the followingMONTHNAME()
This function returns the name of the month from a date
specified as an argument.
Symtax MONTHVAME(date/coTumn_nane)
eg. mysql> SELECT MONTANAME(*7071-09-30")
Above query produces the following output:
MONTH
Write a query to display the month of appliation forall
couches whose getting more than 1200 as pay.
mysql> SELECT MONTANAME (Dateofapp) FROM Club WHERE
Pay>=1200;
Ahove query produces the following output
HONTHNANE( DateoFapp)
January
February
DAY),
‘This function returns the day of the
from a date specified as an argument,
Syntax DAY date/column_nane)
eg. nysal> SELECT DAY( 2021-09-30"):
Above query produces the following output
mth (from 1 to 31)
Write a query to display the day of application forall couches
whose getting more than 1200 as pay
nysq]> SELECT DAY(DateoTapp) FROM Club WHERE
Pay>=1200:
Above query produces the follow
1goutput
Fpavcpave
YEAR()
“This fumetion etuens the YEAR part from the given date
argument, The return value is nthe range of 1000 to 9999 or
O for mul date
Syntax YEAR(cate/colum_nane)
eg mysql> SELECT YEAR("2021-09-30")
CBSE Term | Informatics Practices Xil
Above query produces th
# yeaac-z021-09-30
following output:
202i
White a query to display the year of application for all female
coaches.
anysql> SELECT YEAR(Dateofapp) FROM Club WHERE
Sexe'F
Above query produces the folowing output:
PVeaniostestenny
1998
DAYNAME()
It returns the name of the week day from a date specified as
anargument.
‘Syntax DAYNAME(date/colum_nane)
eg mysql> SELECT DAME ("2021-09-30")
Above query produces the following outputs
DAYHAME(" 2021-09-30")
Thursday
Write a query to display the day name of application for all
female coaches.
riysql> SELECT DAYNANE(Dateofapp) FROM Club WHERE
Sex'F
Above query produces the following output:
Now()
‘This function returns the current date and time in the format
AYW-ABEDD IIL MASS or AYNMDDHINISS
Syntax OKO
eg. ysql> SELECT NEKO
Above query produces the following output:Chapter
Practice
Objective Questions
* Multiple Choice Questions
1
Ans.
Ans.
Ans.
. (} Single-row fun
. Which of the following is the correct sy
Which type of SQL function work with a
single-row ata time?
(a) Multipte-row fonctions
(b) Single-ow funetons
(Both a) and )
4) None ofthe above
jous work with as
row at atime. It
returas a result fr each row ofthe table, on which the
‘query is performed.
. Which function accepts a character string as an
input and provides character string or numeric
values as an output?
(a) Text
(9 Time
tb) Date
(d) Math
(a) Text fmetion accept character strings an input and
provides character string orn
meri vlltes as an tpt
of
LCASE() funetion?
fa) LEASE
(b) LCE (col van_nae
(©) LEASE(Str/Goum_nane)
{@) None ofthe abow
(6 This function converts the characters of an argument
String to the lowercase characters. The syatax is
CASE(str/coTum_pae) or LOWER(str/celutn_name)
|. Which of the following function converts the
characters of an argument string to the uppercase
characters?
(a) UCASE()
(b) UPPER
(©) Both a) and b)
{@) None of the above
{c) This fumetion converts the characters of string int the
uppercase characters. The syntn i
JPPER(Str/columr name) or UCASE(str/eo] uin_name)
5. The correct output of
nyq]>SELECT TRIMCLEBDING “AY FROM °BAK Iida 868,
(a) Inia de hy) India tte
(o) dee India (4) &ete India
‘Ans. (b) This inetion is use to return a string ater removing all
ix or sufix spaces from the given string In other words,
ading and tailing spaces fom a
indicates only the leading prefixes
pr
{his function re
sven string 1
ADIN
te to be removed
6. The default date format is
(@ MM-DD-vYYY
(9 DD-AIM-YYY
(b)The default format for dates in MySQL is fed as
YAVEMM-DD and we have to follow that
) YYY-MM-DD
(4) None ofthese
Ans.
7. Which of the following function returns an integer
that indicates the position of the first occurrence of
the sub-string within the string?
(a) INSTR) (b) RTRIM()
(LENGTH) (a) TRIM)
Ans. (a) The syntax ofthe INSTR function is
INSTR(ri_str/colunn_nane.sub_str)
where, oristris the string to be searched and
sub_stei the string tobe searched from o
8. Write the output of the following SQL. command,
SELECT ROUND (4789)
ova
(aso
fe) The ROUNDS) funetion ours up the number to the
upwards or downwards tothe noarest whole number
(oso
8. Which of the following function returns the name
of the month from selected date?
(a) MONTH (Gato)
(b) MONTH_NAME(date)
{) MONTHNAME(at
(@) NAME_MONTH)
(e)mysal> SELECT MONTES 9
Abave query produces the fllawing output
Ans.
2-30
AME
|/CBSE_TG)10. Which of the following fimetion returns only the
day number from month of selected date?
(a) DAY Gate)
() DAYNO(date)
(9 DAY_NUMBER(ate)
(@) DATE (te)
‘Ans. (a) nysqi SELECT OAY( 2021.09.30");
Above query produces the fellowing output
wy}
11. What will be retuned by the given qu
SELECT ROUND(1SS. 669.2)
@) 1536 153.66
fo 153.67 (@)1s37
Ans. (e)ROUND) funtion wll round off the decimal places up
102 places
12, What will be retuned by the given query ?
SELECT INSTRCINOIA', 01°)
we 03
oa 3
‘Ans. (b) INSTR function returns the starting index of the
substring that is passed ay second argument in the Fanetion
* Case Based MCQs
13, Shanya Kumar is working with the following table
Customers:
‘Table : Customers
ENO CNAME, CITES.
ce SANYAM.
2 SHRUTI
3 MEHER MUMBAI
cs ‘AKSHI (CHENNAL
a RITESH INDORE,
cs RAHUL, DELI
AMER (CHENNAL
MINAKSH ANGLO}
co ANSHUL MUMBAI
She has been given some queries to develop . Help
her to achieve the task,
Shanya Kumar is working with the following table
‘Customers : (Put the table)
She has been given some queries to develop. Help
her to achieve the task.
() Choose the correct query to display the length of
customer's name.
CBSE Term | Informatics Practices Xil
(a) mysql >SELECT LENGTHCCNAME) FROM
(b) mysql >SELECT LEN(CNAME) FROM Customers;
(e)mysal>SELECT COUNT(NAME) FROM Customers
(@)mysaloSFLECT COUNT(CNEME) FROM Customers:
{i) Choose the correct query to display the eity names
in lower case letter whose CNO is either C5 or C9.
{a) mysql>SELECT UPPERCASECCITIES)FROM Customers
WHERE CWO ="C5" OR C10 ="C3
(b) mysql oSFLECT UPPERCCITIES)FROM Customers WHERE
cw ="¢8" oR “9
(c)mysal>SELECT UPPER(CITIES)FROM Customers WHERE
NO ="C5" OR CHO ='C9
(€)nysaQeSELECT UPPERCCITIES)FREM Customers HERE
(HO 1S EITHER “C5" OR “C9
(ii) Choose the correct query to display the length of
ustomer's name for those customers whose name
end with R or L.
(a)mysq]°SELECT LEN(CNAME)FRON Custorers WHERE CNAME
LIKE "AR" 08 CUAME LIKE °31
(b)nysqQ>SELECT LENGTACCNIME)FROW Custemers WHERE
COUAME LIKE “SR OR CNAME LIKE “BL
(e)mysal>SELECT LENSTHCCNAME)FROM Custorers HERE
CAME = "AR" OR CHARE = 31
(@)mysql>SELECT LENGTH(CNAME FROM Customers MERE
(ONMME. LIKE “2R" OR LIKE “31
(iv) Choose the correct query to display the Customer's
name and their respective cities merged together
forall the customers whose CNO is ending with 8
(a)nysqleSELECT MERGE(CNAME, CITIES) Customers HHERE
NO LIKE “38
(b)mysqI°SELECT CMAME,CITIES FROM Customers WHERE
NO LIKE “8
(e)mysqtSELECT CONCAT( CHE
WHERE CHO = “8
(@) mys SELECT CONCATCCNAME
WHERE CD LIKE 18
(©) Choose the correct query to display the left most 4
letters from the customers who lives in Mumbai oF
Banglore.
(a)nysq)>SELECT LEFTCCNAMEDFROM Custoners
WHERE CITIES ="HUMBAI* OR CITIES
BaNSLORE
(b)mysqi>SELECT LEFTCENAME,3)FR0M Custoners
WHERE CITIES «'MOMGAL’ OR CITIES =
BANGLORE
tomers
CITIES) Customers
TES) FROM custoners‘CBSE Term II| Informatics Practices XII
(c) mysqi>SELECT LEFTCCNAME,4)FR
Custemers WHERE CITIES ~FUMBAI" OR
BANGLORE
(d)mysq1SELECT LEFT(CNANE.4)FROM Custoners
WHERE CITIES <"MUMBAT’ OR CITIES =
BANSLORE
‘Ans. (i (a) nysql>SELECT LENGTHCCNANE) FRC Customers
(i (o) mysql>SELECT UPPER(CITIES)FROW Custeners
HERE CNO ='C5" OR CAD ="CH
(o)nysqaSELEC™ LENGTH(CNME)FROM Customers
WHERE CNAME LIKE “3R" OR CNAWE LIKE “2.”
(i) (o)mysq >SELECT CONCATYCHAME, CITIES) FROM
Customers WHERE ONO LIKE “xa
(6) (dymysq) >SELECT. LEFTCCNAME, 4)
MHERE CITIES ="MMGAI’ 08 CITIES =
BANGLORE
Subjective Questions
* Short Answer Type Questions
1. Which type of MySQL function accepts only
numeric values? Give the name of some functions
of that type.
Mathematical fanetions accept only numeri values and
return the value af same type. These fmetons are used to
perform mathematical operations on the database dat,
Some mathematical fnetions are
POW(/POWER), ROUND), ete
2, Which SQL function is used to remove leading and
trailing spaces from a character expression X,
where X= "LEARNING ###MYSQL#### (#
denotes a blank space) and also give the output of
x.
‘Ans. TRIM funetion is wed to remove all Ieading and trailing
spaces from the given character expression.
Symtax TRIN (BOTH|LESDING|TRAILING) Crenstr) FROM]
strieolum rane)
Ans.
eg mysql> SELECT TRIM LEARNING
FRIMYSOLRRE)
‘Output LEARNINGHFRMYSOL
Spaces between LEARNING’ snd "MYSQL esanot be
removed
3. Write the output of following MySQL queries:
) SELECT ROUNDS. 5675.2)
(i) SELECT TRUNCATE(S. 2456.29
(i) SELECT DAYOFFONTH(curdate())
(iv) SELECT MIDC*PRE_POAROCLASS 12° 4.6)
(68) Heurdate is 05/122017, then output is 5
tt ‘.
4.63
IDC PRE_BOMRDCLASS 12
“pono
4, Mr: Manay, a database administrator in “Global
Educational and ‘Training Institute” has ereated
following table n " upeoming
training schedul
ing 1d Name Email_td
Tra ity Fee
NDOL Me ral@ginal com Cyber New 10000
Rajan Steusty Delhi
DMs. urv@yahoocom 1CTin — Gurasram 15000
Unashi Edwation
FDOL Ms. ncenaeificom Cyber Faridabad 12000
Neena Seourity
ND Mr. NULL IcTin New 15000
vin Eduction Delhi
Me navd?ynmaileon Cyber Gurageam NULL
Nave Security
Predict the output of the following queries:
(SELECT SUBSTR(City,2.4) FROM Training WHERE
Topic © "Cyber Securtty
(Gi) SELECT NAME FROM Training WHERE INSTR (Ena‘l_I.
2° )0
Ans. (i) + +
suasre(city.2.4) |
occurrence of a string ‘OUR’ in string ‘COURSE’?
Explain,
‘Ans. The INSTR function searches for given second string into
the fist string and returns the position
Syntax INSTR(sir?str2)
ce MYSCTPSELECT INSTR( "COURSE
‘The output wil be 2 because the por
atin the given sting ‘COURSE’
“OUR”
mn of string OUR is6. Predict the output of the flowing queries:
(ipmysaloSELECT Powe. 21
(ai) mysqTSELECT DATE 200
Ans. ()
01-21 02:01:08")
Powra (3.2)
7009-01-21
7. Write the output of the following queries
(ipmysaloSELECT POHERG-6,2)
(i) nysqToSELECT YEAR(* 2014-02-04")
Ans.) -
5 t
veak 2014-02-08")!
8, Write the output ofthe following queries:
(i)myseloSELECT LENSTAC Name")
) aysqIoSELECT MIO("tnformat ion’, 9.2)
9, Write the output ofthe following query:
mysql >SELECT SUBSTR("STUDENT' 3,3);
Ans. + a.
Fsuasrae'stupen’", 5,3)!
10. Consider the following table structure
STUDENT (RollNo, Nate, Class, Stream,
Percentage)
Give the answer of the following questions on the
basis ofthe above table structure
(Write a query to display the 3rd to 7th character
of name atribute
{i) Weite a query to display the name of each
student in uppercase letters with their RollNo,
whose percentage is greater than 70
CBSE Term | Informatics Practices Xil
‘Ans. (@) nysql>SELECT SUBSTR(Nane,2.5) FROM
STUDENT,
) mysql> SELECT UCASE (Name)
WHERE Percentage>70:
fRoTINo FRO STUDENT
11. What isthe differences between the string function
and numerie funetion ?
Ans. Differences between steing and numeric funetions are given
below:
Returns both numeric and
sting vals
String fnetions cide
ASCII), CHAR), LEFT)
ete
Numeric functions include
POW), ROUND (),
RUNCATE(), ee
12. Mention the type of the functions given below with
their purpose
() TRUNCATE()
(ii) DAYOFMONTH()
(i) LEFT()
‘Ans. (i) TRUNCATE() It is mathematical funetion and retuens|
number truncated upto speed numberof digs
(i) DAYORMONTH( ) Te is a DateTime function and
ns the day of month for the specified date
(i) LERT() Te isa string fametion and returns the leftmost
numberof characters as speciied
13. Give the output of following commands
(@)mysqQSELECT LEFT (Swat 4)
(Gi) mysqT>SELECT TRIM (*1111Study is
‘important !!111")
(iiymysqloSELECT ROWNDCI234.343.1
where, Il! denotes blank spaces.
‘Ans. (i) '
14, White the output ofthe following SQL queries:
(@SELECT SUBSTR(TRIMC’ INDIA Is Great,
3.6)
(a) SeLecr Rovncos4,67152) +
ROUND(IS2. 2186.2)
(fi) SELECT INSTR('MOBILE PHONE’. *E")
(i) SELECT omo-nONTAC 2018-11-22")CBSE Term II| Informatics Practices Xil
15. Witie any four differences between single-row
fimetions and multiple-row funetions. —_ [NCERT]
Ans. Single-row functions Multiple-row fanetions /
Agureatefanetions
TRoperates on angle row at Itoperates on multiple
Tt returns one result for
ultiple ows.
Ttan be wed in SELE Ita be used inthe
WHERE and ORDER BY clause, SELECT clause only
Mathematical, String and Date MAX), MIN) AVGO,
finetions are examples of SUMO, COUNT) anal
Single-row funetions COUNT(*) are examples
cof multiple-row functions
16, Write the name ofthe functions to perform the
following operations INCERT]
(i) To display the day like “Monday”, “Tuesday”,
from the date when India got independence,
(i) To display the specified number of characters
from a particular position of the given string,
(ii) To display the name of the month in which you
were born
(iv) To display your name in capital letters.
‘Ans. (i) DAYNAMB()
(4) MID() or SUBSTR() or SUBSTRING(
MONTHNAME(
(is) UPPER() or UCASE)
* Long Answer Type Questions
17. Write the output produced by the following SQL:
commands: (NCERT]
(SELECT POHC2.3;
(i) ELECT ROUNNCTZ9. 2345, 2), ROUND(3H2, 9234.1)
(i) SELECT LERGIHG*Tnformees Practices")
iv) SELECT YEAR("1979/11/26")
ORTH ("1979/13/26"), DAY("1979/12/26")
ONTHMAME ("1979/13/26")
(0) SELECT LerT¢"INora” 3)
Sclence’.4)
(vi) SELECT MIDC~InFormat es
SUBSTRCPract ices”.3)
Ans. (i) +
Iie"
3.4)
ws
ROUND 123.2345,2)
oun. 342.9234,~
NGTH( "Informatics Pract
(ie) 7
YEAR(*1979/11/26") ,MONTHC1979/11,
26 Novenber
() fone
LEFT(INDIA® 3) .RIGHT(*Computer
ND ence
18. Consider the following table Club
tale ch
: 7
5 Taian o7 Ts
i Tai Va
ve the ans oft llwing gusts on the
basis of the above table
() Write a query to display the substring of 4
characters of the name of each coach, starting
from second character, with their age
(Gi) What will be the output of the following query?
mnysql°SFLECT LENGTH(COACHNAYE) FROM Cub WHERE
E> 30,(Gi) Write a query to display the day for the
Date_of_Joining column.
(iv) What will be the output ofthe following query?
aysql SELECT PAPO. 25-1000
FROM Club
CRE: COACHNAME LIKE “RR”
() Write a query to display 3 characters from left of
coach name.
‘Ans. (i) mysql°SELECT SUBSTR (CORCINWE. 2.4)
cub:
(AGE FROM
FT Leneih cconcimane)
w
(4) mysqloSELECT DAY (Date _of_doining)
lw)
pavan,25+2000
(0) SELECT LEFT (COACHIUME,2) FROM Club;
19. Write SQL commands for (i) to (ii) and write the
‘output for (iv) on the basis of table Graduate.
Table + Graduate
No Subject Average Div
1 Physics 6 1
2 Computers 68 1
3 Chemisty 62 2
4 Physics 6 1
5 Mathematics 70 1
6 400 Chemistry 55 2
7 250 Physics 64 1
5 450 Mathematics 65 1
° 500 Computes 62 1
300 Mathematics 57 2
List the names ofthose students who obtained Div 1
(ii) Display a report, listing Name , Stipend , Subject
and amount of stipend received in a year assuming
that the Stipend is paid every month,
(ii) To insert a new row in the Graduate table
11,"KAJOU,300, “COMPUTERS” 75,1
(iv) Give the output ofthe following SQL statement
based on table Graduate:
(a) SELECT LEFT(NAME..3) FROM Graduate WHERE SNO>?
CBSE Term I!| Informatics Practices Xil
(D)SELECT Name, Stipend FROM Graduate WHERE
Subject="Chemistry" OR Subject="Physics"
(©) SELECT * FROM Graduate WHERE Subject LIKE "C8
MND ausragené8;
(4) SELECT Name FROM Graduate WHERE 01V2
‘Ans. (jj SELECT Nane FROM Graduate WERE Div
) SELECT Nane. Stipend, Subject, Stipend *12 FROM
Graauate;
(ii) INSERT THTD Graduate VALUES (12, "Kajol", 300,
‘Computers', 75.1)
Goa) +
PLeerowawe 3) |
ab
Men
@) perp t
Name | Stipend
Karan | a0
Divya
Jomn | 400
tengo
NO;Nane {Stipend Subject
2 iDivakar! 450 {Computers
20. Write SQL commands for (i) to(¥) and write the
output for (vi) on the basis of table Furniture,
Table: Furniture
Ne Type Dateoistock Price Discount
1 Double Bed 250202 3000025,
2 Baby Cot 200102 7000-20
3 Baby Got 1990202 850020
4 Office Table 01/012 2500030
5 Double Bed 12/01/02 2500025
6 Baby Cot_—240202 650015
7 Royal finish Offi Table 2010202 1800030
8 Royal tiger Sofa 2x22 $1000 30
8 Beono sitting Sofa 13201950025
10 Eating Dining Table 190202 1150025
parade
TI WoodComfort Double Bed 2905/08 2500025
BOF si 2on2i08 1700020
13 Micky Buby Got 240203750015CBSE Term II| Informatics Practices Xil
() ‘To show all information about the baby cots from
the Furniture table,
(ii) To list the itemname which are priced at more than
15000 from the Furniture table
(i) To list itemname and type of those items, in which
date of stock is before 22/01/02 from the Furniture
table in the descending order of itemname.
(iv) To display itemname and dataofstock of thse
items, whose type is ” Sofa” from Furniture table.
{¥) To insert a new row in the Furniture table with the
following, data
14;"Velvet touch”,
25000,30
(Gi) Give the output ofthe following SQL statement
based on table Furniture,
(@) SELECT LEFT(Itemane,3) FRY Furniture MHERE
Type="Double Bed"
(b) SELECT MoNTINANE (DateoFstock) FROM Furnit
WHERE Lype="Sofa
(©) SELECT * FROM Furniture WHERE Itemame Li
vb
Double Bed”,{25/03/03},
(€)SELECT PricesDiscount FROM Furniture WHERE
Dateofstock=31/12/02;
‘Ans. (@ SELECT * FROM Furniture WHERE Type= “Baby Cot’
(i SELECT —Ttemame FROM Furniture WHERE
Price>i5000)
SELECT Itemane, Type FROM Furniture WHERE
Dateofstock < 22/01/02" ORDER BY Ttennane
Dese:
(i) SELECT. Itemmane . Dateofstock FROM Furniture
HERE Type= ‘Sofa’
6) INSERT INTO Furniture VALUES (14
Velvet touch Double Bed
25/03/03", 25000, 30)
i) east
te anes ait
showing details of produets being sold in a grocery
shop.
‘Table : Product
POade ‘UPrice
POL Washing Powder 120
P02 Tooth Paste 4
P03 Soap 2
POL Tooth Paste 85 Pepsode
POS Soap 35 Dove
P06 Shampoo 35 Dove
Write SQL queries for the following and outputs)
produced by executing the following queries on the
basis of the inforn
Product
(i) Create the table Procluct with appropriate data
types and constraints
(ii) Identify the primary ke
(ii) List the product eode, product name and price in
«with their product name forall Dove manufacture,
(iv) Inerease the price by 12 per cent for all the
products manufactured by Dove
‘Ans. (i) CREATE TABLE Product(
Lode char(3) PRIMARY KEY
Name varchar(25) MOT NUL
UPrice intc4)
Manufacture varchar(30))
(i) PCode
(ii) SELECT PCode. Phane
Nanufacture="Dove
(iv) UPDATE Product SET UPrice “UPricetD.124Price
WHERE: aruacture="Dove"
jon given above in the table
in table Prod
UPrice FROM Product WHERE
22, Given the following table Employee
Table : Employee
No. Name Age Department Dateofrd Salary
1 Pankaj 54 10017 120M.
2 Shalini 41 24038 2000
3 Sanjay 32 zee 3500 M.
4 Sudha 25 ono7s 470
5 Rakesh 82 050997 2500 M
6 Shakeel 40 27/0695 3000 M
Suya 250297 210M
S_Shikha 83. Seience SuOTAT 2600