Lec2 Restricting and Sorting Data
Lec2 Restricting and Sorting Data
KEYWORD MEANING
WHERE restricts the query to rows that meet a
condition
⚫ Column name
⚫ Comparison condition
⚫ Column name, constant, or list of values
Using the WHERE Clause
Character Strings and Dates
⚫ Character strings and date values are enclosed in
single quotation marks
⚫ Character values are case sensitive, and date values are
format sensitive
⚫ The default date format is DD-MON-RR
Character Strings and Dates(Contd..)
NOTE: The symbol != and ^= can also represent the not equal to condition.
Other Comparison Conditions
OPERATOR MEANING
BETWEEN Between two values (inclusive)
...AND...
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
Using the BETWEEN Condition
Use the BETWEEN condition to display rows based on a
range of values.
Values specified with the BETWEEN condition are inclusive. You must
specify the lower limit first.
Using the IN Condition
Use the IN membership condition to test for values in a
list.
Using the IN Condition(Contd..)
The IN condition can be used with any data type. The
following example returns a row from the EMPLOYEES
table for any employee whose last name is included in the
list of names in the WHERE clause:
You can use several conditions in one WHERE clause using the AND and OR
operators.
Using the AND Operator
AND requires both conditions to be true.
Using the OR Operator
OR requires either condition to be true.
Using the NOT Operator
SELECT
Rules of Precedence
Rules of Precedence
2
AND (department_id = 50
2
OR department_id = 20);
AND
is working in department 50 OR department 20 2
ORDER BY Clause
⚫ Sort rows with the ORDER BY clause
A 1
⚫ – ASC: ascending order (the default order) Z 9
• Date values are displayed with the earliest value first: for
example, 01-JAN-92 before 01-JAN-95
1 2 3 4
SELECT employee_id, first_name, last_name, department_id
FROM employees
ORDER BY 2 ;
…
Sorting by Multiple Columns
The order of ORDER BY list is the order of sort.
⚫ You can also order by columns that are not included in the
SELECT clause.
Example
Display the last names and salaries of all employees. Order
the result by department number, and then in
descending order by salary.