You are on page 1of 33

Simple SQL Queries

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

Copyright IBM Corporation 2007

EMPLOYEE Table - Part 1 (1 of 2)


MID EMPNO -----000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 FIRSTNME -----------CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE VINCENZO SEAN DOLORES HEATHER BRUCE ELIZABETH MASATOSHI R J M A INIT ------I L A B F D W Q G LASTNAME --------------HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER LUCCHESSI O'CONNELL QUINTANA NICHOLLS ADAMSON PIANKA YOSHIMURA WORK DEPT -------A00 B01 C01 E01 D11 D21 E11 E21 A00 A00 C01 C01 D11 D11 D11 PHONE NO ------3978 3476 4738 6789 6423 7831 5498 0972 3490 2167 4578 1793 4510 3782 2890 HIREDATE ---------1965-01-01 1973-10-10 1975-04-05 1949-08-17 1973-09-14 1980-09-30 1970-08-15 1980-06-19 1958-05-16 1963-12-05 1971-07-28 1976-12-15 1972-02-12 1977-10-11 1978-09-15

Copyright IBM Corporation 2007

EMPLOYEE Table - Part 1 (2 of 2)


ED EMPNO -----000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 JOB -------PRES MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER SALESREP CLERK ANALYST ANALYST DESIGNER DESIGNER DESIGNER LEVEL ----18 18 20 16 16 16 16 14 19 14 16 18 16 17 16 SEX --F M F M M F F M M M F F M F M BIRTHDATE ---------1933-08-24 1948-02-02 1941-05-11 1925-09-15 1945-07-07 1953-05-26 1941-05-15 1956-12-18 1929-11-05 1942-10-18 1925-09-15 1946-01-19 1947-05-17 1955-04-12 1951-01-05 SALARY --------52750.00 41250.00 38250.00 40175.00 32250.00 36170.00 29750.00 26150.00 46500.00 29250.00 23800.00 28420.00 25280.00 22250.00 24680.00 BONUS --------1000.00 800.00 800.00 800.00 500.00 700.00 600.00 500.00 900.00 600.00 500.00 600.00 500.00 400.00 500.00 COMM --------4220.00 3300.00 3060.00 3214.00 2580.00 2893.00 2380.00 2092.00 3720.00 2340.00 1904.00 2274.00 2022.00 1780.00 1974.00

Copyright IBM Corporation 2007

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

MGRNO -----000010 000020 000030 000060 000070 000050 000090 000100

ADMRDEPT -------A00 A00 A00 A00 D01 D01 A00 E01 E01

LOCATION --------

Copyright IBM Corporation 2007

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

Copyright IBM Corporation 2007

Structure of an SQL Query


SELECT - Defines result columns Column names Arithmetic expressions Literals (text or numeric) Scalar functions Column functions Concatenation FROM WHERE ORDER BY - Table or view names - Conditions (qualifies rows) - Sorts result rows
Copyright IBM Corporation 2007

Retrieving All Columns, All Rows


I need a listing of all department data

SELECT * FROM DEPARTMENT

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 -

Copyright IBM Corporation 2007

Retrieving All Columns, Limited Rows


What does the data look like in the Department table?

SELECT * FROM DEPARTMENT FETCH FIRST 5 ROWS ONLY

DEPTNO A00 B01 C01 D01 D11

DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS

MGRNO 000010 000020 000030 000060

ADMRDEPT A00 A00 A00 A00 D01

LOCATION -

Copyright IBM Corporation 2007

Selecting Specific Columns


For each department, I need its number, its name and the department to which it reports.

SELECT DEPTNO, DEPTNAME, ADMRDEPT FROM DEPARTMENT

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

Select with Ordered Output (1 of 2)


By the way, the listing should be sorted by the department reported to SELECT DEPTNO, DEPTNAME, ADMRDEPT FROM DEPARTMENT ORDER BY ADMRDEPT ASC

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 with Ordered Output (2 of 2)


I need the previous listing ordered first according to the number to which the department reports and within that sequence sorted by descending department numbers.

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

Alternate ORDER BY Specifications


SELECT LASTNAME, FIRSTNME, WORKDEPT, JOB, SEX FROM EMPLOYEE ORDER BY WORKDEPT DESC, JOB, LASTNAME, SEX DESC

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

Suppressing Duplicate Output Rows (1 of 2)


Now, I want to know the different jobs performed by the employees.
SELECT JOB FROM EMPLOYEE ORDER BY JOB JOB ANALYST ANALYST CLERK CLERK CLERK CLERK SELECT DISTINCT JOB FROM EMPLOYEE JOB ANALYST CLERK DESIGNER FIELDREP MANAGER OPERATOR PRES SALESREP
Copyright IBM Corporation 2007

. .

Suppressing Duplicate Output Rows (2 of 2)


Also, I need a listing of the job distribution by department

SELECT WORKDEPT, JOB FROM EMPLOYEE ORDER BY WORKDEPT, JOB


WORKDEPT A00 A00 A00 B01 C01 C01 C01 D11 D11 D11 D11 D11 D11 JOB CLERK PRES SALESREP MANAGER ANALYST ANALYST MANAGER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER

SELECT DISTINCT WORKDEPT, JOB FROM EMPLOYEE ORDER BY WORKDEPT, JOB


