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

Functions: Arranging the Data You Retrieve

Objectives

Today we talk about functions. Functions in SQL enable you to perform feats such as determining
the sum of a column or converting all the characters of a string to uppercase. By the end of the day,
you will understand and be able to use all the following:

● Aggregate functions

● Date and time functions

● Arithmetic functions

● Character functions

● Conversion functions

● Miscellaneous functions

These functions greatly increase your ability to manipulate the information you retrieved using the
basic functions of SQL that were described earlier this week. The first five aggregate functions,
COUNT, SUM, AVG, MAX, and MIN, are defined in the ANSI standard. Most implementations of
SQL have extensions to these aggregate functions, some of which are covered today. Some
implementations may use different names for these functions.

Aggregate Functions

These functions are also referred to as group functions. They return a value based on the values in a
column. (After all, you wouldn't ask for the average of a single field.)

The examples in this section use the table EMPLOYEESTATS:

INPUT:

SQL> SELECT * FROM EMPLOYEESTATS;

OUTPUT:

EmployeeId FirstName LastName LeavesTaken Leaves Pending Total Leaves


---------------- --------------- ------------- --------------- ------------------- ---------------
1 Harvinder Singh 10 15 25
2 Jagdev Singh 10 15 25
3 Navneet Kaur 12 13 25
4 Parmjit Kaur 13 12 25
5 Shahida Mustaffah 15 10 25
6 Reena Roy 15 10 25

6 rows selected.

45
COUNT

The function COUNT returns the number of rows that satisfy the condition in the WHERE
clause. Say you wanted to know how many ball players were hitting under 350. You
would type

INPUT/OUTPUT:

SQL> SELECT COUNT(*)


FROM EMPLOYEESTATS
WHERE Leaves Pending < 13

COUNT(*)
--------
3

To make the code more readable, try an alias:

INPUT/OUTPUT:

SQL> SELECT COUNT(*) Leaves Less than 13


FROM EMPLOYEESTATS
WHERE Leaves Pending < 13

Leaves Less than 13


-------------------------
4

SUM

SUM does just that. It returns the sum of all values in a column. To find out how many singles have

been hit, type

INPUT:

SQL> SELECT SUM(LeavesPending) as TOTALLeaves Pending for All Staff


FROM EMPLOYEESTATS

OUTPUT:

TOTAL Leaves Pending for All Staff


---------------------------------------------
75

46
To get several sums, use
INPUT/OUTPUT:

SQL> SELECT
SUM(LeavesPending) as Total Leaves Pending for All Staff
SUM(LeavesTaken) as Total Leaves Taken for All Staff
FROM EMPLOYEESTATS

Total Leaves Pending for All Staff Total Leaves Taken for All Staff
--------------------------------------------- ---------------------------------------------
75 75

SUM works only with numbers. If you try it on a non numerical field, you get ERROR

AVG

The AVG function computes the average of a column. To find the average number of
strike outs, use this:

To Average, type

INPUT/OUTPUT:

SQL> SELECT
Average (LeavesPending) as Total Leaves Pending for All Staff
FROM EMPLOYEESTATS

Total Leaves Pending for All Staff


--------------------------------------------
12

ANALYSIS:

The Sum function helps in calculating the total Sum of the selected Column and the Average
calculates the average of the Column i.e the sum of the Column divided by total rows

47
MAX
If you want to find the largest value in a column, use MAX. For example, what is the highest number
of hits?

INPUT:

SQL> SELECT FirstName,


MAX E(LeavesPending) as Max Leaves Taken
FROM EMPLOYEESTATS

OUTPUT:

First Name MAX Leaves Taken


---------------- ----------------------------
Shahida 15
Reena 15

MIN
MIN does the expected thing and works like MAX except it returns the lowest member of a
column. To find out the fewest at bats, type

INPUT:

SQL> SELECT FirstName,


MIN (LeavesPending) as Min Leaves Taken
FROM EMPLOYEESTATS

OUTPUT:

First Name MIN Leaves Taken


---------------- ----------------------------
Harvinder 10
Jagdev 10

NOTE: As we mentioned in the introduction, the first five aggregate functions are described in the
ANSI standard. The remaining aggregate functions have become de facto standards, present in all
important implementations of SQL.

48
VARIANCE

Variance produces the square of the standard deviation, a number vital to many statistical
calculations. It works like this:

STDDEV

The final group function, STDDEV, finds the standard deviation of a column of numbers, as
demonstrated by this example:

Date and Time Functions

We live in a civilization governed by times and dates, and most major implementations of
SQL have functions to cope with these concepts. This section uses the table PROJECT to
demonstrate the time and date functions.

INPUT:

SQL> SELECT * FROM PROJECT;

OUTPUT:

TASK STARTDATE ENDDATE


-------------- --------- ---------
KICKOFF MTG 01-APR-2010 01-APR-2010
TECH SURVEY 02-APR-2010 01-MAY-2010
USER MTGS 15-MAY-2010 30-MAY-2010
DESIGN WIDGET 01-JUN-2010 30-JUN-2010
CODE WIDGET 01-JUL-2010 02-SEP-2010
TESTING 03-SEP-2010 17-JAN-2010
6 rows selected.

NOTE: This table used the Date data type. Most implementations of SQL have a Date data type, but
the exact syntax may vary.

ADD_MONTHS

This function adds a number of months to a specified date. For example, say something extraordinary
happened, and the preceding project slipped to the right by two months. You could make a new
schedule by typing

49
INPUT:

SQL> SELECT TASK,


2 STARTDATE,
3 ENDDATE ORIGINAL_END,
4 ADD_MONTHS(ENDDATE,2)
5 FROM PROJECT;

OUTPUT:

TASK STARTDATE ORIGINAL_ ADD_MONTH


-------------- --------- --------- ---------
KICKOFF MTG 01-APR-2010 01-APR-2010 01-JUN-2010
TECH SURVEY 02-APR-2010 01-MAY-2010 01-JUL-2010
USER MTGS 15-MAY-2010 30-MAY-2010 30-JUL-2010
DESIGN WIDGET 01-JUN-2010 30-JUN-2010 31-AUG-2010
CODE WIDGET 01-JUL-2010 02-SEP-2010 02-NOV-2010
TESTING 03-SEP-2010 17-JAN-2011 17-MAR-2011

6 rows selected.

Not that a slip like this is possible, but it's nice to have a function that makes it so easy.
ADD_MONTHS also works outside the SELECT clause. Typing

INPUT:

SQL> SELECT TASK TASKS_SHORTER_THAN_ONE_MONTH


2 FROM PROJECT
3 WHERE ADD_MONTHS(STARTDATE,1) > ENDDATE
produces the following result:

OUTPUT:

TASKS_SHORTER_THAN_ONE_MONTH
----------------------------
KICKOFF MTG
TECH SURVEY
USER MTGS
DESIGN WIDGET

ANALYSIS:

You will find that all the functions in this section work in more than one place. However, ADD
MONTHS does not work with other data types like character or number without the help of
functions TO_CHAR and TO_DATE, which are discussed later today.

50
LAST_DAY

LAST_DAY returns the last day of a specified month. It is for those of us who haven't
mastered the "Thirty days has September..." rhyme--or at least those of us who have not
yet taught it to our computers. If, for example, you need to know what the last day of
the month is in the column ENDDATE, you would type

INPUT:

SQL> SELECT ENDDATE, LAST_DAY(ENDDATE)


2 FROM PROJECT;

Here's the result:

OUTPUT:

ENDDATE LAST_DAY(ENDDATE)
--------- -----------------
01-APR-2010 30-APR-2010
01-MAY-2010 31-MAY-2010
30-MAY-2010 31-MAY-2010
30-JUN-2010 30-JUN-2010
02-SEP-2010 30-SEP-2010
17-JAN-2011 31-JAN-2011

6 rows selected.

How does LAST DAY handle leap years?

INPUT/OUTPUT:

SQL> SELECT LAST_DAY('1-FEB-2010') NON_LEAP,


2 LAST_DAY('1-FEB-2011') LEAP
3 FROM PROJECT;

NON_LEAP LEAP
--------- ---------
28-FEB-2010 29-FEB-2011
28-FEB-2010 29-FEB-2011
28-FEB-2010 29-FEB-2011
28-FEB-2010 29-FEB-2011
28-FEB-2010 29-FEB-2011
28-FEB-2010 29-FEB-2011

6 rows selected.

51
ANALYSIS:

You got the right result, but why were so many rows returned? Because you didn't
specify an existing column or any conditions, the SQL engine applied the date functions
in the statement to each existing row. Let's get something less redundant by using the
following:

INPUT:

SQL> SELECT DISTINCT LAST_DAY('1-FEB-2010') NON_LEAP,


2 LAST_DAY('1-FEB-2011') LEAP
3 FROM PROJECT;

This statement uses the word DISTINCT (see Day 2, "Introduction to the Query: The
SELECT Statement") to produce the singular result

OUTPUT:

NON_LEAP LEAP
--------- ---------
28-FEB-2010 29-FEB-2011

Unlike me, this function knows which years are leap years. But before you trust your own or your
company's financial future to this or any other function, check your implementation!

MONTHS_BETWEEN

If you need to know how many months fall between month x and month y, use
MONTHS_BETWEEN like this:

INPUT:

SQL> SELECT TASK, STARTDATE,


ENDDATE,MONTHS_BETWEEN(STARTDATE,ENDDATE)
DURATION
2 FROM PROJECT;
OUTPUT:

TASK STARTDATE ENDDATE DURATION


-------------- --------- --------- ---------
KICKOFF MTG 01-APR-2010 01-APR-2010 0
TECH SURVEY 02-APR-2010 01-MAY-2010 -.201177419
USER MTGS 15-MAY-2010 30-MAY-2010 -.483871
DESIGN WIDGET 01-JUN-2010 30-JUN-2010 -.9354839

52
CODE WIDGET 01-JUL-2010 02-SEP-2010 -2.032258
TESTING 03-SEP-2010 17-JAN-2011 -4.451613

6 rows selected.

Wait a minute--that doesn't look right. Try this:

INPUT/OUTPUT:

SQL> SELECT TASK, STARTDATE, ENDDATE,


2 MONTHS_BETWEEN(ENDDATE,STARTDATE) DURATION
3 FROM PROJECT;

TASK STARTDATE ENDDATE DURATION


-------------- --------- --------- -----------------
KICKOFF MTG 01-APR-2010 01-APR-2010 0
TECH SURVEY 02-APR-2010 01-MAY-2010 .2011774194
USER MTGS 15-MAY-2010 30-MAY-2010 .48387097
DESIGN WIDGET 01-JUN-2010 30-JUN-2010 .93548387
CODE WIDGET 01-JUL-2010 02-SEP-2010 2.0322581
TESTING 03-SEP-2010 17-JAN-2011 4.4516129

6 rows selected.

ANALYSIS:

That's better. You see that MONTHS_BETWEEN is sensitive to the way you order the months.
Negative months might not be bad. For example, you could use a negative result to
determine whether one date happened before another. For example, the following
statement shows all the tasks that started before May 19, 192010:

INPUT:

SQL> SELECT *
FROM PROJECT
WHERE MONTHS_BETWEEN('19 MAY 2010', STARTDATE) > 0;

OUTPUT:

TASK STARTDATE ENDDATE


-------------- --------- ---------
KICKOFF MTG 01-APR-2010 01-APR-2010
TECH SURVEY 02-APR-2010 01-MAY-2010
USER MTGS 15-MAY-2010 30-MAY-2010

53
NEW_TIME

If you need to adjust the time according to the time zone you are in, the New_TIME
function is for you. Here are the time zones you can use with this function:

Abbreviation Time Zone


AST or ADT Atlantic standard or daylight time
BST or BDT Bering standard or daylight time
CST or CDT Central standard or daylight time
EST or EDT Eastern standard or daylight time
GMT Greenwich mean time
HST or HDT Alaska-Hawaii standard or daylight time
MST or MDT Mountain standard or daylight time
NST Newfoundland standard time
PST or PDT Pacific standard or daylight time
YST or YDT Yukon standard or daylight time

You can adjust your time like this:

INPUT:

SQL> SELECT ENDDATE EDT,


2 NEW_TIME(ENDDATE, 'EDT','PDT')
3 FROM PROJECT;

OUTPUT:

EDT NEW_TIME (ENDDATE)


---------------- - ---------------
01-APR-2010 1200AM 31-MAR-2010 0900PM
01-MAY-2010 1200AM 30-APR-2010 0900PM
30-MAY-2010 1200AM 29-MAY-2010 0900PM
30-JUN-2010 1200AM 29-JUN-2010 0900PM
02-SEP-2010 1200AM 01-SEP-2010 0900PM
17-JAN-2011 1200AM 16-JAN-2011 0900PM

6 rows selected.

Like magic, all the times are in the new time zone and the dates are adjusted.

54
NEXT_DAY

NEXT_DAY finds the name of the first day of the week that is equal to or later

than
another specified date. For example, to send a report on the Friday following the
first
day of each event, you would type

INPUT:

SQL> SELECT STARTDATE,


NEXT_DAY(STARTDATE, 'FRIDAY')
FROM PROJECT
Which would return

OUTPUT:
STARTDATE NEXT_DAY(
--------- ---------
01-APR-2010 07-APR-2010
02-APR-2010 07-APR-2010
15-MAY-2010 19-MAY-2010
01-JUN-2010 02-JUN-2010
01-JUL-2010 07-JUL-2010
03-SEP-2010 08-SEP-2010

6 rows selected.

ANALYSIS:

The output tells you the date of the first Friday that occurs after your

STARTDATE.

SYSDATE

SYSDATE returns the system time and date:


INPUT:

SQL> SELECT DISTINCT SYSDATE


FROM PROJECT;

55
OUTPUT:

SYSDATE
----------------
18-JUN-2010 1020PM

If you wanted to see where you stood today in a certain project, you could type

INPUT/OUTPUT:

SQL> SELECT *
2 FROM PROJECT
3 WHERE STARTDATE > SYSDATE;

TASK STARTDATE ENDDATE


-------------- --------- ---------
CODE WIDGET 01-JUL-2010 02-SEP-2010
TESTING 03-SEP-2010 17-JAN-2011

Now you can see what parts of the project start after today.

Arithmetic Functions

Many of the uses you have for the data you retrieve involve mathematics. Most
implementations of SQL provide arithmetic functions similar to the functions covered here.
The examples in this section use the NUMBERS table:
INPUT:
SQL> SELECT *
FROM NUMBERS;

OUTPUT:

A B
--------- ---------
3.1415 4
-45 .707
5 9
-57.667 42
15 55
-7.2 5.3

6 rows selected.

56
ABS

The ABS function returns the absolute value of the number you point to. For example:
INPUT:

SQL> SELECT ABS(A) ABSOLUTE_VALUE


FROM NUMBERS;

OUTPUT:
ABSOLUTE_VALUE
--------------
3.1415
45
5
57.667
15
7.2
6 rows selected.

ABS changes all the negative numbers to positive and leaves positive numbers alone.

CEIL and FLOOR

CEIL returns the smallest integer greater than or equal to its argument. FLOOR

does just the reverse, returning the largest integer equal to or less than its argument.

For example:

INPUT:

SQL> SELECT B, CEIL(B) CEILING


FROM NUMBERS;
OUTPUT:
B CEILING
--------- ---------
4 4
.707 1
9 9
42 42
55 55
5.3 6
6 rows selected.

57
And

INPUT/OUTPUT:

SQL> SELECT A, FLOOR(A) FLOOR


FROM NUMBERS;

A FLOOR
--------- ---------
3.1415 3
-45 -45
5 5
-57.667 -58
15 15
-7.2 -8

6 rows selected.

COS, COSH, SIN, SINH, TAN, and TANH

The COS, SIN, and TAN functions provide support for various trigonometric concepts. They
all work on the assumption that n is in radians. The following statement returns some
unexpected values if you don't realize COS expects A to be in radians.
INPUT:

SQL> SELECT A, COS(A) FROM NUMBERS;


OUTPUT:

A COS(A)
--------- ---------
3.1415 -1
-45 .52532199
5 .28366219
-57.667 .437183
15 -.752011879
-7.2 .60835131

ANALYSIS:

You would expect the COS of 45 degrees to be in the neighborhood of .707, not .525. To
make this function work the way you would expect it to in a degree-oriented world, you need
to convert degrees to radians. (When was the last time you heard a news broadcast report that
a politician had done a pi-radian turn? You hear about a 180-degree turn.) Because 360
degrees = 2 pi radians, you can write

58
INPUT/OUTPUT:

SQL> SELECT A, COS(A* 0.01745329251994)


FROM NUMBERS

A COS(A*0.01745329251994)
--------- -----------------------
3.1415 .99849724
-45 .70710678
5 .920111947
-57.667 .5348391
15 .2011592583
-7.2 .9921147

ANALYSIS:

Note that the number 0.01745329251994 is radians divided by degrees. The


trigonometric functions work as follows:

INPUT/OUTPUT:

SQL> SELECT A, COS(A*0.017453), COSH(A*0.017453)


FROM NUMBERS;

A COS(A*0.017453) COSH(A*0.017453)
--------- --------------- ----------------
3.1415 .99849729 1.0015035
-45 .70711609 1.3245977
5 .9201119483 1.00381
-57.667 .53485335 1.5507072
15 .201159262011 1.0344645
-7.2 .99211497 1.0079058

6 rows selected.
And

INPUT/OUTPUT:

SQL> SELECT A, SIN(A*0.017453), SINH(A*0.017453) FROM NUMBERS

59
A SIN(A*0.017453) SINH(A*0.017453)
--------- --------------- ----------------
3.1415 .05480113 .05485607
-45 -.7070975 -.8686535
5 .08715429 .0873758
-57.667 -.8449449 -1.185197
15 .25881481 .2647201069
-7.2 -.1253311 -.1259926

6 rows selected.
And

INPUT/OUTPUT:

SQL> SELECT A, TAN(A*0.017453), TANH(A*0.017453)


FROM NUMBERS;

A TAN(A*0.017453) TANH(A*0.017453)
--------- --------------- ----------------
3.1415 .05488361 .05477372
-45 -.9999737 -.6557867
5 .08748719 .08704416
-57.667 -1.579769 -.7642948
15 .26794449 .25597369
-7.2 -.1263272 -.1250043
6 rows selected.

EXP

EXP enables you to raise e (e is a mathematical constant used in various formulas)


to a power. Here's how EXP raises e by the values in column A:
INPUT:

SQL> SELECT A, EXP(A) FROM NUMBERS


OUTPUT:

A EXP(A)
--------- ---------
3.1415 23.138549
-45 2.863E-20
5 148.41316
-57.667 9.027E-26
15 3269017.4
-7.2 .00074659
6 rows selected.

60
SQRT
The function SQRT returns the square root of an argument. Because the square root
of a negative number is undefined, you cannot use SQRT on negative numbers.

INPUT/OUTPUT:

SQL> SELECT A, SQRT(A)FROM NUMBERS


However, you can fix this limitation with ABS:

INPUT/OUTPUT:

SQL> SELECT ABS(A), SQRT(ABS(A))


FROM NUMBERS;

ABS(A) SQRT(ABS(A))
--------- ------------
3.1415 1.7724277
45 6.7082039
5 2.236068
57.667 7.5938791
15 3.8729833
7.2 2.6832816
0 0
7 rows selected.

Character Functions

Many implementations of SQL provide functions to manipulate characters and strings of


characters. This section covers the most common character functions. The examples in this
section use the table CHARACTERS.

INPUT/OUTPUT:

SQL> SELECT * FROM CHARACTERS;

LASTNAME FIRSTNAME M CODE


--------------- --------------- --------- ----------
PURVIS KELLY A 32
TAYLOR CHUCK J 67
CHRISTINE LAURA C 65
ADAMS FESTER M 87
COSTALES ARMANDO A 77
KONG MAJOR G 52

6 rows selected.

61
CHR
CHR returns the character equivalent of the number it uses as an argument. The character it
returns depends on the character set of the database. For this example the database is set to
ASCII. The column CODE includes numbers.
INPUT:
SQL> SELECT CODE, CHR(CODE)
FROM CHARACTERS;

OUTPUT:
CODE CH
--------- --
32
67 C
65 A
87 W
77 M
52 4
6 rows selected.
The space opposite the 32 shows that 32 is a space in the ASCII character set.

LTRIM and RTRIM

LTRIM and RTRIM take at least one and at most two arguments. The first argument, like
LPAD and RPAD, is a character string. The optional second element is either a character or
character string or defaults to a blank. If you use a second argument that is not a blank, these
trim functions will trim that character the same way they trim the blanks in the following
examples.
INPUT:

SQL> SELECT LASTNAME, RTRIM(LASTNAME)


FROM CHARACTERS
OUTPUT:
LASTNAME RTRIM(LASTNAME)
--------------- ---------------
PURVIS PURVIS
TAYLOR TAYLOR
CHRISTINE CHRISTINE
ADAMS ADAMS
COSTALES COSTALES
KONG KONG

6 rows selected.

62
You can make sure that the characters have been trimmed with the following statement:

INPUT:

SQL> SELECT LASTNAME, RPAD(RTRIM(LASTNAME),20,'*')


FROM CHARACTERS

OUTPUT:

LASTNAME RPAD(RTRIM(LASTNAME)
--------------- --------------------
PURVIS PURVIS**************
TAYLOR TAYLOR**************
CHRISTINE CHRISTINE***********
ADAMS ADAMS***************
COSTALES COSTALES************
KONG KONG****************
6 rows selected.
The output proves that trim is working. Now try LTRIM:

INPUT:

SQL> SELECT LASTNAME, LTRIM(LASTNAME, 'C')


FROM CHARACTERS;

OUTPUT:

LASTNAME LTRIM(LASTNAME,
--------------- ---------------
PURVIS PURVIS
TAYLOR TAYLOR
CHRISTINE HRISTINE
ADAMS ADAMS
COSTALES OSTALES
KONG KONG

6 rows selected.

Note the missing Cs in the third and fifth rows.

63
REPLACE
REPLACE does just that. Of its three arguments, the first is the string to be searched. The
second is the search key. The last is the optional replacement string. If the third argument is
left out or NULL, each occurrence of the search key on the string to be searched is removed
and is not replaced with anything.

INPUT:
SQL> SELECT LASTNAME, REPLACE(LASTNAME, 'ST') REPLACEMENT
FROM CHARACTERS;
OUTPUT:

LASTNAME REPLACEMENT
--------------- ---------------
PURVIS PURVIS
TAYLOR TAYLOR
CHRISTINE CHRIINE
ADAMS ADAMS
COSTALES COALES
KONG KONG
6 rows selected.
If you have a third argument, it is substituted for each occurrence of the search key in the
target string. For example:

INPUT:

SQL> SELECT LASTNAME, REPLACE(LASTNAME, 'ST','**') REPLACEMENT


FROM CHARACTERS;

OUTPUT:

LASTNAME REPLACEMENT
--------------- ------------
PURVIS PURVIS
TAYLOR TAYLOR
CHRISTINE CHRI**INE
ADAMS ADAMS
COSTALES CO**ALES
KONG KONG

6 rows selected.

If the second argument is NULL, the target string is returned with no changes.

64
SUBSTR

This three-argument function enables you to take a piece out of a target string. The first
argument is the target string. The second argument is the position of the first character to be
output. The third argument is the number of characters to show.
INPUT:

SQL> SELECT FIRSTNAME, SUBSTR(FIRSTNAME,2,3)


FROM CHARACTERS;

OUTPUT:
FIRSTNAME SUB
--------------- ---
kelly ell
CHUCK HUC
LAURA AUR
FESTER EST
ARMANDO RMA
MAJOR AJO
6 rows selected.

Summary

We covered functions--from aggregates to conversions. You don't have to remember every


function--just knowing the general types (aggregate functions, date and time functions,

arithmetic functions, character functions, conversion functions, and miscellaneous functions)


is enough to point you in the right direction when you build a query that requires a function.

Q&A

Q Why are so few functions defined in the ANSI standard and so many defined by the

individual implementations?

A ANSI standards are broad strokes and are not meant to drive companies into bankruptcy
by forcing all implementations to have dozens of functions. On the other hand, when
company X adds a statistical package to its SQL and it sells well, you can bet company Y
and Z will follow suit.

Q I thought you said SQL was simple. Will I really use all of These functions?

65
AThe answer to this question is similar to the way a trigonometry teacher might respond to
the question, Will I ever need to know how to figure the area of an isosceles triangle in real
life? The answer, of course, depends on your profession.
The same concept applies with the functions and all the other options available with SQL.
How you use functions in SQL depends mostly on you company's needs. As long as you
understand how functions work as a whole, you can apply the same concepts to your own
queries.

Workshop
The Workshop provides quiz questions to help solidify your understanding of the material
covered, as well as exercises to provide you with experience in using what you have learned.
Try to answer the quiz and exercise questions before checking the answers in Appendix ,
"Answers to Quizzes and Exercises."

Quiz
1. Which function capitalizes the first letter of a character string and makes the rest
lowercase?

2. Which functions are also known by the name group functions?

3. Will this query work?

SQL> SELECT COUNT(LASTNAME) FROM CHARACTERS;


4. How about this one?

SQL> SELECT SUM(LASTNAME) FROM CHARACTERS;

5. Assuming that they are separate columns, which function(s) would splice together
FIRSTNAME and LASTNAME?
6. What does the answer 6 mean from the following SELECT?

INPUT:
SQL> SELECT COUNT(*) FROM EMPLOYEESTATS;

OUTPUT:
COUNT(*)

7. Will the following statement work?

SQL> SELECT SUBSTR LASTNAME,1,5 FROM NAME_TBL;

66

You might also like