At A Glance: Restricting Rows and Sorting Data
At A Glance: Restricting Rows and Sorting Data
Chapter 8
Restricting Rows and Sorting Data
At a Glance
Instructor’s Notes
♦ Chapter Overview
♦ Chapter Objectives
♦ Instructor Notes
♦ Troubleshooting Tips
♦ Quick Quizzes
♦ Discussion Questions
♦ Key Terms
Oracle 12c: SQL 8-2
Chapter Overview
In this chapter, students will learn how to select rows based on a condition specified in a
WHERE clause. This will include learning various comparison operators and how to join
multiple conditions using logical operators. In addition, the ORDER BY clause can be used to
perform primary and secondary sorts to display the output in a specified order.
Chapter Objectives
After completing this chapter, you should be able to do the following:
Instructor Notes
Quick Quiz
1. Which part of an SQL command is case sensitive?
ANSWER: Search condition values
5. What type of data does not need to be included in single quotation marks when searching
for specific rows in a table?
ANSWER: Numeric data
Comparison Operators
Comparison operators include arithmetic operators, as well as special comparison operators. The
special comparison operators include BETWEEN, IN, LIKE, and NULL. Each special operator
has a specific syntax that must be followed or an error message will be returned.
Oracle 12c: SQL 8-4
Troubleshooting Tip Demonstrate that the BETWEEN operator includes the specified
endpoints of the range.
The LIKE operator is different from the other basic comparison operators in that it is used to
identify character patterns. The % and _ wildcard characters are used to create a pattern.
Quick Quiz
1. Which comparison operator can be used to specify a range of values?
ANSWER: BETWEEN
Logical Operators
Logical operators are used to combine search conditions. If the AND operator is used to combine
more than one condition, both conditions must be TRUE. If the OR operator is used to combine
the conditions, only one condition must be TRUE. The NOT is used to specify that only FALSE
evaluations are to be included in the results.
Troubleshooting Tip Demonstrate that the logical operators are evaluated in the
sequence of NOT, AND, and OR. Present an example and then use
parentheses to override the order of evaluation.
Troubleshooting Tip Demonstrate that the condition of = NULL or =’NULL’ will not
return the same results as the IS NULL comparison operator.
Quick Quiz
1. Why can’t the equal sign operator be used to search for NULL values?
ANSWER: A NULL value is not equal to anything.
Quick Quiz
1. What is the purpose of the ORDER BY clause?
ANSWER: Specifies the presentation order of the output
2. When data is sorted in descending order, are NULL values listed first or last?
ANSWER: First
4. How can the position of a column in the SELECT clause be referenced in the ORDER
BY clause?
ANSWER: Specify the numeric position of the column in the column list given in the
SELECT clause
5. When specifying a secondary sort, how are the column names listed in the ORDER BY
clause?
ANSWER: The column for the primary sort is listed first, followed by a comma, and then
the column for the second sort is listed.
Discussion Questions
1. Discuss the differences in handling different datatypes when including conditions in a
query.
Key Terms
comparison operator — A search condition that indicates how data should relate to a given
search value (equal to, greater than, less than, etc.). Common comparison operators include >, <,
>=, and <= .
Oracle 12c: SQL 8-7
condition — A portion of an SQL statement that identifies what must exist, or a requirement that
must be met. When a query is executed, any record meeting the given condition will be returned
in query results.
equality operator — A search condition that evaluates data for exact, or equal, values. The
equality operator symbol is the equal sign (=).
logical operators — Used to combine two or more search conditions. The logical operators
include AND and OR. The NOT operator reverses the meaning of search conditions.
NULL value — Means no value has been stored in that particular field. A NULL value indicates
the absence of data, not a blank space.
primary sort — When only one column is specified in the ORDER BY clause, data is ordered,
or sorted, based on the data organization within the specified column.
secondary sort — When two or more columns are specified in the ORDER BY clause, data in
the second column (or additional columns) provide an alternative field on which to order data if
an exact match occurs between two or more rows in the first, or primary, sort.
wildcard characters — Symbols used to represent one or more alphanumeric characters. The
wildcard characters in Oracle 12c are the percent sign (%) and the underscore symbol ( _ ). The
percent sign is used to represent any number of characters; the underscore represents one
character.