1.3 - Restricting and Sorting Data - Lecture Note
1.3 - Restricting and Sorting Data - Lecture Note
1.3 RESTRICTING AND SORTING DATA method of restriction is the basis of the WHERE
clause in SQL.
Objectives
After completing this lesson, you should be able to
do the following: Limiting the Rows That Are Selected
Execute a SQL SELECT statement in solving Restrict the rows that are returned by using the
problems. WHERE clause:
The WHERE clause follows the FROM clause.
Use the WHERE clause function to limit the
rows of output.
Introduction
Overview
You can restrict the rows that are returned from the
When retrieving data from the database, you may query by using the WHERE clause. A WHERE clause
need to do the following: contains a condition that must be met, and it
directly follows the FROM clause. If the condition is
Restrict the rows of data that are displayed
true, the row meeting the condition is returned.
Specify the order in which the rows are
Syntax:
displayed
WHERE: Restricts the query to rows that
This lesson explains the SQL statements that you
meet a condition.
use to perform these actions.
Condition: Composed of column names,
expressions, constants, and a comparison
Limiting Rows Using a Selection operator.
The WHERE Clause Can Compare:
Values in columns
Literal values
Arithmetic expressions
Functions
It consists of three elements:
1. Column name
2. Comparison condition
3. Column name, constant, or list of values
Example:
Character strings and dates in the WHERE clause
must be enclosed in single quotation marks ('').
However, number constants should not be Comparison conditions are used to compare one
enclosed in single quotation marks. expression to another value or expression. They are
commonly used in the WHERE clause in the
All character searches are case-sensitive. following format:
In the following example, no rows are returned Syntax:
because the EMPLOYEES table stores all last names
in mixed case:
ITE 014 | INFORMATION MANAGEMENT
Examples: The BETWEEN condition specifies a lower
limit and an upper limit.
The SELECT statement returns rows from
the EMPLOYEES table for employees whose
salary is between $2,500 and $3,500.
Values specified with BETWEEN are
An alias cannot be used in the WHERE inclusive. The lower limit must be specified
clause. first.
The symbols != and ^= can also represent You can also use the BETWEEN condition with
the not equal to condition. character values:
The underscore (_) is now interpreted To retrieve employees who are not entitled to
literally instead of as a wildcard. receive a commission, use:
Logical Conditions
2. Condition 2: Employee has job ID Sort retrieved rows using ORDER BY.
'SA_REP'. Sorting order options:
Since AND has a higher precedence than o ASC (Ascending order, default).
OR, the query first checks if the employee is
a president earning more than $15,000. o DESC (Descending order).
Then, it includes all employees who are The ORDER BY clause must be the last
sales representatives (SA_REP), regardless clause in the SELECT statement.
of salary.
🔹 Query Meaning:
"Select employees who are presidents earning
more than $15,000 OR employees who are sales
representatives."
Syntax:
Explanation:
Two conditions are evaluated: Components:
1. Condition 1: Employee has job ID ORDER BY → Specifies the sorting order of
'AD_PRES' OR 'SA_REP' (evaluated the result set.
first due to parentheses).
ASC → Sorts rows in ascending order
2. Condition 2: Employee earns more (default).
than $15,000.
DESC → Sorts rows in descending order.
ITE 014 | INFORMATION MANAGEMENT
Key Notes: Reverse the default sort order using DESC:
If ORDER BY is not used, the order of rows
is undefined.
The Oracle server may fetch rows in
different orders for the same query if
sorting is not explicitly defined.
🔹 Result: Sorts employees by most recent hire date
first.
Sorting
Default Sort Order (Ascending)
2. Using Column Alias in ORDER BY Clause
Numeric Values → Sorted from smallest to
largest (1 to 999).
Date Values → Earliest dates first (01-JAN-
92 before 01-JAN-95).
🔹 Result: Sorts employees by annual salary in
Character Values → Alphabetical order (A descending order.
first, Z last).
Null Values →
3. Sorting by Multiple Columns
o Last in ascending order (ASC).
o First in descending order (DESC).
Sorting on Non-Selected Columns → You
can sort by a column that is not in the
SELECT list. 🔹 Result:
Primary Sort: Ascending by department_id.
Secondary Sort: Descending by salary
within each department.
S
The value 100 is used for employee_num,
Double ampersand (&&) is used when you and the user is not prompted.
want to reuse a variable without prompting
To remove the variable:
the user multiple times.
The user is prompted once, and the entered
value is used throughout the statement.
Example: Using the VERIFY Command
The VERIFY command toggles the display of
the substitution variable before and after
iSQL*Plus replaces it with a value.
If a variable is created using DEFINE, the Use the WHERE clause to restrict output
user is not prompted for input; instead, the rows:
pre-assigned value is used in the query. o Apply comparison conditions (=, >, <,
The variable remains in the session until the >=, <=, !=)
user undefines it or exits SQL*Plus.
ITE 014 | INFORMATION MANAGEMENT
o Use BETWEEN, IN, LIKE, and IS NULL
conditions
o Apply logical operators (AND, OR,
NOT)
Use the ORDER BY clause to sort output
rows.
Use ampersand (&) substitution in
SQL*Plus to:
o Restrict and sort output at runtime.
o Allow user input in SQL statements
dynamically.