WHERE clause
- Using the WHERE clause, the row does not satisfy the conditions to filter out.
Example:
SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ;
Character and Date
- Character and date to be included in single quotes.
- Character case sensitive, sensitive date format.
Example:
SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Whalen';
Comparison operation
Operators | meaning |
= | equal |
> | more than the |
>= | greater or equal to |
< | Less than |
<= | Less than or equal |
<> | It is not equal to (or may be! =) |
Use assignment: = sign
comparison operation Example:
SELECT last_name, salary FROM employees WHERE salary <= 3000;
Other comparison operations
Operators | meaning |
BETWEEN...AND... | Between two values (inclusive) |
IN(set) | Equal to the value of a list |
LIKE | Fuzzy query |
IS NULL | Null |
Use BETWEEN operator to display values within a range of
Example 1:
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;
Values using the IN operator display list
SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN ( 100, 101, 201 );
LIKE
- Similar use the LIKE operator selected value
- Selection criteria may include characters or numbers:
- % Represents zero or more characters (any characters).
- _ Represents one character.
Example:
SELECT first_name FROM employees WHERE first_name LIKE 'S%';
'%' And '-' simultaneously
Example:
SELECT last_name FROM employees WHERE last_name LIKE '_o%';
ESCAPE
- ESCAPE identifier may be used to select '%' and '_' symbol.
- Avoidance of special symbols: escaped. For example: A [%] into [\%], [_] into [\ _], then add [ESCAPE '\'] can.
Example:
SELECT job_id FROM jobs WHERE job_id LIKE 'IT\_%' ESCAPE '\';
NULL
- Use IS (NOT) NULL NULL value is determined.
SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL;
logic operation
Operators | meaning |
AND | And logic |
OR | Logical or |
NOT | No logic |
AND
- AND relationship requirements and it is true.
Example:
SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 AND job_id LIKE '%MAN%';
OR
- OR requirements or relation is true.
SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%';
NOT
- NOT this is not within
SELECT last_name, job_id FROM employees WHERE job_id NOT IN ( 'IT_PROG', 'ST_CLERK', 'SA_REP' );
priority
priority | name |
1 | Arithmetic Operators |
2 | Joiner |
3 | Comparison operators |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | NOT |
7 | AND |
8 | OR |
- Parentheses may be used to change the order of priority
ORDER BY clause
- ORDER BY clause to sort
- ASC (ascend): ascending
- DESC (descend): Descending
- The end of the ORDER BY clause in the SELECT statement.
Example:
SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date;
descending sort
SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date DESC;
Alias Sort by:
SELECT employee_id, last_name, salary * 12 annsal FROM employees ORDER BY annsal;
A plurality of column sorting: Sort the order of ORDER BY list.
SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;