Sie sind auf Seite 1von 10

Objectives

After completing this lesson, you should be able to do


the following:
• Limit the rows that are retrieved by a query
Restricting and Sorting Data
• Sort the rows that are retrieved by a query
• Use ampersand substitution in iSQL*Plus to
restrict and sort output at run time

Copyright © 2006, Oracle. All rights reserved. 2-2 Copyright © 2006, Oracle. All rights reserved.

Limiting Rows Using a Selection Limiting the Rows That Are Selected

EMPLOYEES • Restrict the rows that are returned by using the


WHERE clause:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];

… • The WHERE clause follows the FROM clause.

“retrieve all
employees in
department 90”

2-3 Copyright © 2006, Oracle. All rights reserved. 2-4 Copyright © 2006, Oracle. All rights reserved.
Using the WHERE Clause Character Strings and Dates

SELECT employee_id, last_name, job_id, department_id • Character strings and date values are enclosed in
FROM employees single quotation marks.
WHERE department_id = 90 ;
• Character values are case sensitive, and date values
are format sensitive.
• The default date format is DD-MON-RR.

SELECT last_name, job_id, department_id


FROM employees
WHERE last_name = 'Whalen' ;

2-5 Copyright © 2006, Oracle. All rights reserved. 2-6 Copyright © 2006, Oracle. All rights reserved.

Comparison Conditions Using Comparison Conditions

Operator Meaning SELECT last_name, salary


= Equal to FROM employees
WHERE salary <= 3000 ;
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN Between two values
...AND... (inclusive)
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value

2-7 Copyright © 2006, Oracle. All rights reserved. 2-8 Copyright © 2006, Oracle. All rights reserved.
Using the BETWEEN Condition Using the IN Condition

Use the BETWEEN condition to display rows based on a Use the IN membership condition to test for values in a
range of values: list:
SELECT last_name, salary SELECT employee_id, last_name, salary, manager_id
FROM employees FROM employees
WHERE salary BETWEEN 2500 AND 3500 ; WHERE manager_id IN (100, 101, 201) ;

Lower limit Upper limit

2-9 Copyright © 2006, Oracle. All rights reserved. 2 - 10 Copyright © 2006, Oracle. All rights reserved.

Using the LIKE Condition Using the LIKE Condition

• Use the LIKE condition to perform wildcard • You can combine pattern-matching characters:
searches of valid search string values. SELECT last_name
• Search conditions can contain either literal FROM employees
characters or numbers: WHERE last_name LIKE '_o%' ;

– % denotes zero or many characters.


– _ denotes one character.

SELECT first_name
• You can use the ESCAPE identifier to search for the
FROM employees actual % and _ symbols.
WHERE first_name LIKE 'S%' ;

2 - 11 Copyright © 2006, Oracle. All rights reserved. 2 - 12 Copyright © 2006, Oracle. All rights reserved.
Using the NULL Conditions Logical Conditions

Test for nulls with the IS NULL operator. Operator Meaning


AND Returns TRUE if both component
SELECT last_name, manager_id
FROM employees conditions are true
WHERE manager_id IS NULL ; OR Returns TRUE if either component
condition is true
NOT Returns TRUE if the following
condition is false

2 - 13 Copyright © 2006, Oracle. All rights reserved. 2 - 14 Copyright © 2006, Oracle. All rights reserved.

Using the AND Operator Using the OR Operator

AND requires both conditions to be true: OR requires either condition to be true:


SELECT employee_id, last_name, job_id, salary SELECT employee_id, last_name, job_id, salary
FROM employees FROM employees
WHERE salary >=10000 WHERE salary >= 10000
AND job_id LIKE '%MAN%' ; OR job_id LIKE '%MAN%' ;

2 - 15 Copyright © 2006, Oracle. All rights reserved. 2 - 16 Copyright © 2006, Oracle. All rights reserved.
Using the NOT Operator Rules of Precedence

SELECT last_name, job_id Operator Meaning


FROM employees 1 Arithmetic operators
WHERE job_id
2 Concatenation operator
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition

You can use parentheses to override rules of precedence.

2 - 17 Copyright © 2006, Oracle. All rights reserved. 2 - 18 Copyright © 2006, Oracle. All rights reserved.

Rules of Precedence Using the ORDER BY Clause

SELECT last_name, job_id, salary


FROM employees • Sort retrieved rows with the ORDER BY clause:
WHERE job_id = 'SA_REP' 1 – ASC: ascending order, default
OR job_id = 'AD_PRES' – DESC: descending order
AND salary > 15000;
• The ORDER BY clause comes last in the SELECT
statement:
SELECT last_name, job_id, department_id, hire_date
FROM employees
SELECT last_name, job_id, salary ORDER BY hire_date ;
FROM employees
WHERE (job_id = 'SA_REP' 2
OR job_id = 'AD_PRES')
AND salary > 15000;

