Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
Objectives
After completing this lesson, you should be able to do the following:
Limit the rows that are retrieved by a query
Sort the rows that are retrieved by a query
Use ampersand substitution to restrict and sort output at run time
2-2
Lesson Agenda
Limiting rows with:
The WHERE clause
The comparison conditions using =, <=, BETWEEN, IN, LIKE, and
NULL conditions
Logical conditions using AND, OR, and NOT operators
2-3
EMPLOYEES
retrieve all
employees in
department 90
2-4
2-5
2-6
2-7
Comparison Operators
Operator
Meaning
Equal to
>
Greater than
>=
<
Less than
<=
<>
Not equal to
BETWEEN
...AND...
IN(set)
LIKE
IS NULL
Is a null value
2-8
2-9
Upper limit
2 - 10
2 - 11
SELECT
FROM
WHERE
first_name
employees
first_name LIKE 'S%' ;
2 - 12
You can use the ESCAPE identifier to search for the actual % and
_ symbols.
2 - 13
2 - 14
Operator
Meaning
AND
OR
NOT
2 - 15
2 - 16
2 - 17
2 - 18
Lesson Agenda
Limiting rows with:
The WHERE clause
The comparison conditions using =, <=, BETWEEN, IN, LIKE, and
NULL operators
Logical conditions using AND, OR, and NOT operators
2 - 19
Rules of Precedence
Operator
Meaning
Arithmetic operators
Concatenation operator
Comparison conditions
[NOT] BETWEEN
Not equal to
OR logical condition
2 - 20
Rules of Precedence
SELECT
FROM
WHERE
OR
AND
SELECT
FROM
WHERE
OR
AND
2 - 21
Lesson Agenda
Limiting rows with:
The WHERE clause
The comparison conditions using =, <=, BETWEEN, IN, LIKE, and
NULL operators
Logical conditions using AND, OR, and NOT operators
2 - 22
2 - 23
Sorting
Sorting in descending order:
SELECT
last_name, job_id, department_id, hire_date
FROM
employees
1
ORDER BY hire_date DESC ;
2 - 24
Sorting
Sorting by using the columns numeric position:
SELECT
last_name, job_id, department_id, hire_date
FROM
employees
3
ORDER BY 3;
2 - 25
Lesson Agenda
Limiting rows with:
The WHERE clause
The comparison conditions using =, <=, BETWEEN, IN, LIKE, and
NULL operators
Logical conditions using AND, OR, and NOT operators
2 - 26
Substitution Variables
... salary = ?
department_id = ?
... last_name = ? ...
I want
to query
different
values.
2 - 27
Substitution Variables
Use substitution variables to:
Temporarily store values with single-ampersand (&) and doubleampersand (&&) substitution
WHERE conditions
ORDER BY clauses
Column expressions
Table names
Entire SELECT statements
2 - 28
2 - 29
2 - 30
2 - 31
2 - 32
2 - 33
Lesson Agenda
Limiting rows with:
The WHERE clause
The comparison conditions using =, <=, BETWEEN, IN, LIKE, and
NULL operators
Logical conditions using AND, OR, and NOT operators
2 - 34
2 - 35
2 - 36
Summary
In this lesson, you should have learned how to:
Use the WHERE clause to restrict rows of output:
Use the comparison conditions
Use the BETWEEN, IN, LIKE, and NULL operators
Apply the logical AND, OR, and NOT operators
2 - 37
Practice 2: Overview
This practice covers the following topics:
Selecting data and changing the order of the rows
that are displayed
Restricting rows by using the WHERE clause
Sorting rows by using the ORDER BY clause
Using substitution variables to add flexibility to your
SQL SELECT statements
2 - 38