Limiting selected rows

advertisement
Limiting Selected Rows
Objectives
•
•
Sort row output using the ORDER BY clause.
Enter search criteria using the WHERE clause.
2-2
The ORDER BY Clause
Sort rows with the ORDER BY clause.
• ASC – ascending order, default.
• DESC – descending order.
• ORDER BY clause is last in SELECT command.
SELECT
FROM
ORDER BY
2-3
last_name, dept_id, start_date
s_emp
last_name;
The ORDER BY Clause
•
•
•
The default sort order is ascending.
The sort order can be reversed by using DESC.
You can sort by expressions or aliases.
SELECT
last_name EMPLOYEE, start_date
FROM
s_emp
ORDER BY EMPLOYEE DESC;
• Null values are displayed
– Last for ascending sequences.
– First for descending sequences.
2-4
Sorting by Multiple Columns
SELECT
last_name, dept_id, salary
FROM
s_emp
ORDER BY dept_id, salary DESC;
• The order of ORDER BY list is order of sort.
• You can sort by a column that is not in the
SELECT list.
2-5
Limiting Rows Selected
Restrict the rows returned by using the WHERE
clause.
•
•
The WHERE clause follows the FROM clause.
Conditions consist of the following:
– Column name, expression, constant
– Comparison operator
SELECT
FROM
WHERE
2-6
last_name, dept_id, salary
s_emp
dept_id = 42;
Character Strings and Dates
•
Character strings and dates are enclosed within
single quotation marks.
•
Number values are not enclosed within
quotation marks.
•
•
Character values are case-sensitive.
The default date format is 'DD-MON-YY'.
SELECT first_name, last_name, title
FROM
s_emp
WHERE last_name = 'Magee';
2-7
Comparison and Logical Operators
•
Logical comparison operators
= > >= < <=
•
SQL comparison operators
– BETWEEN ... AND...
– IN(list)
– LIKE
– IS NULL
•
Logical operators
– AND
– OR
– NOT
2-8
Negating Expressions
Sometimes it is easier to exclude rows you know you
do not want.
•
Logical Operators
!= <> ^=
•
SQL Operators
– NOT BETWEEN
– NOT IN
– NOT LIKE
– IS NOT NULL
2-9
BETWEEN and IN SQL Operators
•
Use the BETWEEN operator to test for values
between, and inclusive of, a range of values.
SELECT first_name, last_name, start_date
FROM
s_emp
WHERE start_date BETWEEN '09-may-91'
AND '17-jun-91';
• Use IN to test for values in a list.
SELECT
FROM
WHERE
2-10
id, name, region_id
s_dept
region_id IN (1,3);
LIKE SQL Operator
•
You can use the LIKE operator to perform
wildcard searches of valid search string values.
•
Search conditions can contain either literal
characters or numbers.
– "%" denotes none or many characters.
– "_" denotes one character.
SELECT
FROM
WHERE
2-11
last_name
s_emp
last_name LIKE 'M%';
LIKE SQL Operator
•
The LIKE operator can be used as a shortcut for
some BETWEEN comparisons.
SELECT
FROM
WHERE
•
You can combine pattern matching characters.
SELECT
FROM
WHERE
•
2-12
last_name, start_date
s_emp
start_date LIKE '%91';
last_name
s_emp
last_name LIKE '_a%';
You can use the ESCAPE identifier to search for "%"
or "_".
IS NULL SQL Operator
•
•
Test for null values with the IS NULL operator.
Do not use the = operator.
SELECT
FROM
WHERE
2-13
id, name, credit_rating
s_customer
sales_rep_id IS NULL;
Multiple Conditions
•
•
•
Use complex criteria.
Combine conditions with AND or OR operators.
AND requires both conditions to be TRUE.
SELECT last_name, salary, dept_id, title
FROM
s_emp
WHERE
dept_id = 41
AND title = 'Stock Clerk';
• OR requires either condition to be TRUE.
SELECT last_name, salary, dept_id, title
FROM
s_emp
WHERE dept_id = 41
2-14
OR title = 'Stock Clerk';
Rules of Precedence
Override rules of precedence by using
parentheses.
Order Evaluated
2-15
Operator
1
All comparison operators.
2
AND
3
OR
Rules of Precedence: Examples
•
Display information for those employees in department
44 who earn 1000 or more, and any employees in
department 42.
SELECT
FROM
WHERE
last_name, salary, dept_id
s_emp
salary >= 1000 AND dept_id = 44
OR dept_id = 42;
• Display information for those employees in department 44
or 42 who earn 1000 or more.
SELECT
FROM
WHERE
2-16
last_name, salary, dept_id
s_emp
salary >= 1000 AND
(dept_id = 44 OR dept_id = 42);
Download