Professional Documents
Culture Documents
cover
Student Notebook
ERC 4.1
Trademarks
IBM® is a registered trademark of International Business Machines Corporation.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX DB2 iSeries
OS/390 POWER QMF
WebSphere z/OS 400
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the
United States, other countries, or both.
Windows is a trademark of Microsoft Corporation in the United States, other countries, or
both.
UNIX® is a registered trademark of The Open Group in the United States and other
countries.
Linux® is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Other company, product, or service names may be trademarks or service marks of others.
The information contained in this document has not been submitted to any formal IBM test and is distributed on an “as is” basis without
any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer
responsibility and depends on the customer’s ability to evaluate and integrate them into the customer’s operational environment. While
each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will
result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk.
© Copyright International Business Machines Corporation 1999, 2007. All rights reserved.
This document may not be reproduced in whole or in part without the prior written permission of IBM.
Note to U.S. Government Users — Documentation related to restricted rights — Use, duplication or disclosure is subject to restrictions
set forth in GSA ADP Schedule Contract with IBM Corp.
V3.1.0.1
Student Notebook
TOC Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Course Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
TMK Trademarks
The reader should recognize that the following terms, which appear in the content of this
training document, are official trademarks of IBM or other companies:
IBM® is a registered trademark of International Business Machines Corporation.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX® DB2® iSeries™
OS/390® POWER™ QMF™
WebSphere® z/OS® 400®
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the
United States, other countries, or both.
Windows is a trademark of Microsoft Corporation in the United States, other countries, or
both.
UNIX® is a registered trademark of The Open Group in the United States and other
countries.
Linux® is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Other company, product, or service names may be trademarks or service marks of others.
Duration: 2 days
Purpose
This course provides an introduction to the SQL language and applies
to the entire DB2 Family.
This course is appropriate for customers working in all DB2
environments, that is, z/OS, VM/VSE, iSeries, Linux, UNIX, and
Windows.
Audience
Everyone needing to write, support, or understand SQL queries. This
includes but is not limited to end-users, programmers, application
designers, database administrators, and system administrators who
do not yet have knowledge of the SQL Data Manipulation Language
(DML).
Prerequisites
• Basic computer literacy
• Basic editing skills
• Database skills are not required
Objectives
On completion of this course, you should be able to:
• Code SQL statements to retrieve data from a DB2 table, including
the SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER
BY clauses
• Code inner Joins, including joining a table to itself
• Use SQL arithmetic operations
• Use scalar and column functions
• Use UNION and UNION ALL
• INSERT, UPDATE and DELETE rows
• Code simple CREATE TABLE and CREATE VIEW statements
Contents
• Introduction
• Simple SQL Queries
• Retrieving data from multiple tables
• Scalar functions and arithmetic
• Column functions and grouping
• UNION
• Using Subqueries
• Maintaining data
Uempty Agenda
Day 1
Welcome
Introduction
Simple SQL Queries
Exercise 1
Exercise Review
Retrieving Data from Multiple Tables
Exercise 2
Exercise Review
Scalar Functions and Arithmetic
Exercise 3
Day 2
Exercise review
Column Functions and Grouping
Exercise 4
Exercise Review
Union
Exercise 5
Exercise Review
Using Subqueries
Exercise 6
Exercise Review
Maintaining Data
Exercise 7
Exercise Review
Unit Objectives
After completing this unit, you should be able to:
Define the terms
– Database, table, row, column, view
Notes:
Departments
Projects
Employees
Notes:
In a traditional office,
• Employee data was filed in an 'EMPLOYEES' folder.
• Department data was filed in a 'DEPARTMENTS' folder.
• Project data was filed in a 'PROJECTS' folder.
People manually managed the data, updated the data, search through it, and retrieved it on
demand.
Uempty
EMPLOYEE DEPARTMENT
..
MID WORK DEPT
EMPNO FIRSTNME LASTNAME ... DEPTNAME .
INIT DEPT NO
000010 CHRISTINE I HAAS A00 A00 SPIFFY COMPUTER SERVICE DIV.
PROJECT ..
DEPT
PROJNO PROJNAME NO .
Notes:
A relational database is a collection of tables.
A table is a set of columns and rows. All data is stored in tables.
Every table has a name.
In a well-designed database, each table contains data about a specific subject (entity); for
instance, one table contains employee data, another table department data, and a third one
data about projects.
The data base management system manages the data.
If someone requires information about an employee, he/she must talk to the data
management system in a language which the system understands: SQL - Structured Query
Language.
Notes:
A query requires:
1. Someone who requests data (data requestor)
2. Someone who returns the data
3. A language understood by both sides
4. A medium for communication
5. A pool with the desired information
Uempty
SELECT * FROM
EMPLOYEE WHERE
WORKDEPT = 'D11'
ORDER BY LASTNAME
SE L E
CT S
tatem
ent
Notes
Human communication RDBMS
Data requestor End-user, programmer, designer
Human language Structured Query Language (SQL)
A medium for communication - phone, notes, The medium depends on the environment.
etc. The medium could be a program, a query
tool, etc.
Pool (folders) with the desired information Tables
Someone who fetches the information Integrated search and retrieval functions
Notes:
SQL has similarities with the human language. By means of SQL, we communicate with
the RDBMS. We tell DB2 UDB what we want, not how DB2 UDB should access the data.
SQL can be used either interactively or embedded in an application program, for example,
COBOL, PL/1, C or Java.
The object of a DML query is a set of tables. The result of the DML query is a set of
qualifying rows from these tables.
• DDL is mostly used by data base administrators to define DB2 UDB objects such as
tables, views and indices
• DML is used by all data base users to retrieve information or to maintain data
• DCL is also used by data base administrators to allow or disallow activities in DB2 UDB
by granting or revoking privileges
Most Relational Database Managers understand SQL.
Uempty
Table
USERA.EMPLOYEE
Notes:
• Every table has a name.
• Every column has a name.
• Column names must be unique within a table.
• A table consists of a set of rows and columns.
• Rows are normally stored unordered.
• For each row, a column has one entry (field).
• All values of a column have the same data type.
Notes:
• Every table must have a unique name.
• Every table name consists of at least two parts referred to as simple table name and its
qualifier (tables used in a distributed processing environment may have three part
names).
• The qualifier precedes the simple table name and is separated from it by a period.
• The simple table name must be unique within a qualifier.
Uempty
Notes:
The visual shows a few of the DB2 UDB supported numeric data types. For complete list of
supported data types in your environment, see the appropriate reference manual.
Notes:
The visual shows a few of the alphanumeric data types available for the various platforms.
For completeness, see the reference manual for your environment.
Alphanumeric data may contain any character or printable special character.
In queries, alphanumeric values must be enclosed in single quotes.
Uempty
Notes:
USA = US Standard
EUR = European Standard
ISO = International Standards Organization
JIS = Japanese Industrial Standard
Within an SQL statement, enclose dates in apostrophes.
Examples:
'2005-01-15'
'01/15/2005'
'15.01.2005'
Within an SQL statement, all date formats (USA, JIS, EUR, ISO) are accepted.
Notes
USA = US Standard
EUR = European Standard
ISO = International Standards Organization
JIS = Japanese Industrial Standard
Within an SQL statement, enclose times in apostrophes.
Examples:
'21.45.00'
'9:45 PM'
'21:45:00'
Uempty
Create Table
Notes:
If a table name qualifier is not explicitly supplied in the CREATE TABLE statement, the DB2
UDB environment determines the qualifier to be assigned (e.g. userid.tablename).
For each column, at a minimum, the following must be defined:
Column name
Data type (and length)
NULL characteristic
NULL Characteristic
Notes:
When a table is created, if a null characteristic is not provided the column is marked as
"nullable".
A column that is specified as NOT NULL must always contain a value for every row in the
table.
NOT NULL WITH DEFAULT columns are automatically supplied a value when the inserting
user or application does not supply its value.
Uempty
Authorization
DEPARTMENT
PROJECT
EMPLOYEE
Notes:
A table owner or DB2 UDB administrator can grant privileges to other users.
Example 1: Table owner USERX issues the following SQL GRANT and REVOKE
statement:
GRANT SELECT ON PROJECT TO SALLY
This statement gives SALLY permission to retrieve data (SELECT) from the table named
USERX.PROJECT.
REVOKE SELECT ON PROJECT FROM SALLY
This statement revokes SALLY's permission to retrieve data (SELECT) from the table
named USERX.PROJECT.
Example 2: USERX issues the following SQL GRANT:
GRANT SELECT, INSERT, UPDATE ON EMPLOYEE TO SECR1
This statement authorizes user SECR1 to retrieve data from (SELECT), add rows to
(INSERT), or change values of (UPDATE) table USERX.EMPLOYEE.
VIEW
000340
JASON R GOUNOT E21 5698
Notes:
Views provide alternative ways of looking at the data of one or more tables. A view can be
thought of as containing the data of selected columns and rows of the base table. For
retrieval, all views can be used as base tables.
Purpose:
• Improved data security.
• Improved data independence.
• Simplification for the end user
(less data, more user-friendly column names)
Properties:
• No additional data is stored.
• A view encompasses a subset of columns and rows.
• A view may encompass data from several tables and/or other views.
• A view may encompass derived data, for example, averages.
• Data manipulation via some views is limited.
Uempty
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
4.
Unit Summary
Having completed this unit, you should be able to:
Define the terms
– Database, table, row, column, view
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Objectives
After completing this unit, you should be able to:
Describe four clauses of an SQL SELECT statement
Use a SELECT statement to:
– Retrieve all rows of a table
– Retrieve specific columns
– Retrieve rows based on comparisons to numeric or alphanumeric
data
– Retrieve rows based on specific columns containing NULL values
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
EMPNO Employee number
FIRSTNME First name
MIDINIT Middle initial
LASTNAME Last name
WORKDEPT Department number
PHONENO Phone number
HIREDATE Hire date
Uempty
Notes:
EMPNO Employee number (repeated for readability purpose)
JOB Job
EDLEVEL Education level (Ed-level)
SEX Sex
BIRTHDATE Date of birth
SALARY Salary (yearly)
BONUS Bonus (yearly)
COMM Commission (yearly)
EMPNO is repeated on this page to facilitate row location. It is found only once within the
Employee table.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
2-6
Employee Table Data
M
Student Notebook
I
D
I
Uempty
DEPARTMENT Table
Notes:
DEPTNO Department number
DEPTNAME Department name
MGRNO Employee serial number of the responsible manager
ADMRDEPT Department number of the department to which this department reports
LOCATION Location
The dashes in the MGRNO and LOCATION columns indicate unknown values. The dashes
are not physically stored in the table, they are displayed by the report writer to represent
unknown values.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
PROJECT Table
DEPT RESP PR MAJ
PROJNO PROJNAME NO EMP STAFF PRSTDATE PRENDATE PROJ
------ --------------------- ---- ------ ----- ---------- ---------- ------
AD3100 ADMIN SERVICES D01 000010 6.50 1982-01-01 1983-02-01 -
AD3110 GENERAL ADMIN SYSTEMS D21 000070 6.00 1982-01-01 1983-02-01 AD3100
AD3111 PAYROLL PROGRAMMING D21 000230 2.00 1982-01-01 1983-02-01 AD3110
AD3112 PERSONNEL PROGRAMMING D21 000250 1.00 1982-01-01 1983-02-01 AD3110
AD3113 ACCOUNT PROGRAMMING D21 000270 2.00 1982-01-01 1983-02-01 AD3110
IF1000 QUERY SERVICES C01 000030 2.00 1982-01-01 1983-02-01 -
IF2000 USER EDUCATION C01 000030 1.00 1982-01-01 1983-02-01 -
MA2100 WELD LINE AUTOMATION D01 000010 12.00 1982-01-01 1983-02-01 -
MA2110 W L PROGRAMMING D11 000060 9.00 1982-01-01 1983-02-01 MA2100
MA2111 W L PROGRAM DESIGN D11 000220 2.00 1982-01-01 1982-12-01 MA2110
MA2112 W L ROBOT DESIGN D11 000150 3.00 1982-01-01 1982-12-01 MA2110
MA2113 W L PROD CONT PROGS D11 000160 3.00 1982-02-15 1982-12-01 MA2110
OP1000 OPERATION SUPPORT E01 000050 6.00 1982-01-01 1983-02-01 -
OP1010 OPERATION E11 000090 5.00 1982-01-01 1983-02-01 OP1000
OP2000 GEN SYSTEMS SERVICES E01 000050 5.00 1982-01-01 1983-02-01 -
OP2010 SYSTEMS SUPPORT E21 000100 4.00 1982-01-01 1983-02-01 OP2000
OP2011 SCP SYSTEMS SUPPORT E21 000320 1.00 1982-01-01 1983-02-01 OP2010
OP2012 APPLICATIONS SUPPORT E21 000330 1.00 1982-01-01 1983-02-01 OP2010
OP2013 DB/DC SUPPORT E21 000340 1.00 1982-01-01 1983-02-01 OP2010
PL2100 WELD LINE PLANNING B01 000020 1.00 1982-01-01 1982-09-15 MA2100
© Copyright IBM Corporation 2007
Notes:
PROJNO Project number
PROJNAME Project name
DEPTNO Responsible department
RESPEMP Employee number of the responsible employee
PRSTAFF Estimated mean staffing
PRSTDATE Estimated starting date
PRENDATE Estimated ending date
MAJPROJ Major project for a subproject
Uempty
Notes:
Clauses must be coded in the sequence indicated in the visual. Each SELECT statement
must have a SELECT and a FROM clause. The other clauses are optional.
The individual clauses will be discussed in detail later on in this course.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
The above statement displays all columns and all rows of the DEPARTMENT table.
The output of every SELECT statement is called the result table.
This is a simple statement useful for displaying all of the data in small tables. In practice,
we rarely need to see all of the data in a production table.
In a query, retrieve only the needed columns and rows.
Uempty
Notes:
The above statement displays all columns of the DEPARTMENT table but limits the result
table to the first x rows specified in the FETCH FIRST clause.
The FETCH FIRST clause, when used with an ORDER BY clause is useful in producing
'top 10' type reports. More on ORDER BY coming up.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
Items in the select list must be separated by commas. Spaces before and after commas
are optional.
Uempty
Notes:
• Resulting rows are sequenced according to columns listed in the ORDER BY clause.
• The keyword ASC means sort in ascending sequence. This is the default sort sequence
and the keyword ASC may be omitted.
• DESC means sort in descending sequence.
• Depending on the environment, columns used in the ORDER BY clause must be a part
of the result table.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
• If multiple columns are specified in the ORDER BY clause, their left to right sequence
within the ORDER BY determines their sorting priority. The first ORDER BY column
defines the primary order of the rows. Subsequent columns of the ORDER BY clause
refine the order of rows. The rows with the same first-column values are further ordered
by the values of the second column specified. The rows with identical first- and
second-column values are further ordered by the values of the third column, and so on.
• ASC or DESC may be specified for each column.
• Date AND time columns may also be sorted in ascending or descending order. Assume
you execute following statement:
SELECT EMPNO, BIRTHDATE FROM EMPLOYEE ORDER BY BIRTHDATE DESC
Then, the youngest employee (the one with the latest birth date) will be listed first.
• A selected column can be identified by the column name or by an integer, referencing
the columns position within the selection list.
Uempty
Notes.
Only one ORDER BY clause is allowed per query.
In queries with a SELECT, FROM, WHERE and ORDER BY clause the ORDER BY must
be coded as the last clause in the SELECT statement.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-15
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
• DISTINCT ensures that the result contains unique rows. DISTINCT removes duplicate
rows.
• The keyword DISTINCT must immediately follow the word SELECT. It is effective at the
row level.
Uempty
Notes:
• DISTINCT eliminates all rows that contain duplicate data in the set of columns specified
in the SELECT clause.
• DISTINCT refers to the result row.
• DISTINCT must not be used more than once within the scope of the SELECT
statement.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-17
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
DEPTNO ADMRDEPT
A00 A00
B01 A00
C01 A00
D01 A00
E01 A00
Notes:
To qualify specific table rows for further processing use the WHERE clause to specify the
conditions rows must meet to be part of the result table.
A WHERE clause can have one or more predicates, tests, or conditions.
A predicate in the WHERE clause specifies a condition that is true, false, or unknown for a
given row or group of rows. The values specified in a predicate must be compatible with the
data type of the column or expression to which it is compared.
For example, if you compare a column which is defined as alphanumeric, the value to
which it is to be compared must be enclosed in single quotes.
The comparison of alphanumeric columns is case sensitive.
The SELECT list need not include the columns used in the WHERE clause.
Uempty
LASTNAME EDLEVEL
KWAN 20
LUCCHESSI 19
Notes:
For numeric columns, for example, INTEGER, SMALLINT or DECIMAL, the values to be
compared must not be enclosed in single quotes.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-19
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Comparison Operators
SELECT *
FROM EMPLOYEE
WHERE SALARY = 20000 -- equal to
OR SALARY <> 20000 -- not equal to
OR SALARY > 20000 -- greater than
OR SALARY >= 20000 -- greater than or equal to
OR SALARY < 20000 -- less than
OR SALARY <= 20000 -- less than or equal to
Notes:
The symbols between the column named SALARY and the value 20000 are interpreted to
mean the words to the right of the predicate.
Within many query editors, two hyphens side by side (no space between them) represents
a comment marker. Text to the right of the comment marker is ignored.
Uempty
R R
Y
Y Y
Y Y
Yellow OR Oval Y
Y Y Y
R
R
Notes:
When predicates are combined via AND, for a row to qualify, both predicates must be true.
For predicates combined via OR, rows qualify if at least one predicate is true.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-21
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
When AND is used between predicates, for a row to qualify, both conditions must be met.
In the above example, only rows for analysts with an education level of 16 are displayed.
Uempty
Multiple Conditions - OR
Notes:
Rows qualify for further processing if at least one condition is met.
In the above example, employees who are analysts, and employees with an education
level of 20 (independent of their job) are displayed.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-23
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
If you combine multiple conditions by AND and OR, AND takes precedence over OR.
Uempty
Notes:
Search conditions within parentheses are evaluated first. If the order of evaluation is not
determined by parentheses, AND is applied before OR. The order in which operators at the
same precedence level are evaluated is undefined to allow for the optimization of search
conditions.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-25
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
SELECT with IN
LASTNAME EDLEVEL
JEFFERSON 14
LEE 14
O'CONNELL 14
SMITH 14
SPENSER 14
LUCCHESSI 19
KWAN 20
© Copyright IBM Corporation 2007
Notes:
The IN predicate compares a value with a set of values.
The values may be numeric, or alphanumeric, a date, or a time.
An alphanumeric value, a date, or a time must be enclosed within single quotes.
Example:
SELECT * FROM DEPARTMENT WHERE DEPTNO IN ('B01', 'C01', 'D01')
Uempty
EMPNO EDLEVEL
000290 12
000310 12
000100 14
000120 14
000230 14
000300 14
000330 14
000250 15
000270 15
© Copyright IBM Corporation 2007
Notes:
The BETWEEN predicate determines if a given value lies between and including two
values specified in ascending order (low value listed first, the higher or equal value must be
listed after the AND).
The clause
WHERE EDLEVEL BETWEEN 12 AND 15
is equivalent to
WHERE EDLEVEL >= 12 AND EDLEVEL <= 15
Alphanumeric values could also be used within the between predicate. Example:
WHERE WORKDEPT BETWEEN 'A00' AND 'D00'
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-27
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
The NULL predicate tests for null values.
The result of "IS NULL" cannot be unknown. If the value of the expression is null, the result
is true. If the value is not null, the result is false. If "IS NOT NULL" is specified, the result is
reversed.
Uempty
SELECT LASTNAME
FROM EMPLOYEE
WHERE LASTNAME LIKE 'G%'
LASTNAME
GEYER
GOUNOT
Notes:
• The LIKE predicate searches for character strings that have a certain pattern.
• In the pattern the percent sign ('%') serves as place holder for a string of zero or more
arbitrary characters.
The above SELECT searches for names beginning with the letter 'G'.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-29
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
The first example displays all names ending with 'SON'. and will only work as illustrated if
the data type of the LASTNAME column is VARCHAR.
The second example displays all names containing 'M' and 'N' in that sequence.
Names beginning with 'M' and ending with 'N' would also be displayed.
Uempty
SELECT LASTNAME
FROM EMPLOYEE
WHERE LASTNAME LIKE '_C%'
LASTNAME
SCOUTTEN
SCHNEIDER
© Copyright IBM Corporation 2007
Notes:
The underscore symbol ('_') in the pattern causes the character in the corresponding
position of the appropriate column to not be compared. Each underscore in the pattern
represents a single character whose value is not compared.
The above SELECT returns names which start with any arbitrary character if the second
character is the letter 'C'.
To search for last names whose third character is a 'C', the pattern would have to start with
two underscores.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-31
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Negation
DEPTNO DEPTNAME
A00 SPIFFY COMPUTER SERVICE DIV.
B01 PLANNING
C01 INFORMATION CENTER
E01 SUPPORT SERVICES
E11 OPERATIONS
E21 SOFTWARE SUPPORT
© Copyright IBM Corporation 2007
Notes:
If there are composite conditions, NOT is applied before AND and AND is applied before
OR.
All predicates can be negated by preceding the predicate with the word NOT.
WHERE NOT SALARY > 2000
Uempty
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
4.
5.
6.
© Copyright IBM Corp. 1999, 2007 Unit 2. Simple SQL Queries 2-33
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Summary
Having completed this unit, you should be able to:
Describe four clauses of an SQL SELECT statement
Use a SELECT statement to:
– Retrieve all rows of a table
– Retrieve specific columns
– Retrieve rows based on comparisons to numeric or alphanumeric
data
– Retrieve rows based on specific columns containing NULL values
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Objectives
After completing this unit, you should be able to:
Retrieve data from more than one table or view
Specify JOIN predicates
Use correlation names in queries
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
DEPARTMENT
Notes:
For each department number in table PROJECT, one row exists in table DEPARTMENT.
By looking at the data within the two tables we can see how a row in one table is related to
a row in the other table. The rows are related by the values in the DEPTNO columns.
Uempty
For every project, list the project number, project name, and the
number and name of the department responsible for the project.
Notes:
You can use the SELECT statement to produce reports that contain information from two or
more tables. This is commonly referred as a JOIN.
To join two tables, specify the columns you want to be displayed in the SELECT clause, the
table names in the FROM clause, and the join predicate in the WHERE clause.
Because DEPTNO is a column of tables DEPARTMENT and PROJECT, the column name
must be qualified. Furthermore, it is a good practice that all column names in a SELECT
referencing more than one table be fully qualified to avoid potential error.
When the join predicate is omitted each qualified row from the first table is combined with
all qualifying rows in the second table. This is called a Cartesian Product and is usually an
unwanted result.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Correlation Names
Notes:
Correlation names are defined in the FROM clause of any query. They can be used as
short-hand synonyms for the table names in that query.
Here we take advantage of the fact that we specify short correlation names and we follow
the recommendation to qualify all column names in our query.
Uempty
JOIN Syntax 1
Notes:
The tables EMPLOYEE and DEPARTMENT have common values for columns
WORKDEPT and DEPTNO. WORKDEPT contains the department number to which the
employee belongs. The join predicate tells DB2 UDB to combine the row for an employee
with the row for the department to which the employee belongs.
In most cases, when joining two tables we provide at least one JOIN condition. For three
tables, we provide at least two JOIN conditions. The general rule of thumb is:
The number of tables minus one (n-1) is USUALLY the LEAST number of join
predicates needed for the query, ensuring that there are no un-linked
tables.
Further conditions are allowed and may be added by means of AND or OR.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
When the JOIN keyword is used in the FROM clause, the join predicates must be specified
in an ON clause.
Row conditions (local predicates) must be written in a WHERE clause which must follow
the ON clause.
Uempty
...
Display the department name, and the employee number and last
name of the manager, for department D21.
DEPARTMENT
EMPLOYEE
EMPNO FIRSTNME MIDINIT LASTNAME ...
000010 CHRISTINE I HAAS
000020 MICHAEL L THOMPSON
000030 SALLY A KWAN
000050 JOHN B GEYER
000060 IRVING F STERN
000070 EVA D PULASKI
000090 EILEEN W HENDERSON
000100 THEODORE Q SPENSER
Notes:
The DEPARTMENT and EMPLOYEE tables can be joined another way: the MGRNO
column in the DEPARTMENT table corresponds to the employee number (EMPNO in the
EMPLOYEE table) of the manager of the department.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
This shows the JOIN answering the question on the previous page. Note that we could also
have used the second type of syntax:
SELECT DEPTNAME, MGRNO, LASTNAME
FROM DEPARTMENT JOIN EMPLOYEE
ON MGRNO = EMPNO
WHERE DEPTNO = ‘D21'
Uempty
DEPARTMENT
DEPTNO DEPTNAME MGRNO . . .
EMPLOYEE
EMPNO FIRSTNME MIDINIT LASTNAME ...
000010 CHRISTINE I HAAS
000020 MICHAEL L THOMPSON
000030 SALLY A KWAN
000050 JOHN B GEYER
000060 IRVING F STERN
000070 EVA D PULASKI
000090 EILEEN W HENDERSON
000100 THEODORE Q SPENSER
Notes:
Here we want to find out the project number for projects belonging to department D21, as
well as the department name, the employee number and last name of the department's
manager. This requires joining together the PROJECT, DEPARTMENT and EMPLOYEE
tables.
To find the name of the department that a project is assigned to, DB2 UDB can first read
the PROJECT table to obtain the department number of the department.
Next, read the corresponding row of the DEPARTMENT table to obtain the department
name and the employee number of the responsible manager.
Next, read the corresponding row of the EMPLOYEE table to obtain the manager's last
name.
The DB2 UDB optimizer determines the sequence of the above steps.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
With three tables being joined together, and the join conditions being on one column, we
need two join predicates (conditions)
If multiple tables are specified, but no JOIN predicate and no search condition is specified,
the intermediate result table consists of all possible combinations of the rows of the
specified tables (the Cartesian product); the number of rows in the result is the product of
the number of rows of all specified tables.
Uempty
DEPARTMENT
Notes:
In the DEPARTMENT table, the column ADMRDEPT contains the number of the
department (DEPTNO) that this department reports to; for example, department B01
reports to department A00 (the highest department, A00, reports to itself). ADMRDEPT and
DEPTNO are columns in the same table; therefore, the DEPARTMENT table can be joined
to itself.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
DEPARTMENT (SUP)
Notes:
To answer the question on the visual, we have to access the DEPARTMENT table twice:
once for the department we are interested in (B01) and once for the department it reports
to.
We will code this as if we had two copies of the table: one copy for the department we are
interested in – we name this copy DEP in the visual, and one for the higher department
(named SUP in the visual).
Of course, in reality, DB2 will NOT create a copy of the table; it will join each candidate row
of the table with other rows of the same table.
Uempty
DEPTNAME DEPTNAME
PLANNING SPIFFY COMPUTER SERVICE DIV.
Notes:
Table qualifiers (correlation names) are required because a table is referenced twice within
the FROM clause of the query.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-15
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
To get the required data, the EMPLOYEE table must be accessed twice.
Using the EMPLOYEE table, for each employee we read his/her department number.
Then, from the department table we find his/her manager's employee number. Using the
manager's employee number, we access the EMPLOYEE table again to retrieve the
manager's date of birth.
Then, the dates of birth are compared and for each employee that is older than his/her
manager information is put into the result table.
Uempty
Notes:
Once again, table qualifiers (correlation names) are required because a table is referenced
twice within the FROM clause of the query.
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-17
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
Uempty
Unit Summary
Having completed this unit, you should be able to:
Retrieve data from more than one table or view
Specify JOIN predicates
Use correlation names in queries
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 3. Retrieving Data from Multiple Tables 3-19
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Objectives
After completing this unit, you should be able to:
Use arithmetic in the SELECT and WHERE clauses
Use basic scalar functions such as COALESCE/VALUE,
DECIMAL, SUBSTR
Use date and time scalar functions
Use the CONCAT operator
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
• You can use the following arithmetic operators and parentheses() to calculate values:
addition +
subtraction -
multiplication *
division /
• Precedence of operations:
Expressions within parentheses are evaluated first. When the order of elevation is not
specified by parentheses, multiplication and division are applied before addition and
subtraction. Operators at the same precedence level are applied from left to right.
• In the result table, derived columns, such as SALARY+COMM, do not have meaningful
column names associated with them.
Uempty
Notes:
Derived columns ordinarily have no column names or have dummy column names
specified by the SQL tool used (SPUFI, QMF, Command Editor, and so on). You can use
the AS clause to rename the column headings for these derived column results.
Because the SELECT clause is executed after the WHERE clause the new column name
may only be used in the ORDER BY clause.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
SELECT DEPTNAME
, COALESCE (MGRNO, 'UNKNOWN')
AS MANAGER
FROM DEPARTMENT
ORDER BY DEPTNAME
DEPTNAME MANAGER
ADMINISTRATION SYSTEMS 000070
DEVELOPMENT CENTER UNKNOWN
INFORMATION CENTER 000030
MANUFACTURING SYSTEMS 000060
OPERATIONS 000090
PLANNING 000020
SOFTWARE SUPPORT 000100
SPIFFY COMPUTER SERVICE DIV. 000010
SUPPORT SERVICES 000050
© Copyright IBM Corporation 2007
Notes:
The COALESCE scalar function is equivalent to the VALUE scalar function.
• COALESCE allows multiple parameters:
COALESCE(expression_1, expression_2, expression_n, 'constant')
• The COALESCE function takes a variable number of parameters and returns the value
of the first parameter that is not null. The result is null if the values of all parameters are
null.
• In the example, the value "UNKNOWN" is returned if a department does not have a
manager, that is, MGRNO is NULL.
• Every expression must be of a compatible data type (if the first expression is numeric,
every expression must be numeric, and so on).
Uempty
Notes:
If one of the columns used in the expression contains a NULL value, the result of the
expression is unknown.
In the left example, when commission is NULL, SALARY + COMM is null. In the right
example, for SALARY + COMM, nulls are replaced with zeros.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Calculated Values
EMPNO SALARY
000210 18270.00 18955.125000
000250 19180.00 19899.250000
000260 17250.00 17896.875000
000290 15340.00 15915.250000
000300 17750.00 18415.625000
000310 15900.00 16496.250000
000320 19950.00 20698.125000
© Copyright IBM Corporation 2007
Notes:
The above example shows multiplication using a constant. You can also use calculations
with multiple columns (for example, COL1 * COL2).
Uempty
EMPNO SALARY
000210 18270.00 18955.12
000250 19180.00 19899.25
000260 17250.00 17896.87
000290 15340.00 15915.25
000300 17750.00 18415.62
000310 15900.00 16496.25
000320 19950.00 20698.12
© Copyright IBM Corporation 2007
Notes:
The DECIMAL scalar function returns a decimal representation of a numeric value or a
decimal representation of a numeric looking character value. In the example above, the
DECIMAL scalar function is used to ensure that the result column has 2 decimal positions.
The result is truncated after the second decimal position. No rounding occurs with the
DECIMAL scalar function.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
EMPNO SALARY
000210 18270.00 18955.13
000250 19180.00 19899.25
000260 17250.00 17896.88
000290 15340.00 15915.25
000300 17750.00 18415.63
000310 15900.00 16496.25
000320 19950.00 20698.13
© Copyright IBM Corporation 2007
Notes:
If you need a rounded result, add 5 to the first decimal position to be truncated and apply
the DECIMAL function to this expression.
Most environments support a ROUND scalar function as another method of rounding. See
the SQL Reference manual for your environment for additional information.
Uempty
Notes:
Arithmetic expressions can be used in the SELECT list and in the WHERE clause.
The above SELECT returns rows for employees with a commission higher than 8 percent
of their salary.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
The visual shows the internal and external formats for DATE/TIME data.
A timestamp is a seven-part value (year, month, day, hour, minute, second, and
microsecond) that represents a date and a time including microseconds.
Uempty
CHAR Function
03:30 PM 15.30.00
Notes:
The visual shows the CHAR function to convert a DATE or TIME to a chosen external
character format.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
Columns defined with a DATE or TIME data type can be compared with other DATE or
TIME columns or with a valid date or time.
Date or time constants (character string representations of a valid date or time) must be
placed in single quotes. All legal formats can be used.
In the shown example, all employees are selected that were born '1955-01-01' or later.
Uempty
Notes:
A date duration represents the period between two dates. The result is presented in format
yyyymmdd.
A time duration represents the period between two times. The result is presented in format
hhmmss.
A timestamp duration represents the period between two timestamps. The result is
presented in format yyyymmddhhmmss.nnnnnn (the nnnnnn portion represents
microseconds).
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-15
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Subtraction of Dates
Notes:
In above example, the values of two date columns are subtracted. The result of the
subtraction is presented in the format yyyymmdd. Leading zeros are not displayed.
For example, in the first result line, the value 790727 means 79 years, 7 months, and 27
days. (The results are for CURRENT_DATE of 2005-05-12).
In the WHERE clause, the calculated durations are compared with 720000, that is, 72
years, 00 months, and 00 days (72 years old or more).
CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP (or CURRENT DATE,
CURRENT TIME and CURRENT TIMESTAMP) are special registers provided by DB2
UDB, containing the system date, system time, or system timestamp respectively. The
format with the underscore is more standard, but older versions of DB2 on some platforms
do not support this format.
Uempty
Notes:
The visual shows some scalar functions for date and time values.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-17
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
In the above example, year, month, and day are displayed separately using appropriate
scalar functions.
The resulting columns normally do not have column headings. The AS clause is used to
name the columns.
Uempty
DATE Arithmetic (1 of 2)
SELECT PROJNO,
DAYS(PRENDATE) - DAYS(PRSTDATE) AS DAYS
FROM PROJECT
WHERE DAYS(PRENDATE) - DAYS(PRSTDATE) <= 300
ORDER BY DAYS
PROJNO DAYS
PL2100 257
MA2113 289
Notes:
The example shows the duration of projects in days. To obtain a positive result, the project
start date (PRSTDATE) must be subtracted from the project end date (PRENDATE)
because the end date is higher.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-19
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
DATE Arithmetic (2 of 2)
PROJNO PRENDATE
Notes:
The example shows the use of labeled durations and adds 2 months and 15 days to a date.
Uempty
Substring of Strings (1 of 2)
COURSINF
C F1 2 SQL B AS I CS
C F1 3 SQL A DV A NCED
SQL B AS I CS
SQL A DV A NCED
Notes:
If only a part of a character string is to be selected, use the SUBSTR scalar function to
extract the part.
Assume that COURSINF is defined CHAR(20) and that both course code and course title
are stored in that column, separated by a blank. To only display the course title, you can
use the SUBSTR scalar function.
SUBSTR(COURSINF,6,15) returns a substring of length 15 beginning at position 6, that is,
the course title in the above example.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-21
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Substring of Strings (2 of 2)
PROJ_CLASS PROJNAME
IF QUERY SERVICES
IF USER EDUCATION
Notes:
The first two characters of the project number represents the project class. To display only
the project class, the SUBSTR function is used.
SUBSTR(PROJNO,1,2) extracts the first two characters. The function can be interpreted to
read: Within the PROJNO column, start with the first character and return two characters.
Uempty
Concatenation of Values
NAME
HAAS, CHRISTINE
LUCCHESSI, VINCENZO
O'CONNELL, SEAN
Notes:
The above example displays last name and first name as a single column, separated by a
comma and a space.
CONCAT combines character strings into a single column. The word CONCAT works in all
environments.
Double bars (||) can be used to represent concatenation, but you must make sure that the
vertical bar symbol on your keyboard will actually result in a vertical bar on the host.
Example:
SELECT LASTNAME || ',' || FIRSTNME FROM EMPLOYEE
Check your environment's SQL documentation to see what additional concatenation
symbols are supported.
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-23
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
Uempty
Unit Summary
Having completed this unit, you should be able to:
Use arithmetic in the SELECT and WHERE clauses
Use basic scalar functions such as COALESCE/VALUE,
DECIMAL, SUBSTR
Use date and time scalar functions
Use the CONCAT operator
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 4. Scalar Functions and Arithmetic 4-25
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Objectives
After completing this unit, you should be able to:
Describe the difference between scalar and column functions
List the more common DB2 column functions
Group rows into sets based on one or more columns
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
SQL Functions
Function_name(f_argument)
Column Scalar
Functions Functions
AVG(SALARY) YEAR(HIREDATE)
________ __________ _____
________
________
________
________
}
______
__________ _____
__________ _____
__________ _____
__________ _____
Notes:
There are two types of SQL functions:
• Column functions (also known as summary functions)
• Scalar functions
The format for writing both types of functions is the same, the name of the function followed
by parentheses. Inside the parentheses is the argument to which the function is applied.
Column functions produce a summary row for a set of rows.
Scalar functions produce a value for each row within the set.
Uempty
Column Functions
Notes:
For SUM, AVG, MIN, MAX, COUNT(*), COUNT_BIG(*), COUNT(DISTINCT...),
COUNT_BIG(DISTINCT…), the following applies:
The basis for these column functions are values derived from one or more rows.
The data type of the input values for SUM and AVG must be numeric.
Example for COUNT(*):
SELECT COUNT(*) FROM EMPLOYEE WHERE WORKDEPT = 'A00'
returns the number of rows having the value 'A00' in the WORKDEPT column. In general,
the argument for COUNT(*) or COUNT_BIG(*) is a set of rows. The result is the number of
rows in the set.
COUNT returns an INTEGER value (up to 2,147,483,647); if a larger result is possible
(there are tables with billions of rows), then we can use COUNT_BIG which returns a value
in DECIMAL(31,0) format.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Uempty
Notes:
The set of rows to which these column functions are applied contains all rows of the
employee table.
The result does not display detailed information for individual rows.
Precision and scale of the resulting values are derived from the definition for the column to
which the column function applies.
Except for COUNT(*) and COUNT_BIG(*), expressions (for example, SALARY + COMM)
may be used as an argument for column functions.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Beware of Nulls
SUM(SALARY)+SUM(BONUS)+SUM(COMM)
VERSUS
SUM(SALARY+BONUS+COMM)
Notes:
If a null is part of row level arithmetic the result is null.
Nulls are ignored in column level arithmetic.
Consider using the COALESCE/VALUE scalar function to replace nulls with known values.
Uempty
SUM
373020.00
© Copyright IBM Corporation 2007
Notes:
The result is derived from the set of rows for which the WHERE clause predicate is true.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
GROUP BY
WORKDEPT SALARY
A00 52750.00
A00 46500.00 WORKDEPT SUM
A00 29250.00 A00 128500.00
B01 41250.00 B01 41250.00
C01 38250.00 C01 90470.00
C01 23800.00
C01 28420.00
© Copyright IBM Corporation 2007
Notes:
With GROUP BY, a column function results in a single value for each group. A group is
made up of all the candidate rows (rows satisfying the WHERE clause) having the same
value for all the columns in the GROUP BY (in this example, having the same value for
WORKDEPT).
A name defined by an AS clause cannot be used in GROUP BY.
GROUP BY does not guarantee a sorted result table. If an ordered result table is
required an appropriate ORDER BY clause must be specified.
Uempty
Find out the average salary per education level for each department group (given by
the first character of the department number), for education levels 18 and higher
Notes:
Here we are grouping by the first character of the department number and education level.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
ALL
Notes:
Column functions are:
• MIN
• MAX
• SUM
• AVG
• COUNT(*)
• COUNT_BIG(*)
• COUNT(DISTINCT column_name)
• COUNT_BIG(DISTINCT column_name)
The reverse of the rule is not true, that is, columns listed in the GROUP BY clause don't
have to have been selected.
Uempty
Notes:
The requirement is, from the PROJECT table, produce a list that includes MAJPROJ,
DEPTNO, the average number of staff assigned and the number of projects that are part of
the major project. Our hard to remember rule tells us that when we have a column function
in the select list we must GROUP BY all columns in the select list that are not arguments of
a column function.
Looking at the fourth line in the report, it can be interpreted to read: Department E21 has 3
subprojects to major project OP2010 and their average staffing is 1.
Note that nulls within the scope of the grouping are combined: the first report line can be
interpreted to read: Department C01 has two projects that have an unknown major project.
Note also that nulls are sorted as high values (ORDER BY MAJPROJ DESC).
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
WORKDEPT SUM
A00 128500.00 WORKDEPT SUM
A00 128500.00
B01 41250.00
C01 90470.00
C01 90470.00
© Copyright IBM Corporation 2007
Notes:
So far we built groups. With HAVING, we now can select groups satisfying certain
conditions. Conditions for groups must be specified in the having clause.
HAVING may specify any column function on any column in a table being queried. This
column need not be in the SELECT list.
HAVING is used to qualify or disqualify a group from further processing.
Uempty
Notes:
In the above example, we see another example of a GROUP BY clause with two columns,
and another example of HAVING.
The first three rows in the result have the same value ('A00') in the WORKDEPT column,
but different values in the JOB column. The results of a GROUP BY clause is one result
row for each unique set of values across the columns being grouped.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-15
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Notes:
The left SELECT is displayed to show the result without a HAVING clause.
The right SELECT determines the result table consisting of groups for which the search
condition of the HAVING clause is true.
The condition COUNT(*) > 1 applies to the result table of the left SELECT
Uempty
Restrictions
Column functions may be specified only in
– SELECT
– HAVING
Notes:
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-17
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
FROM EMPL
ORDER BY 3 DESC
Notes:
The SELECT clause tells DB2 UDB which column(s) of information we want on our report.
The SELECT list contains a column function (also known as a summary function) AVG.
Column functions tell DB2 UDB to produce a single summary row for a set of detail rows.
The FROM clause tells DB2 UDB which table(s) contain the requested data.
The WHERE clause specifies which row(s) of information are to be used based upon some
qualification criteria.
The GROUP BY clause tells DB2 UDB which row(s) of information are to be grouped
together (that is, summarized) based upon values in a column(s).
The HAVING clause tells DB2 UDB which group(s) of information are to be processed
based upon a group qualification criteria.
The ORDER BY clause specifies sequence of the result rows in the final result table.
Uempty
Notes:
The clauses are executed in this sequence:
• FROM
• WHERE
• GROUP BY
Through the FROM clause DB2 UDB locates the table.
The WHERE clause extracts into the conceptual intermediate table rows that will be
processed further.
The GROUP BY clause sorts the rows in the intermediate table into subsets.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-19
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
GRE S 34000
S 31000 BLU BLU S 30000 RED S 30500
S 29000 BLU BLU S 30000
S 30000 BLU
Notes:
Only those groups passing the HAVING clause test are processed further.
Uempty
Checkpoint
1. True or False? A scalar function produces a summary row
for a set of rows.
2. A SELECT statement whose SELECT list includes a
column function (SUM, AVG, MIN, MAX, COUNT, and so
forth) and three columns not in column functions does not
require a GROUP BY clause.
3. Which clause qualifies groups for further processing?
a. SELECT
b. FROM
c. WHERE
d. GROUP BY
e. HAVING
f. ORDER BY
4. True or False? The following query is syntactically correct.
SELECT WORKDEPT, AVG(SALARY)
FROM EMPLOYEE
WHERE AVG(SALARY) > 20000
GROUP BY WORKDEPT
HAVING COUNT(*) > 3
ORDER BY 2 DESC
© Copyright IBM Corporation 2007
Notes:
Please write down your answers below:
1.
2.
3.
4.
© Copyright IBM Corp. 1999, 2007 Unit 5. Column Functions and Grouping 5-21
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Student Notebook
Unit Summary
Having completed this unit, you should be able to:
Describe the difference between scalar and column functions
List the more common DB2 column functions
Group rows into sets based on one or more columns
Notes:
Unit Objectives
After completing this unit, you should be able to:
Produce a single result table containing data from more than
one query
State the UNION rules
State the difference between UNION and UNION ALL
Notes:
Notes:
Each time a query is executed a single report is produced.
Sometimes we have a requirement for data returned by one query to appear in a report
with data from additional queries.
Uempty
-- QUERY 1
SELECT some_columns DATA FROM QUERY 1
FROM some_table(s)
WHERE some_conditions
UNION ALL
DATA FROM QUERY 2
-- QUERY 2
SELECT some_columns
FROM some_table(s)
WHERE some_conditions
Notes:
By following the UNION RULES, and adding a UNION ALL clause between the queries the
data from both queries appears in a single report.
UNION ALL
Notes:
If the first query in the stack returns x number of columns every query in the stack must
return x number of columns.
Uempty
UNION ALL
Notes:
If the nth column in the first query returns character data then the corresponding nth
column in every query of the stack must return character data.
Column lengths do not have to match.
Numeric precision and scale do not have to match (integer and DEC(7,2) data can appear
in the same column). The report column will carry the largest precision and scale of all
numbers in the column.
UNION ALL
Notes:
Only one ORDER BY clause allowed on a UNIONed query.
Uempty
Notes:
The above example is one of several ways of translating coded data into more human
terms (the value of M replaced with the word MALE, the value of F replaced with the word
FEMALE.
MGRNO NAME
000010 Mgr.: HAAS
000010 Dept.: SPIFFY COMPUTER SERVICE DIV.
000020 Mgr.: THOMPSON
000020 Dept.: PLANNING
000030 Mgr.: KWAN
000030 Dept.: INFORMATION CENTER
000050 Mgr.: GEYER
000050 Dept.: SUPPORT SERVICES
© Copyright IBM Corporation 2007
Notes:
The first SELECT establishes the column headings of the result.
If a column you wish to sort by has the same name in each query in the stack, then the
column name can be used in the ORDER BY clause. Otherwise, the column's position
number must be used.
Uempty
Notes:
UNION ALL does not eliminate duplicates.
Use UNION ALL if the queries cannot return duplicate rows.
UNION
Notes:
UNION combines two sets of rows and removes duplicates.
Therefore, UNION ALL often performs better than UNION.
The problem to list last name and education level of employees who are analysts or have
an education level of 18 can be solved by means of a UNION performing the following
three steps:
1. Each SELECT is evaluated separately
2. The results are merged and ordered
3. Duplicates are removed
Uempty
Notes:
Any type of SELECT statement can be used as long as the rules for UNION are adhered
to. All column and scalar functions can be used.
In the above example, a substring of first name concatenated with the MIDINIT and the last
name are selected in the first SELECT. To get the equivalent number of columns in the
other SELECT statements, blanks (compatible to the data types of the concatenated value
and the last name value) are specified as corresponding column values. Numeric literals,
current date or current time can be 'place holders' at the appropriate spots in UNIONed
queries.
EMPNO must be selected to order the result table.
To get a four row result for each employee in the desired order
(1st salary, 2nd bonus, 3rd commission, 4th sum), we need an additional sort column.
The double exclamation points (!!), and double bars (||) (in environments where they are
supported), are equivalent to the CONCAT operator (which is supported in all
environments). Check the manuals for your environment to determine its supported
operator concatenation operators.
Uempty
Notes:
Here are two other sets of words that can be used in place of UNION ALL and UNION.
They are also used for merging data, but work differently.
EXCEPT displays what is in the first result set but not in the second result set. In this case,
it displays ANALYSTS who are not edlevel 18. This would show duplicates if there were
any; EXCEPT ALL would not.
INTERSECT displays only what is in both the first result set and in the second result set. In
this case, it displays ONLY ANALYSTS who are ALSO edlevel 18. This would show
duplicates if there were any; INTERSECT ALL would not.
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
Uempty
Unit Summary
Having completed this unit, you should be able to:
Produce a single result table containing data from more than
one query
State the UNION rules
State the difference between UNION and UNION ALL
Notes:
Unit Objectives
After completing this unit, you should be able to:
Use subqueries in WHERE and HAVING clauses
Code subqueries using basic predicates
Code subqueries using IN
Notes:
It is often useful to imbed a query within another. This enables the inner query to provide an
answer to a question for use in the outer query. The inner query is referred to as a
subquery.
27303
Notes:
Assume that you want to identify all employees whose salary is higher than the average
salary of all employees.
Without subqueries, you would have to do the following:
• Evaluate the average salary of all employees (first select)
• Code a second query using the result (27303) in the WHERE clause.
• Run the second query to find the employees whose salary is higher than the average
salary (27303).
Uempty
FROM EMPLOYEE
FROM EMPLOYEE)
Notes:
The problem, described on the previous visual can be solved by means of a single
execution of a query containing a subquery.
We can code the first SELECT of the previous visual in the WHERE condition of the
second SELECT of the previous visual at the place where the average is needed.
As a consequence, the second SELECT is executed by the outer(main) query. The inner
SELECT is referred to as the subquery.
The subquery runs first, and the result is used to evaluate the WHERE condition of the
main query.
A subquery cannot contain an ORDER BY.
The results of the subquery are passed to the outer query and are not visible to the user.
Notes:
In this example, we look for the employee(s) who earned the lowest bonus.
The subquery will determine the lowest bonus and it will be compared with the bonuses of
all employees. If the bonus of an employee matches the value provided by the subquery,
the result of the predicate is true and the row is returned.
A basic predicate is one that uses single value comparison operator (=, <, >, or any
combination thereof). A subquery in a basic predicate must not return more than one value.
Uempty
Subquery Using IN
SELECT FIRSTNME, LASTNAME, EMPNO
FROM EMPLOYEE
List the names and employee
numbers of employees who are WHERE EMPNO IN (SELECT MGRNO
FROM DEPARTMENT)
managers of a department
Final result Result of subquery
Notes:
IN and NOT IN can be used to compare with a list of values. IN means that for the condition
to evaluate TRUE, the value in the first operand (EMPNO in the example) must be equal to
at least one of the values in the list returned by the subquery.
Variations on Predicates
Notes:
In later releases of DB2 UDB it is possible to use the syntax illustrated in this WHERE
clause. A list of items enclosed in parenthesis can be compared with a list of values, also
enclosed in parenthesis, on the right hand side of the comparison operator. Both lists must
contain the same number of items and the items must be data type compatible. The
comparison operator must be equal (=) or not equal (<>).
DB2 UDB will rewrite the WHERE clause to the equivalent of:
WHERE WORKDEPT = 'D21' AND JOB = 'CLERK'
Check the reference manuals for your environment to determine if the syntax is supported.
Uempty
Subquery Using IN
Notes:
Later releases of DB2 UDB support the above syntax with a list of items enclosed in
parenthesis compared with a multiple value comparison operator to a subquery returning
zero, one or more rows with the same number of items as in the list on the left. The
comparison operator must be IN or NOT IN, even if the subquery returns only one row.
Result of Subquery
Final Result B01
DEPTNO DEPTNAME C01
D01
A00 SPIFFY COMPUTER SERVICE
D11
D21
E01
E11
E21
© Copyright IBM Corporation 2007
Notes:
The subquery in the example builds a list of the DEPTNO values for every department of
the PROJECT table. The outer query determines the department number and department
name of any department whose department number is NOT in the list returned by the
subquery. In other words, the query is reporting on departments which do not have
projects.
The IN predicate, when used with a subquery, enables the outer query to compare the
values in a column of the outer table with a list of values provided by the subquery.
For an IN predicate, if any of the non-null values returned by the subquery match any of the
non-null values of the column being searched in the outer query, the rows in the outer
query that contain the matching value will appear in the final result set. Nulls in the
subquery result will never match nulls in the outer query result.
When the IN is prefaced with a NOT, as in the example, the subquery should be written to
return only non-null values. Then, all of the rows of the outer query that do not match any of
the values returned by the subquery will appear in the result set. If the NOT IN subquery
returns a null, the outer query will always produce an empty result set!
Uempty
Final Result
WORKDEPT AVG_WORKDEPT Result of Subquery
A00 42833.33333333 25188.80000000
C01 26110.00000000
© Copyright IBM Corporation 2007
Notes:
Subqueries can be also used in a HAVING condition for GROUP values.
Restrictions/Reminders
Subqueries
Must be on right side of search condition when used
in WHERE clause or in HAVING clause
Must be enclosed in parentheses
Can return single or multiple values
Number of values subquery can return must be compatible
with operator in outer SELECT
The subquery must return the same number of items as in the
list to which it is compared
Notes:
Uempty
Checkpoint
Notes:
Please write down your answers below:
1.
2.
3.
Unit Summary
Having completed this unit, you should be able to:
Use subqueries in WHERE and HAVING clauses
Code subqueries using basic predicates
Code subqueries using IN
Notes:
Unit Objectives
After completing this unit, you should be able to:
Create a simple table to test DML statements
Create a view
Insert rows into a table
Update data in a table
Delete rows from a table
Drop a table
Notes:
Creating tables is normally done by database administrators. For test purposes, it could be
helpful to create a simple test table by yourself.
After the test table has been created, INSERT, UPDATE, and DELETE statements can be
used to add rows, change rows, and remove rows.
TESTEMP Table
Notes:
The visual shows the contents of the TESTEMP table. This table will be the basis for the
exercises of this unit.
Uempty
Notes:
This CREATE TABLE statement creates the TESTEMP table.
In addition to the column definitions, an IN clause may be required. The format of the IN
clause depends on the environment.
z/OS:
IN DB_NAME.TS_NAME - Table must be created in the named table space
IN DATABASE DB_NAME - If a database DB_NAME has been provided, and the table is
to be created in a table space which will be created by the system in this database.
No IN clause if the table is to be created in the default database. This is almost never
allowed by administrators in a z/OS environment.
No IN clause if a default table space has been provided and the table should be created in
the default table space.
Uempty
SELECT *
FROM PERSINFO
Notes:
This CREATE VIEW statement describes a part of the base table, consisting of employee
number, last name, and yearly salary of all employees in department C01.
The AS clause in the subselect enables you to specify column names in the view different
from the column names in the referenced table. In the above example, "SALARY AS
YEAR_SAL" is used to rename the SALARY column in the base table TESTEMP to
YEAR_SAL in view PERSINFO. Users referencing the view PERSINFO must use the new
column name.
Another possibility for renaming columns in a view is:
CREATE VIEW PERSINFO (EMPNO, LASTNAME, YEAR_SAL)
AS SELECT EMPNO, LASTNAME, SALARY
FROM EMPLOYEE WHERE WORKDEPT = 'C01'
When using this syntax, the number of column names listed in the parentheses must match
the number of the selected columns.
Users who are authorized to use view PERSINFO will get the listed columns for employees
whose department number is C01. Other data is not visible.
Uempty
OR
Notes:
Use an INSERT statement to insert a row into a table. Inserting a row via a view inserts the
row into the table on which the view is based. Therefore, inserts via a view are only
possible if the view contains all columns that are defined with NOT NULL without a
DEFAULT value in the base table.
In the first example, the column list is omitted. In this case, a value for each column in the
table or view must be provided in the VALUES clause, in the order they are defined for the
table or the view.
In the second example, a column list is specified. The number of values provided for a row
via the VALUES clause must be equal to the number of column names in the column list.
The first value is inserted in the first column of the list, the second value in the second
column, and so on.
DB2 UDB for Linux, UNIX, and Windows allows inserting multiple rows in one INSERT
statement; for example:
INSERT INTO TESTEMP
VALUES ('000111', 'SMITH', 'C01', '1998-06-25', 25000, NULL),
('000113', 'JONES', 'C01', '2001-06-25', 25000, NULL),
('000114', 'THOMPSON', 'C01', '2001-06-25', 25000, NULL)
Uempty
Notes:
Instead of the VALUES clause, include a SELECT statement in the INSERT statement to
tell DB2 UDB that another table or view contains the data for the new rows. The number
and sequence of the selected columns of the EMPLOYEE table must match the number
and sequence of the columns of the TESTEMP table.
If a columns list for the target table is used, all its NOT NULL columns must be included.
In the example, we assume that the rows for SMITH, JONES and THOMPSON had already
been inserted in the TESTEMP table, and we use this INSERT to copy selected rows from
the EMPLOYEE table into the TESTEMP table.
Update Columns (1 of 2)
Before:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000111 SMITH C01 1998-06-25 25000.00 -
000113 JONES C01 2001-06-25 25000.00 -
000114 THOMPSON C01 2001-06-25 25000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 38250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
UPDATE TESTEMP
SET BONUS = 500,
SALARY = 26000
WHERE EMPNO = '000111'
After:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000111 SMITH C01 1998-06-25 26000.00 500.00
000113 JONES C01 2001-06-25 25000.00 -
000114 THOMPSON C01 2001-06-25 25000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 38250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
© Copyright IBM Corporation 2007
Notes:
The UPDATE statement updates the values of the specified columns for one ore more rows
of a table or view.
Our new colleague, Mr Smith (employee number 000111) receives a first bonus and a
salary raise. Therefore, we must set BONUS to 500 and SALARY to 26000
If more than one column is changed, the columns must be separated by commas. The SET
keyword must precede the first column and must not be repeated.
To set BONUS to NULL, the UPDATE statement must be coded as follows:
UPDATE TESTEMP
SET BONUS = NULL
WHERE EMPNO = '000111'
Uempty
Update Columns (2 of 2)
Before:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000111 SMITH C01 1998-06-25 26000.00 500.00
000113 JONES C01 2001-06-25 25000.00 -
000114 THOMPSON C01 2001-06-25 25000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 38250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
UPDATE TESTEMP
SET SALARY = SALARY + 1000
WHERE WORKDEPT = 'C01'
After:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000111 SMITH C01 1998-06-25 27000.00 500.00
000113 JONES C01 2001-06-25 26000.00 -
000114 THOMPSON C01 2001-06-25 26000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 39250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
© Copyright IBM Corporation 2007
Notes:
Our department C01 has finished a project with great success. The salary of all employees
should be increased by 1,000.
To update selected rows, use the WHERE clause to specify the rows to be updated.
If you omit the WHERE clause, all rows of the table or view are updated.
Delete Rows
Before:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000111 SMITH C01 1998-06-25 27000.00 500.00
000113 JONES C01 2001-06-25 26000.00 -
000114 THOMPSON C01 2001-06-25 26000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 39250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
After:
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
000113 JONES C01 2001-06-25 26000.00 -
000114 THOMPSON C01 2001-06-25 26000.00 -
000010 HAAS A00 1965-01-01 52750.00 1000.00
000020 THOMPSON B01 1973-10-10 41250.00 800.00
000030 KWAN C01 1975-04-05 39250.00 800.00
000050 GEYER E01 1949-08-17 40175.00 800.00
© Copyright IBM Corporation 2007
Notes:
Mr Smith left the company. Therefore, we delete the row with employee number 000111.
The DELETE statement removes entire rows from a table. Depending on the search
condition of the WHERE clause, the DELETE statement removes zero, one, or more rows
of a table.
To delete specific rows, use the WHERE clause to specify the rows to be deleted.
If you omit the WHERE clause, all rows of the table or view are deleted.
Uempty
VIEW PERSINFO is
automatically dropped
© Copyright IBM Corporation 2007
Notes:
When executing the statement DROP TESTEMP, DB2 UDB deletes the object, that is, the
table. As a consequence, all rows and the table definition are deleted.
Any objects (for example, views, indexes) that are dependent on the table are deleted as
well.
Reminders
You must be authorized to execute CREATE/DROP table
statements
You must be authorized to execute statements which change
table content
Dropping a table drops dependent objects
Search condition in the WHERE clause qualifies rows to be
updated or deleted
Inserting a row into a view actually inserts the row into the
table on which the view is based
Updating a row of a view updates the row of the table on which
the view is based
Omitting the WHERE clause of an UPDATE or DELETE
statement causes all rows of the table or view to be updated or
deleted
© Copyright IBM Corporation 2007
Notes:
See your Database Administrator (DBA) for appropriate authorizations.
Uempty
Checkpoint
Notes:
Please write down your answers below:
1.
2.
Unit Summary
Having completed this unit, you should be able to:
Create a simple table to test DML statements
Create a view
Insert rows into a table
Update data in a table
Delete rows from a table
Drop a table
Notes:
Checkpoint Solutions
1. True
2. Automatic verification of correctness
Date arithmetic
Date, time, timestamp sorting
Extraction of components
3. CHAR/CHARACTER (fixed length)
VARCHAR (variable length)
SMALLINT
INTEGER, INT
DECIMAL(x,y), DEC(x,y)
4. SELECT
INSERT
UPDATE
DELETE
© Copyright IBM Corporation 2006
Unit 2
Checkpoint Solutions
1. False
2. c
3. SELECT, FROM
4. Apostrophes (or single quotes)
5. The keyword LIKE
6. False
They are joined by AND or OR.
AP Unit 3
Checkpoint Solutions
1. True
2. b
3. As short names for (qualified) tables
To avoid ambiguity
To establish correlated references
Unit 4
Checkpoint Solutions
1. False
2. c
3. DECIMAL
SUBSTR
COALESCE
VALUE
YEAR
MONTH
DAY
DAYS
ROUND
AP Unit 5
Checkpoint Solutions
Unit 6
Checkpoint
AP Unit 7
Checkpoint Solutions
1. True
2. b
3. IN
Unit 8
Checkpoint Solutions
Aggregate functions
AVG COUNT COUNT_BIG MAX
MIN STDDEV SUM VARIANCE
Scalar functions
ABS ACOS ASIN ATAN
ATANH ATAN2 BLOB CEILING
CHAR CLOB COALESCE CONCAT
COS COSH DATE DAY
DAYNAME DAYOFWEEK DAYOFWEEK_ISO DAYOFYEAR
DAYS DBCLOB DECIMAL DECRYPT_BIT
DECRYPT_CHAR DEGREES DIGITS DOUBLE_PRECISION
ENCRYPT EXP FLOAT FLOOR
GETHINT GRAPHIC HEX HOUR
IDENTITY_VAL_LOCAL INSERT INTEGER JULIAN_DAY
LCASE LEFT LENGTH LN
LOCATE LOG10 LOWER LTRIM
MICROSECOND MIDNIGHT_SECONDS MINUTE MOD
MONTH MONTHNAME MULTIPLY_ALT NULLIF
POSSTR POWER QUARTER RADIANS
RAISE_ERROR RAND REAL REPEAT
REPLACE RIGHT ROUND RTRIM
SECOND SIGN SIN SINH
SMALLINT SPACE SQRT SUBSTR
TAN TANH TIME TIMESTAMP
TRANSLATE TRUNCATE UCASE UPPER
VALUE VARCHAR VARGRAPHIC WEEK
WEEK_ISO YEAR
backpg
Back page
®