Upload
ecomputernotes
View
220
Download
0
Embed Size (px)
Citation preview
8/3/2019 e computer notes - Restricting and Sorting Data
1/27
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
2/27
Restricting and Sorting Data
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
3/27
Objectives
After completing this lesson, you should be able todo the following:
" Limit the rows retrieved by a query"Sort the rows retrieved by a query
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
4/27
8/3/2019 e computer notes - Restricting and Sorting Data
5/27
Limiting the Rows Selected
" Restrict the rows returned by using theWHEREclause.
SELECT *|{[DISTINCT] column|expression [alias],...}
FRO M table
[WHERE condition(s)];
" TheWHEREclause follows theFROM clause.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
6/27
Using theWHEREClause
SELECT employee_id, last_name, job_id, department_idFROM employeesWHERE department_id = 90 ;
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
7/27
Character Strings and Dates
"Character strings and date values are enclosed insingle quotation marks.
"Character values are case sensitive, and datevalues are format sensitive.
"The default date format is DD-MON-RR.
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen';
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
8/27
Comparison Conditions
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
8/3/2019 e computer notes - Restricting and Sorting Data
9/27
Using Comparison Conditions
SELECT last_name, salary
FROM employees
WHERE salary
8/3/2019 e computer notes - Restricting and Sorting Data
10/27
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
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
11/27
Using theBETWEENCondition
Use theBETWEENcondition to display rows based ona range of values.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
Lower limit Upper limit
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
12/27
Using theINCondition
Use theINmembership condition to test for values ina list.
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201);
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
13/27
Using theLIKECondition
" Use theLIKEcondition to perform wildcardsearches of valid search string values.
" Search conditions can contain either literalcharacters or numbers:
%denotes zero or many characters.
_ denotes one character.
SELECT first_name
FRO M employees
WHERE first_name LIKE 'S%';
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
14/27
Using theLIKECondition
"You can combine pattern-matching characters.SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
"You can use the ESCAPE identifier to search for theactual%and_symbols.
8/3/2019 e computer notes - Restricting and Sorting Data
15/27
Using theNULLConditions
Test for nulls with theIS NULLoperator.
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL;
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
16/27
Logical Conditions
MeaningOperator
ReturnsTRUEifbothcomponentAND
conditions are true
ORReturnsTRUEifeithercomponent
condition is true
NOT ReturnsTRUEif the following
condition is false
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
17/27
Using theANDOperator
ANDrequires both conditions to be true.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000 AND
job_id LIKE '%MAN%';
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
18/27
Using theOROperator
ORrequires either condition to be true.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000 ORjob_id LIKE '%MAN%';
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
19/27
Using theNOTOperator
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
20/27
Rules of Precedence
Order Evaluated Operator
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL,LIKE,[NOT]
5 [NOT] BETWEEN
6 NOTlogical condition
7 ANDlogical condition
8 ORlogical condition
Override rules of precedence by using parentheses.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/8/3/2019 e computer notes - Restricting and Sorting Data
21/27
Rules of Precedence
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;
8/3/2019 e computer notes - Restricting and Sorting Data
22/27
Rules of Precedence
Use parentheses to force priority.
SELECT last_name, job_id, salary
FROM employees
WHERE (job_id ='SA_REP' OR job_id ='AD_PRES')AND salary > 15000;
8/3/2019 e computer notes - Restricting and Sorting Data
23/27
ORDER BYClause
"Sort rows with the ORDER BY clauseASC: ascending order, default
DESC: descending order
" TheORDER BYclause comes last in theSELECTstatement.
SELECT last_name, job_id, department_id,hire_date FROM employees
ORDER BY hire_date ;
8/3/2019 e computer notes - Restricting and Sorting Data
24/27
Sorting in Descending Order
SELECT last_name, job_id, department_id,hire_date FROM employeesORDER BY hire_date DESC ;
8/3/2019 e computer notes - Restricting and Sorting Data
25/27
Sorting by Column Alias
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
8/3/2019 e computer notes - Restricting and Sorting Data
26/27
Sorting by Multiple Columns
" The order ofORDER BYlist is the order of sort.SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
8/3/2019 e computer notes - Restricting and Sorting Data
27/27