Restricting and Sorting Data
Restricting and Sorting Data
“retrieve all
employees
in department 90”
Limiting the Rows Selected
Restrict the rows returned by using the WHERE
clause.
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
The WHERE clause follows the FROM clause.
Using the WHERE Clause
Operator Meaning
= Equal to
Operator Meaning
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.
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
Using the LIKE Condition
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 the
actual % and _ symbols.
Using the NULL Conditions
Operator Meaning
…
Sorting in Descending Order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
…
Sorting by Column Alias
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
…
Sorting by Multiple Columns
The order of ORDER BY list is the order of sort.
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
You can sort by a column that is not in the