Professional Documents
Culture Documents
SQL Functions
LEVEL LEARNER
1
Question
s
Coding
Standards
Referenc
e
Test Your
Understanding
Icons Used
Lend A
Hand
Hands on Exercise Points To
Ponder
Summar
y
Overview
SQL Functions session !ro"ides
#no$%edge and understanding of
t&e use of functions a"ai%a'%e in
A(S) and *na%%y a!!%y t&e syntax
%earned as !art of t&is session in a
case study !ro"ided+
,
To understand t&e SQL functions conce!ts t&at a
de"e%o!er needs to #no$ to $or# $it& it+
Aggregate Function
0at&ematica% Function
(esting of Functions
Objective
1
For com!%ete understanding of A(S) SQL $e are
going to ma#e use of Product Management
System 2PMS3 for A4C Traders+
A4C Traders is a com!any $&ic& 'uys co%%ecta'%e
mode% cars- trains- truc#s- 'uses- trains and s&i!s
direct%y from manufacturers and se%%s t&em to
distri'utors across t&e g%o'e+ )n order to manage t&e
stoc#ing- su!!%y and !ayment transactions t&e
a'o"e soft$are is de"e%o!ed+
As !er t&e re5uirement of t&e trading com!any a
in"entory system is de"e%o!ed to co%%ect t&e
information of !roducts and customers and t&eir
!ayment !rocessing+
Scenario
6
Database tables
T&ere are many entities in"o%"ed in Product
Management System. PMS as gi"en 'e%o$ $&ic& $e
$i%% 'e dea%ing $it& t&roug&out t&is course
P
a
y
m
e
n
t
s
T
o
m
a
i
n
t
a
i
n
i
n
f
o
r
m
a
t
i
o
n
o
f
!
a
y
m
e
n
t
s
d
o
n
e
e
+
g
+
!
a
y
m
e
n
t
d
a
t
e
-
a
m
o
u
n
t
e
t
c
+
C
u
s
t
o
m
e
r
T
o
m
a
i
n
t
a
i
n
c
u
s
t
o
m
e
r
d
e
t
a
i
%
s
e
+
g
+
C
u
s
t
o
m
e
r
(
a
m
e
-
a
d
d
r
e
s
s
O
r
d
e
r
s
T
o
m
a
i
n
t
a
i
n
8
r
d
e
r
s
d
o
n
e
'
y
c
u
s
t
o
m
e
r
s
e
+
g
+
o
r
d
e
r
n
o
-
d
a
t
e
e
t
c
+
O
f
c
e
s
T
o
m
a
i
n
t
a
i
n
i
n
f
o
r
m
a
t
i
o
n
o
f
8
9
c
e
s
e
+
g
+
8
9
c
e
c
o
d
e
-
a
d
d
r
e
s
s
-
c
i
t
y
e
t
c
+
E
m
p
l
o
y
e
e
s
T
o
m
a
i
n
t
a
i
n
e
m
!
%
o
y
e
e
d
e
t
a
i
%
s
e
+
g
+
i
d
-
(
a
m
e
e
t
c
+
P
r
o
d
u
c
t
s
T
o
m
a
i
n
t
a
i
n
i
n
f
o
r
m
a
t
i
o
n
o
f
!
r
o
d
u
c
t
s
e
+
g
+
!
r
o
d
u
c
t
i
d
-
n
a
m
e
e
t
c
+
O
r
d
e
r
D
e
t
a
i
l
s
T
o
m
a
i
n
t
a
i
n
8
r
d
e
r
s
d
o
n
e
'
y
c
u
s
t
o
m
e
r
s
e
+
g
+
o
r
d
e
r
n
o
-
d
a
t
e
e
t
c
+
:
Schema diagram
;
Lets %earn a'out aggregate functions $&ic& $i%% &e%! us meet T)0<s
re5uirements++
Scenario
Hi=
>ood t&at t&e re5uirements %i#e
adding t$o co%umns are
im!%emented using o!erators-
remaining re5uirements %i#e
*nding t&e maximum amount
!aid 'y t&e customers s&ou%d
'e im!%emented+
?
Do you Know
@&at is FunctionA
?
B
What are Functions?
What are SQL Functons !
SQL functions are 'ui%t in AP)<s $&ic& SQL !ro"ides for
de"e%o!ers $&ic& can 'e used in SQL statements to
!erform s!eci*c %ogicCfunctiona%ity+
E"am#$e!
Lets %earn a'out date time functions $&ic& $i%% &e%! us meet T)0<s
re5uirements++
Scenario
Hi=
P%ease te%% me t&e
command to *nd t&e
current date+
,
Date ,ime Function
Funct
on
Name
'escr#ton E"am#$e Resu$t
/ATE Con"erts T)0ESTA0P or c&aracter string to
/ATE+
se$ect date2=??@?=3
(rom customersP
DDFD?F
D
A///AT
E
Adds inter"a% to date time "a%ue+ SELE%. A'''A.E2A=??@)?B)
?=Q- IN.ERVAL ,1 'A53
FR2M customersP
DD?FDF
D
/ATE/)F
F
Su'tract t$o dates 2Com!utes diGerence
'et$een t$o date time "a%ues+ 3
SELE%. 'A.E'IFF2A=??C)
B=)>B =>!DE!DEQ-A=??C)B=)
>?Q3
(rom customers4
1
T)0E Con"erts T)0ESTA0P or c&aracter string to
T)0E+
SELE%. tme2A=??@)?=)?>Q3
FR2M customersP
DDHDHD?
EKTRAC
T
A%%o$s t&e date !art to 'e extracted
2YEAR- 08(TH- /AY- H8UR- 0)(UTE-
SEC8(/- T)0ET8(ENH8UR- or
T)0ET8(EN0)(UTE3 from a tem!ora%
ex!ression+
SELE%. E6.RA%.+'A5
FR2M 'A.E2A=??E)?B)?BQ3 3
FR2M customers4
1
'e&ntonH /ateCTime functions o!erates on date-
timestam! data ty!e+
1
Date ,ime Function
Functon
Name
'escr#ton E"am#$e Resu$t
CURRE(TN/A
TE
Returns current date SELE%. %1RREN.*'A.E4 D1,FDF16
CURRE(TNT)
0E
Returns current time SELE%. %1RREN.*.IME4 DBH1DH61
CURRE(TNT)
0ESTA0P Returns current date and time
SELE%.
%1RREN.*.IMES.AMP4
D1,FDF16
DBH1H1D
/ate AdditionAdding days to a date SELE%. %1RREN.*'A.EFB?4 D1,D6
/ate
Su'traction
Su'tracting days from a date SELE%. %1RREN.*'A.E)B?4 D1,DD6
/ate
/iGerence
Pro"ides no of days 'et$een t$o dates
SELE%. %1RREN.*'A.E)
orderdate FR2M orders4
+%urrent date s =?B>)?=)BD
and orderdate s =?B>)?=)
?D04
1D
Fe9 more e"am#$es
6
+iscellaneous Functions
Msce$$aneous Functons
C8ALESCE23H
Synta" H
Add Course
To add t&e course detai%s into t&e course management
system+
Retrieve Course
Retrie"e t&e courses stored in t&e system and dis!%ay it+
T&e courses to 'e added $i%% &a"e t&e fo%%o$ing
attri'utes Course Code- Course (ame- (um'er of
!artici!ants- Course /escri!tion- Course /uration-
Course start date and Course Ty!e+
@e $i%% use t&e same C0S case study for %earning &o$ to use
o!erators in /QL and /0L statements
@e $i%% use t&e same C0S case study for %earning &o$ to use
o!erators in /QL and /0L statements
,6
Pre)reMuste ! Use t&e CourseN)nfo and
CourseNFees ta'%e+
/eterministic and
(ondeterministic functions
0isce%%aneous Functions
2C8ALESCE . (ULL)F3