Professional Documents
Culture Documents
Copyright IBM Corporation 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
4.0.3 3.3.1
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
Use the keywords BETWEEN, IN, LIKE, DISTINCT Order the resulting rows in a desired sequence
DEPARTMENT Table
DEPTNO -----A00 B01 C01 D01 D11 D21 E01 E11 E21
DEPTNAME ----------------------------SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT
ADMRDEPT -------A00 A00 A00 A00 D01 D01 A00 E01 E01
LOCATION --------
PROJECT Table
DEPT PROJNO -----AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 IF2000 MA2100 MA2110 MA2111 MA2112 MA2113 OP1000 OP1010 OP2000 OP2010 OP2011 OP2012 OP2013 PL2100 PROJNAME --------------------ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT PROGRAMMING QUERY SERVICES USER EDUCATION WELD LINE AUTOMATION W L PROGRAMMING W L PROGRAM DESIGN W L ROBOT DESIGN W L PROD CONT PROGS OPERATION SUPPORT OPERATION GEN SYSTEMS SERVICES SYSTEMS SUPPORT SCP SYSTEMS SUPPORT APPLICATIONS SUPPORT DB/DC SUPPORT WELD LINE PLANNING NO ---D01 D21 D21 D21 D21 C01 C01 D01 D11 D11 D11 D11 E01 E11 E01 E21 E21 E21 E21 B01 RESP EMP -----000010 000070 000230 000250 000270 000030 000030 000010 000060 000220 000150 000160 000050 000090 000050 000100 000320 000330 000340 000020 PR STAFF ----6.50 6.00 2.00 1.00 2.00 2.00 1.00 12.00 9.00 2.00 3.00 3.00 6.00 5.00 5.00 4.00 1.00 1.00 1.00 1.00 PRSTDATE ---------1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-02-15 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 PRENDATE ---------1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-12-01 1982-12-01 1982-12-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-09-15 MAJ PROJ -----AD3100 AD3110 AD3110 AD3110 MA2100 MA2110 MA2110 MA2110 OP1000 OP2000 OP2010 OP2010 OP2010 MA2100
DEPTNO DEPTNAME A00 B01 C01 D01 D11 D21 E01 E11 E21 SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT
MGRNO ADMRDEPT LOCATION 000010 000020 000030 000060 000070 000050 000090 000100 A00 A00 A00 A00 D01 D01 A00 E01 E01 -
DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS
LOCATION -
DEPTNO A00 B01 C01 D01 D11 D21 E01 E11 E21
DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT
Copyright IBM Corporation 2007
ADMRDEPT A00 A00 A00 A00 D01 D01 A00 E01 E01
DEPTNO A00 C01 B01 E01 D01 D11 D21 E21 E11
DEPTNAME ADMRDEPT SPIFFY COMPUTER SERVICE DIV.A00 A00 INFORMATION CENTER A00 PLANNING A00 SUPPORT SERVICES A00 DEVELOPMENT CENTER D01 MANUFACTURING SYSTEMS D01 ADMINISTRATION SYSTEMS E01 SOFTWARE SUPPORT E01 OPERATIONS
Copyright IBM Corporation 2007
SELECT DEPTNO, DEPTNAME, ADMRDEPT FROM DEPARTMENT ORDER BY ADMRDEPT ASC, DEPTNO DESC
DEPTNO E01 D01 C01 B01 A00 D21 D11 E21 E11
DEPTNAME SUPPORT SERVICES DEVELOPMENT CENTER INFORMATION CENTER PLANNING SPIFFY COMPUTER SERVICE DIV. ADMINISTRATION SYSTEMS MANUFACTURING SYSTEMS SOFTWARE SUPPORT OPERATIONS
Copyright IBM Corporation 2007
ADMRDEPT A00 A00 A00 A00 A00 D01 D01 E01 E01
Equivalent ORDER BY clauses: ORDER BY WORKDEPT DESC, JOB ASC, LASTNAME ASC, SEX DESC ORDER BY 3 DESC, 4, 1, 5 DESC ORDER BY 3 DESC, 4 ASC, 1 ASC, 5 DESC ORDER BY 3 DESC, JOB, LASTNAME, 5 DESC ORDER BY WORKDEPT DESC, 4 ASC, 1 ASC, SEX DESC
Copyright IBM Corporation 2007
. .
. .
. .
JOB CLERK PRES SALESREP MANAGER ANALYST MANAGER DESIGNER MANAGER CLERK MANAGER MANAGER MANAGER OPERATOR FIELDREP MANAGER
EDLEVEL 20 19
Comparison Operators
* EMPLOYEE SALARY = 20000 -- equal to OR OR OR OR OR SALARY <> 20000 -- not equal to SALARY > SALARY < 20000 -- greater than 20000 -- less than SALARY >= 20000 -- greater than or equal to SALARY <= 20000 -- less than or equal to
Y Y R R
Y Y R
Y Y
Yellow OR Oval
Y Y
Y Y R
EMPNO 000130
JOB ANALYST
EDLEVEL 16
Multiple Conditions - OR
List the columns below for all analysts and all employees with an education level of 20. Sort the list by job and employee number.
EDLEVEL 16 18 20
EMPNO, JOB, EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 16 EDLEVEL = 18 JOB, EMPNO
EDLEVEL 16 18 18 18 18
EDLEVEL 16 18
SELECT with IN
List last name and ed-level for all employees with an ed-level of 14, 19 or 20. Sort the listing by ed-level and last name.
DEPTNO D01
MGRNO -
Negation
I need a listing of all departments except those with a department number starting with 'D'
DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT
Copyright IBM Corporation 2007
Checkpoint
1. True or False? The sequence in which clauses are coded in a SELECT statement is arbitrary. 2. BETWEEN causes the rows of the result table to be
a. Sorted in ascending order b. Sorted in descending order c. Not sorted at all
3. Which clauses in a SELECT statement are required? 4. Within a WHERE clause, character data, dates, times and timestamps must always be enclosed within what? 5. When doing patterned character string searches, what gives the underscore and the percent symbol their masking abilities? 6. True or False? WHERE clause predicates must be separated from each other by commas.
Copyright IBM Corporation 2007
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.
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
Use the keywords BETWEEN, IN, LIKE, DISTINCT Order the resulting rows in a desired sequence