Lesson02 Part2
Lesson02 Part2
Lesson Agenda
2 - 19
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
9 OR logical condition
2 - 20
Rules of Precedence
The rules of precedence determine the order in which expressions are evaluated and calculated. The
table in the slide lists the default order of precedence. However, you can override the default order by
using parentheses around the expressions that you want to calculate first.
2 - 21
1-1
Quiz2
1-2
Quiz3
1-3
Quiz4
1-4
Lesson Agenda
2 - 22
…
2 - 23
2 - 24
Sorting
The default sort order is ascending:
• Numeric values are displayed with the lowest values first (for example, 1 to 999).
• Date values are displayed with the earliest value first (for example, 01-JAN-92 before
01-JAN-95).
• Character values are displayed in the alphabetical order (for example, “A” first and “Z” last).
• Null values are displayed last for ascending sequences and first for descending sequences.
• You can also sort by a column that is not in the SELECT list.
Examples:
1. To reverse the order in which the rows are displayed, specify the DESC keyword after the
column name in the ORDER BY clause. The example in the slide sorts the result by the most
recently hired employee.
2. You can also use a column alias in the ORDER BY clause. The slide example sorts the data by
annual salary.
Note: The DESC keyword used here for sorting in descending order should not be confused with the
DESC keyword used to describe table structures.
2 - 25
Sorting (continued)
Examples:
3. You can sort query results by specifying the numeric position of the column in the SELECT
clause. The example in the slide sorts the result by the department_id as this column is at
the third position in the SELECT clause.
4. You can sort query results by more than one column. The sort limit is the number of columns in
the given table. In the ORDER BY clause, specify the columns and separate the column names
using commas. If you want to reverse the order of a column, specify DESC after its name. The
result of the query example shown in the slide is sorted by department_id in ascending order and
also by salary in descending order.
You can specify the row_limiting_clause in the SQL SELECT statement by placing it
after the ORDER BY clause. Note that an ORDER BY clause is not required.
OFFSET: Use this clause to specify the number of rows to skip before row limiting begins. The
value for offset must be a number. If you specify a negative number, offset is treated as 0. If
you
specify NULL or a number greater than or equal to the number of rows that are returned by the
query, 0 rows are returned.
ROW | ROWS: Use these keywords interchangeably. They are provided for semantic clarity.
FETCH: Use this clause to specify the number of rows or percentage of rows to return.
FIRST | NEXT: Use these keywords interchangeably. They are provided for semantic
clarity.
row_count | percent PERCENT: Use row_count to specify the number of rows to return. Use
percent PERCENT to specify the percentage of the total number of selected rows to return.
The value for percent must be a number.
ONLY | WITH TIES: Specify ONLY to return exactly the specified number of rows or
percentage of rows. Specify WITH TIES to return all rows that have the same sort keys as
the last row of the row-limited result set (WITH TIES requires an ORDER BY clause).
The first code example returns the five employees with the lowest employee_id.
The second code example returns the five employees with the next set of lowest
employee_id.
Note: If employee_id is assigned sequentially by the date when the employee joined the
organization, these examples give us the top 5 employees and then employees 6-10, all in
terms of seniority.
1-6
Quiz7
1-7
Lesson Agenda
2 - 26
... salary = ? …
… department_id = ? …
... last_name = ? ...
I want
to query
different
values.
2 - 27
Substitution Variables
So far, all the SQL statements were executed with predetermined columns, conditions, and their
values. Suppose that you want a query that lists the employees with various jobs and not just those
whose job_ID is SA_REP. You can edit the WHERE clause to provide a different value each time
you run the command, but there is also an easier way.
By using a substitution variable in place of the exact values in the WHERE clause, you can run the
same query for different values.
You can create reports that prompt users to supply their own values to restrict the range of data
returned, by using substitution variables. You can embed substitution variables in a command file or
in a single SQL statement. A variable can be thought of as a container in which values are
temporarily stored. When the statement is run, the stored value is substituted.
2 - 28
2 - 29
The example in the slide creates a SQL Developer substitution variable for an employee number.
When the statement is executed, SQL Developer prompts the user for an employee number and then
displays the employee number, last name, salary, and department number for that employee.
With the single ampersand, the user is prompted every time the command is executed if the variable
does not exist.
2 - 30
2 - 31
2 - 32
…
2 - 33
2 - 34
UNDEFINE employee_num
2 - 35
2 - 36
2 - 37
Answers: 1, 2, 3, 6
2 - 38
Summary
In this lesson, you should have learned about restricting and sorting rows that are returned by the
SELECT statement. You should also have learned how to implement various operators and
conditions.
By using the substitution variables, you can add flexibility to your SQL statements. This enables the
queries to prompt for the filter condition for the rows during run time.
2 - 39
Practice 2: Overview
In this practice, you build more reports, including statements that use the WHERE clause and the
ORDER BY clause. You make the SQL statements more reusable and generic by including the
ampersand substitution.