0% found this document useful (0 votes)
51 views

At A Glance: Restricting Rows and Sorting Data

The document discusses restricting rows and sorting data in Oracle 12c SQL. It covers using a WHERE clause to restrict rows based on conditions, comparison operators like BETWEEN and IN, logical operators like AND and OR, handling NULL values, and using an ORDER BY clause to sort results. The chapter objectives are to learn how to restrict rows, use comparison operators, join conditions, handle NULLs, and sort data.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

At A Glance: Restricting Rows and Sorting Data

The document discusses restricting rows and sorting data in Oracle 12c SQL. It covers using a WHERE clause to restrict rows based on conditions, comparison operators like BETWEEN and IN, logical operators like AND and OR, handling NULL values, and using an ORDER BY clause to sort results. The chapter objectives are to learn how to restrict rows, use comparison operators, join conditions, handle NULLs, and sort data.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Oracle 12c: SQL 8-1

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:

♦ Use a WHERE clause to restrict the rows returned by a query


♦ Create a search condition using mathematical comparison operators
♦ Use the BETWEEN...AND comparison operator to identify records within a range of
values
♦ Specify a list of values for a search condition using the IN comparison operator
♦ Search for patterns using the LIKE comparison operator
♦ Identify the purpose of the % and _ wildcard characters
♦ Join multiple search conditions using the appropriate logical operator
♦ Perform searches for NULL values
♦ Specify the order for the presentation of query results using an ORDER BY clause

Instructor Notes

WHERE Clause Syntax


The WHERE clause specifies the rows to be displayed in the output. The WHERE clause syntax
requires the user to specify the column to be used for the comparison, the comparison operator to
indicate the relationship, and the value the column is to be compared against. The condition can
be a value or another column stored in the database. If the condition is not a numeric value, it
must be enclosed in single quotation marks.

Troubleshooting Tip Demonstrate that search conditions are case sensitive by


specifying a condition in lowercase characters that is stored in the
table in uppercase characters.
Oracle 12c: SQL 8-3

Quick Quiz
1. Which part of an SQL command is case sensitive?
ANSWER: Search condition values

2. When must a search condition value be included in single quotation marks?


ANSWER: If it is a nonnumeric value

3. What is special about searching for dates?


ANSWER: The date must be in single quotation marks and in the format of DD-MON-
YY.

4. When is the WHERE clause required?


ANSWER: When searching for rows based on a certain condition

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

2. Which comparison operator is used to indicate a list of values?


ANSWER: IN

3. Wildcard characters can be used with which comparison operator?


ANSWER: LIKE

4. Which wildcard character represents exactly one character in a search pattern?


ANSWER: Underscore

5. Which wildcard character represents one or more characters in a search pattern?


ANSWER: Percent sign
Oracle 12c: SQL 8-5

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.

Treatment of NULL Values


Since a NULL value indicates the absence of data, it can only be identified using the IS NULL
comparison operator. No condition is specified because it is implied in the operator. To identify
rows that do not contain a NULL value in the specified column, use the IS NOT NULL operator.

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.

2. What is a NULL value?


ANSWER: The absence of data

3. What is the purpose of the IS NULL operator?


ANSWER: It is used to search for rows containing NULL values in a specified column.

4. When is the IS NOT NULL operator used?


ANSWER: When searching for rows that do not contain a NULL value in the specified
column

5. Why can’t a blank space be used to search for NULL values?


ANSWER: A blank space has a value; a NULL values does not.
Oracle 12c: SQL 8-6

ORDER BY Clause Syntax


The ORDER BY clause is used to indicate that the results are to be presented in a specified
order. This makes it easier for management to locate specific rows in the results. The order of the
presentation will be ascending by default. In ascending order, any numeric values will be listed
first, followed by character values and then NULL values. The listing of NULL values can be
overridden using the NULLS FIRST or NULLS LAST keywords.

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

3. Can a column alias be referenced in an ORDER BY clause?


ANSWER: Yes

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.

2. Discuss issues regarding the handling of multiple 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.

You might also like