Unit 3 - Learn To Restrict and Sort Data
Unit 3 - Learn To Restrict and Sort Data
In the example, the SELECT statement retrieves the employee ID, last
name, job ID, and department number of all employees who are in
department 90.
• Character strings and date values are enclosed with single quotation
marks
• Character values are case-sensitive and date values are format-
sensitive
• The default date display format is DD-MON-YY.
SELECT last_name
FROM employees
WHERE hire_date = '17-OCT-03';
• OPERATORS:
• = Equal to
• > Greater than
• >= Greater than or equal to
• < Less than
• <= Less than or equal to
• <>, !=, ^= Not equal to
• BETWEEN … AND … Between two values (inclusive)
• IN(set) Match any of a list of values
• LIKE Match a character pattern
• IS NULL Is a null value
• NOT Negation of a condition
• You can use the ESCAPE identifier to search for the actual % and _
symbols
• The ESCAPE identifies the backslash (\) as the escape character. This
causes the Oracle server to interpret the underscore literally
• The NULL conditions include the IS NULL condition and the IS NOT
NULL condition
Example: Display the last name, job ID, and commission for all employees
who don’t have a commission
• You can use several conditions in a single WHERE clause using the
AND and OR operators
• This example displays the last name and job ID of all employees whose
job ID is not IT_PROG, ST_CLERK, or SA_REP
• The NOT operator can also be used with other SQL operators, such as
BETWEEN, LIKE, and NULL
Restrict and Sort Data
Agenda
• Limiting rows with:
• The WHERE clause
• The comparison operators using =, <=,
BETWEEN, IN, LIKE, and NULL conditions
• Logical conditions using AND, OR, and NOT
operators
• Rules of precedence for operators in an expression
• Sorting rows using the ORDER BY clause
• SQL Row limiting clause in a query
• Substitution variables
• DEFINE and VERIFY commands
• You can override the default order by using parentheses around the
expressions that you want to calculate first
Syntax
SELECT expr
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, numeric_position} [ASC|DESC]];
Syntax:
...
[ order_by_clause ]
[OFFSET offset { ROW | ROWS }]
[FETCH { FIRST | NEXT } [{ row_count | percent PERCENT
}] { ROW | ROWS }
{ ONLY | WITH TIES }]
• With the single ampersand, the user is prompted every time the
command is executed if the variable does not exist
• SQL Developer stores the value that is supplied by using the DEFINE
command. After a user variable is in place, you need to use the
UNDEFINE command to delete it: UNDEFINE column_name;
UNDEFINE employee_num;
SET VERIFY ON
SELECT employee_id, last_name, salary
FROM employees
WHERE employee_id = &employee_num;
• Which of the following are not valid operators for the WHERE clause?.
a. >=
b. IS NULL
c. !=
d. IS LIKE
e. IN BETWEEN
f. <>
• Selecting data and changing the order of the rows that are displayed