2 - 19 Copyright © 2006, Oracle. All rights reserved. 2 - 20 Copyright © 2006, Oracle. All rights reserved.
Sorting Substitution Variables

• Sorting in descending order: ... salary = ? …


SELECT last_name, job_id, department_id, hire_date … department_id = ? …
FROM employees ... last_name = ? ...
ORDER BY hire_date DESC ; 1
I want
• Sorting by column alias: to query
SELECT employee_id, last_name, salary*12 annsal different
values.
FROM employees 2
ORDER BY annsal ;

• Sorting by multiple columns:


SELECT last_name, department_id, salary
FROM employees 3
ORDER BY department_id, salary DESC;

2 - 21 Copyright © 2006, Oracle. All rights reserved. 2 - 22 Copyright © 2006, Oracle. All rights reserved.

Substitution Variables Using the & Substitution Variable

• Use iSQL*Plus substitution variables to: Use a variable prefixed with an ampersand (&) to prompt
– Temporarily store values with single-ampersand (&) the user for a value:
and double-ampersand (&&) substitution
SELECT employee_id, last_name, salary, department_id
• Use substitution variables to supplement the FROM employees
following: WHERE employee_id = &employee_num ;
– WHERE conditions
– ORDER BY clauses
– Column expressions
– Table names
– Entire SELECT statements

2 - 23 Copyright © 2006, Oracle. All rights reserved. 2 - 24 Copyright © 2006, Oracle. All rights reserved.
Using the & Substitution Variable Character and Date Values
with Substitution Variables

Use single quotation marks for date and character values:

101 SELECT last_name, department_id, salary*12


FROM employees
WHERE job_id = '&job_title' ;

1
2

2 - 25 Copyright © 2006, Oracle. All rights reserved. 2 - 26 Copyright © 2006, Oracle. All rights reserved.

Specifying Column Names, Using the && Substitution Variable


Expressions, and Text

SELECT employee_id, last_name, job_id,&column_name Use the double ampersand (&&) if you want to reuse the
FROM employees variable value without prompting the user each time:
WHERE &condition
ORDER BY &order_column ; SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name ;

salary

salary > 15000

last_name

2 - 27 Copyright © 2006, Oracle. All rights reserved. 2 - 28 Copyright © 2006, Oracle. All rights reserved.
Using the iSQL*Plus DEFINE Command Using the VERIFY Command

• Use the iSQL*Plus DEFINE command to create and Use the VERIFY command to toggle the display of the
assign a value to a variable. substitution variable, both before and after iSQL*Plus
• Use the iSQL*Plus UNDEFINE command to remove a replaces substitution variables with values:
variable.
SET VERIFY ON
SELECT employee_id, last_name, salary, department_id
DEFINE employee_num = 200
FROM employees
WHERE employee_id = &employee_num;
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;

UNDEFINE employee_num old 3: WHERE employee_id = &employee_num


new 3: WHERE employee_id = 200

2 - 29 Copyright © 2006, Oracle. All rights reserved. 2 - 30 Copyright © 2006, Oracle. All rights reserved.

Summary Practice 2: Overview

In this lesson, you should have learned how to: This practice covers the following topics:
• Use the WHERE clause to restrict rows of output: • Selecting data and changing the order of
– Use the comparison conditions the rows that are displayed
– Use the BETWEEN, IN, LIKE, and NULL conditions • Restricting rows by using the WHERE clause
– Apply the logical AND, OR, and NOT operators • Sorting rows by using the ORDER BY clause
• Use the ORDER BY clause to sort rows of output: • Using substitution variables to add flexibility to
SELECT *|{[DISTINCT] column|expression [alias],...} your SQL SELECT statements
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]] ;

• Use ampersand substitution in iSQL*Plus to


restrict and sort output at run time

2 - 31 Copyright © 2006, Oracle. All rights reserved. 2 - 32 Copyright © 2006, Oracle. All rights reserved.
2 - 33 Copyright © 2006, Oracle. All rights reserved. 2 - 34 Copyright © 2006, Oracle. All rights reserved.

2 - 35 Copyright © 2006, Oracle. All rights reserved. 2 - 36 Copyright © 2006, Oracle. All rights reserved.
2 - 37 Copyright © 2006, Oracle. All rights reserved. 2 - 38 Copyright © 2006, Oracle. All rights reserved.

Das könnte Ihnen auch gefallen