You are on page 1of 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.

SELECT last_name. All rights reserved. • The default date format is DD-MON-RR. Comparison Conditions Using Comparison Conditions Operator Meaning SELECT last_name. and date values are format sensitive. (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. 2-5 Copyright © 2006.. last_name. job_id. Oracle. . All rights reserved. All rights reserved. department_id FROM employees WHERE last_name = 'Whalen' . job_id. > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to BETWEEN Between two values . department_id • Character strings and date values are enclosed in FROM employees single quotation marks. 2-6 Copyright © 2006. All rights reserved. WHERE department_id = 90 .AND. 2-8 Copyright © 2006. Using the WHERE Clause Character Strings and Dates SELECT employee_id. salary = Equal to FROM employees WHERE salary <= 3000 .. Oracle.. Oracle.. • Character values are case sensitive. Oracle.

2 . Oracle.10 Copyright © 2006. 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. All rights reserved. 201) . salary SELECT employee_id. WHERE first_name LIKE 'S%' . Oracle. 101.12 Copyright © 2006. salary. – % denotes zero or many characters. Oracle. last_name. 2 . manager_id FROM employees FROM employees WHERE salary BETWEEN 2500 AND 3500 . All rights reserved. SELECT first_name • You can use the ESCAPE identifier to search for the FROM employees actual % and _ symbols. – _ denotes one character. Oracle. 2 . 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. Lower limit Upper limit 2-9 Copyright © 2006. All rights reserved.11 Copyright © 2006. All rights reserved. SELECT last_name • Search conditions can contain either literal FROM employees characters or numbers: WHERE last_name LIKE '_o%' . . WHERE manager_id IN (100.

OR job_id LIKE '%MAN%' . All rights reserved. Oracle. 2 . job_id. . last_name. OR Returns TRUE if either component condition is true NOT Returns TRUE if the following condition is false 2 . 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. Operator Meaning AND Returns TRUE if both component SELECT last_name.15 Copyright © 2006. All rights reserved. salary SELECT employee_id.16 Copyright © 2006.14 Copyright © 2006. manager_id FROM employees conditions are true WHERE manager_id IS NULL . 2 . Oracle. All rights reserved. job_id. last_name. salary FROM employees FROM employees WHERE salary >=10000 WHERE salary >= 10000 AND job_id LIKE '%MAN%' . Using the NULL Conditions Logical Conditions Test for nulls with the IS NULL operator. Oracle.13 Copyright © 2006. All rights reserved. Oracle. 2 .

18 Copyright © 2006. Oracle. hire_date FROM employees SELECT last_name. Oracle. job_id. 'ST_CLERK'. Rules of Precedence Using the ORDER BY Clause SELECT last_name. LIKE. Oracle. [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 . All rights reserved. All rights reserved. All rights reserved. 2 . Oracle. FROM employees WHERE (job_id = 'SA_REP' 2 OR job_id = 'AD_PRES') AND salary > 15000. • The ORDER BY clause comes last in the SELECT statement: SELECT last_name.20 Copyright © 2006. 2 . department_id. job_id Operator Meaning FROM employees 1 Arithmetic operators WHERE job_id 2 Concatenation operator NOT IN ('IT_PROG'. job_id. All rights reserved. job_id.17 Copyright © 2006.19 Copyright © 2006. … 2 . salary ORDER BY hire_date . . 'SA_REP') . 3 Comparison conditions 4 IS [NOT] NULL. Using the NOT Operator Rules of Precedence SELECT last_name. default OR job_id = 'AD_PRES' – DESC: descending order AND salary > 15000. salary FROM employees • Sort retrieved rows with the ORDER BY clause: WHERE job_id = 'SA_REP' 1 – ASC: ascending order.

last_name. salary*12 annsal different values. • Sorting by multiple columns: SELECT last_name. 2 . ORDER BY hire_date DESC .23 Copyright © 2006. last_name.22 Copyright © 2006. last_name = ? . 2 . hire_date … department_id = ? … FROM employees ... 2 . Oracle.. department_id. salary.. . Oracle. Oracle. salary FROM employees 3 ORDER BY department_id. All rights reserved.21 Copyright © 2006. Oracle. 1 I want • Sorting by column alias: to query SELECT employee_id.. job_id. salary DESC.. 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.24 Copyright © 2006. salary = ? … SELECT last_name. department_id. Sorting Substitution Variables • Sorting in descending order: . FROM employees 2 ORDER BY annsal . department_id • Use substitution variables to supplement the FROM employees following: WHERE employee_id = &employee_num . All rights reserved. All rights reserved. – WHERE conditions – ORDER BY clauses – Column expressions – Table names – Entire SELECT statements 2 .

&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 . 2 . All rights reserved. Oracle. . &&column_name FROM employees ORDER BY &column_name . last_name. Using the && Substitution Variable Expressions. Using the & Substitution Variable Character and Date Values with Substitution Variables Use single quotation marks for date and character values: 101 SELECT last_name.27 Copyright © 2006. Oracle. SELECT employee_id. 2 . department_id.25 Copyright © 2006. Oracle. Oracle. job_id. job_id.28 Copyright © 2006.26 Copyright © 2006. salary salary > 15000 last_name … 2 . All rights reserved. salary*12 FROM employees WHERE job_id = '&job_title' . and Text SELECT employee_id. Specifying Column Names. All rights reserved. last_name. 1 2 2 . All rights reserved.

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. All rights reserved. Oracle. 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]. Oracle. salary. All rights reserved. substitution variable..} your SQL SELECT statements FROM table [WHERE condition(s)] [ORDER BY {column. LIKE. last_name. department_id DEFINE employee_num = 200 FROM employees WHERE employee_id = &employee_num...29 Copyright © 2006. OR. Oracle. 2 . SET VERIFY ON SELECT employee_id. salary. department_id FROM employees WHERE employee_id = &employee_num .31 Copyright © 2006. 2 . and NULL conditions • Restricting rows by using the WHERE clause – Apply the logical AND. 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. expr. • Use ampersand substitution in iSQL*Plus to restrict and sort output at run time 2 . .30 Copyright © 2006.32 Copyright © 2006. SELECT employee_id. UNDEFINE employee_num old 3: WHERE employee_id = &employee_num new 3: WHERE employee_id = 200 2 . alias} [ASC|DESC]] . Oracle. Summary Practice 2: Overview In this lesson. both before and after iSQL*Plus • Use the iSQL*Plus UNDEFINE command to remove a replaces substitution variables with values: variable. IN. All rights reserved. last_name. All rights reserved.

Oracle.35 Copyright © 2006. 2 . 2 . Oracle. All rights reserved. Oracle. All rights reserved. . All rights reserved. 2 . All rights reserved.2 .36 Copyright © 2006. Oracle.33 Copyright © 2006.34 Copyright © 2006.

Oracle. . 2 .2 . All rights reserved.37 Copyright © 2006.38 Copyright © 2006. All rights reserved. Oracle.