WORKDEPT A00 A00 A00 B01 C01 C01 D11 D11 D21 D21 E01 E11 E11 E21 E21
Copyright IBM Corporation 2007

. .

. .

JOB CLERK PRES SALESREP MANAGER ANALYST MANAGER DESIGNER MANAGER CLERK MANAGER MANAGER MANAGER OPERATOR FIELDREP MANAGER

Retrieving Rows by Character Comparison


I need a list of the departments reporting to department A00

SELECT FROM WHERE

DEPTNO, ADMRDEPT DEPARTMENT ADMRDEPT = 'A00'

DEPTNO A00 B01 C01 D01 E01

ADMRDEPT A00 A00 A00 A00 A00

Copyright IBM Corporation 2007

Retrieving Rows by Numerical Comparison


I want the last name and education level of all employees with an education level greater than or equal to 19
SELECT FROM WHERE LASTNAME, EDLEVEL EMPLOYEE EDLEVEL >= 19

LASTNAME KWAN LUCCHESSI

EDLEVEL 20 19

Copyright IBM Corporation 2007

Comparison Operators

SELECT FROM WHERE

* 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

Copyright IBM Corporation 2007

AND and OR - Principle

Y Y R R

Yellow AND Oval

Y Y R

Y Y

Yellow OR Oval

Y Y

Y Y R

Copyright IBM Corporation 2007

Multiple Conditions - AND


List employee number, job, and education level for analysts with an education level of 16

SELECT FROM WHERE AND

EMPNO, JOB, EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 16

EMPNO 000130

JOB ANALYST

EDLEVEL 16

Copyright IBM Corporation 2007

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.

SELECT FROM WHERE OR ORDER BY

EMPNO, JOB, EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 20 JOB, EMPNO

EMPNO 000130 000140 000030

JOB ANALYST ANALYST MANAGER


Copyright IBM Corporation 2007

EDLEVEL 16 18 20

Multiple Conditions - AND/OR


List the columns below for all analysts with an ed-level of 16. Include employees with an ed-level of 18. Sort the list by job and employee number.

SELECT FROM WHERE AND OR ORDER BY

EMPNO, JOB, EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 16 EDLEVEL = 18 JOB, EMPNO

EMPNO 000130 000140 000220 000020 000010

JOB ANALYST ANALYST DESIGNER MANAGER PRES

EDLEVEL 16 18 18 18 18

Copyright IBM Corporation 2007

Multiple Conditions - Parentheses


List the columns below for all analysts with an ed-level of 16 or 18.
SELECT FROM WHERE AND OR EMPNO, JOB, EDLEVEL EMPLOYEE JOB = 'ANALYST' (EDLEVEL = 16 EDLEVEL = 18)

EMPNO 000130 000140

JOB ANALYST ANALYST

EDLEVEL 16 18

Copyright IBM Corporation 2007

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.

SELECT FROM WHERE ORDER BY


LASTNAME JEFFERSON LEE O'CONNELL SMITH SPENSER LUCCHESSI KWAN EDLEVEL 14 14 14 14 14 19 20

LASTNAME, EDLEVEL EMPLOYEE EDLEVEL IN (14,19,20) EDLEVEL, LASTNAME

Copyright IBM Corporation 2007

SELECT with BETWEEN


List employee number and ed-level for all employees with an ed-level from 12 through 15. Sort by ed-level.

SELECT FROM WHERE ORDER BY


EMPNO 000290 000310 000100 000120 000230 000300 000330 000250 000270 EDLEVEL 12 12 14 14 14 14 14 15 15

EMPNO, EDLEVEL EMPLOYEE EDLEVEL BETWEEN 12 AND 15 EDLEVEL

Copyright IBM Corporation 2007

Retrieving Rows - Null Comparison


List number and name of all departments whose manager is unknown

SELECT FROM WHERE

DEPTNO, DEPTNAME, MGRNO DEPARTMENT MGRNO IS NULL

DEPTNO D01

DEPTNAME DEVELOPMENT CENTER

MGRNO -

Copyright IBM Corporation 2007

Partial String Search - LIKE


List the last name for all employees whose last name starts with the letter G

SELECT FROM WHERE

LASTNAME EMPLOYEE LASTNAME LIKE 'G%'

LASTNAME GEYER GOUNOT


Copyright IBM Corporation 2007

Partial String Search - Examples with '%'


SELECT FROM WHERE LASTNAME EMPLOYEE LASTNAME LIKE '%SON' LASTNAME THOMPSON HENDERSON ADAMSON JEFFERSON JOHNSON

SELECT FROM WHERE

LASTNAME EMPLOYEE LASTNAME LIKE '%M%N%'

LASTNAME THOMPSON ADAMSON MARINO

Copyright IBM Corporation 2007

Partial String Search - Example with '_'


I need a listing of all employee last names whose second character is the letter 'C'

SELECT FROM WHERE

LASTNAME EMPLOYEE LASTNAME LIKE '_C%'

LASTNAME SCOUTTEN SCHNEIDER


Copyright IBM Corporation 2007

Negation
I need a listing of all departments except those with a department number starting with 'D'

SELECT FROM WHERE

DEPTNO, DEPTNAME DEPARTMENT DEPTNO NOT LIKE 'D%'

DEPTNO A00 B01 C01 E01 E11 E21

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.

Copyright IBM Corporation 2007

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

Copyright IBM Corporation 2007

You might also like