Restricting and Sorting Data
Restricting and Sorting Data
Objectives
After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the rows that are retrieved by a query
Restrict the rows that are returned by using the WHERE clause:
Character strings and date values are enclosed by single quotation marks. Character values are case-sensitive, and date values are format-sensitive. The default date format is DD-MON-RR.
Comparison Conditions
Operator = >
>=
< <= <>
BETWEEN ...AND...
IN(set) LIKE
IS NULL
Is a null value
Use the LIKE condition to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers:
% denotes zero or many characters. _ denotes one character.
Logical Conditions
Operator AND OR
Meaning Returns TRUE if both component conditions are true Returns TRUE if either component condition is true
NOT
SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Rules of Precedence
Operator 1
2
3 4
Concatenation operator
Comparison conditions IS [NOT] NULL, LIKE, [NOT] IN
5
6 7 8
[NOT] BETWEEN
Not equal to NOT logical condition AND logical condition
OR logical condition
Rules of Precedence
SELECT FROM WHERE OR AND last_name, job_id, salary employees job_id = 'SA_REP' job_id = 'AD_PRES' salary > 15000;
last_name, job_id, salary employees (job_id = 'SA_REP' job_id = 'AD_PRES') salary > 15000;
Sorting
SELECT last_name, job_id, department_id, hire_date FROM employees 1 ORDER BY hire_date DESC ;
SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;