oracle sql: part 2
TRANSCRIPT
4. RESTRICTING AND SORTING DATA .
OBJECTIVES
After completing this module, you will be able to
1. Restrict the rows that are retrieved by a query.
2. Properly arrange the rows that are retrieved by a query.
A simple SELECT statement would display all the rows in the specified columns of the stated table.
However, when you want to limit or restrict the rows to be returned by a SELECT statement, you use the WHERE keyword.
WHERE
The WHERE keyword is used to place a condition on a certain column such that only the rows that satisfy that condition are returned by the accompanying SELECT statement.
EXAMPLE:
This query returns only the employees that earn a salary of 17,000.
FORMAT:
SELECT * FROM table_name WHERE column_name (condition);
Note that the name of the tested column is stated immediately after the WHERE keyword followed by a conditional operator like equal to, greater than or less than.
A WHERE clause can be accompanied by other conditions which are either stated using the IN or LIKE or BETWEEN clause.
Where these conditions are used, there must be enclosed in brackets.
THE IN CONDITION
The IN condition -also referred to as the membership condition- is used to specify a number of values to be tested.
FORMAT:
SELECT * FROM table_name WHERE column_name IN (value1, value2)
The tested values are enclosed in brackets and separated by commas and only the rows that fit into one of those values are returned by the select statement.
Let’s attempt to display the employees that earn 10000, 17000 or 24000.
EXAMPLE:
Only the employees whose salary value are members of the IN condition are returned by this query.
The IN condition can be used with any data type whatsoever but where the tested value is a character string, you must remember to enclose each condition in single quotation marks.
EXAMPLE:
THE LIKE CONDITION
The LIKE condition is most often used with the character data type.
Two symbols can be used to construct the search string for the LIKE condition; first we use the percentage symbol and then the underscore.
The Percentage symbol %is used to represent any string of characters whatsoever.
FORMAT:
SELECT column_name
FROM table_name
WHERE column_name
LIKE ‘%x’
EXAMPLE:
The Oracle Server simply returns all those rows where the first_name column starts with any sequence of characters and ends with the letter a.
Therefore all the first_name displayed end with the letter a
EXAMPLE:
Query all the columns where the last name starts with the letter A and end with any sequence of characters whatsoever.
Observe from the result that all the rows displayed have the last_name starting with the letter A and ending with any sequence of character values.
The underscore _ when used in the LIKE clause, represents any single character.
Wherever we see the underscore, it represents means any single character values as opposed to the percentage % which represents a character string.
FORMAT:
SELECT column_name
FROM table_name
WHERE column_name
LIKE ‘_xyz’
EXAMPLE:
Query the employees database for those employees whose last name start with and single character and ends with ‘ee’.
EXAMPLE:
Let’s query the employees table to find the details of those employees whose last name start with any character string and end with either do, or de, or di, or d and any single character.
Observe the last_name column of all the rows returned by this query.
EXAMPLE :
Query the employees table for those employees whose last_name starts with any single character, has the letter o in the second space and ends with any sequence of characters.
NULL
A NULL is a value that is not assigned, not available, or inapplicable. It is not the same as a zero or a blank space.
If a row lacks value for any particular column, that column is said to contain a NULL..
A NULL condition is dictated using the IS NULL clause in the WHERE statement.
FORMAT:
SELECT *
FROM table_name
WHERE column_name IS NULL;
EXAMPLE:
Query the employees table for data of those employees without a manager_id.
From the result that only one employee is returned and he is not that employee whose manager_id is zero, neither is he that employee whose manager_id is a blank space.
Rather he is that staff who does not have a manager_id, because he is the General Manager.
EXAMPLE:
Query the employees table for employees without a commission percentage.
FORMAT:
SELECT *
FROM table_name
WHERE column_name NOT NULL
LOGICAL CONDITIONS
Logical conditions are used to place more than one restriction on a table.
EXAMPLE:
Query the employees table for those employees whose salaries are above 15000 and do not earn a commission percentage.
All the employees returned have no commission_pct and a salary > 15000.
Three logical conditions are used in the WHERE clause of a SELECT statement; AND, OR and NOT.
THE AND CONDITION
The AND displays only the row that satisfy all the conditions stated in the WHERE clause.
FORMAT:
SELECT *
FROM table_name
WHERE column_name (condition) AND column_name (condition);
EXAMPLE:
Query the employees table for those employees whose do not earn a commissions percentage and whose salary is above 15000.
THE OR CONDITION
The OR displays the entire rows that satisfies any of the conditions stated in the WHERE clause.
FORMAT:
SELECT *
FROM table_name
WHERE column_name (condition) OR column_name (condition);
EXAMPLE:
Query the employees table for those who either do not earn a commission percentage or whose salary is more than 15000.
THE NOT CONDITION
The NOT returns the rows that do not satisfy the conditions, stated in the WHERE clause i.e the inverse or opposite of the stated conditions.
The NOT operator can be used with other operators like NOT IN, NOT BETWEEN, NOT LIKE OR NOT NULL
FORMAT:
SELECT *
FROM table_name
WHERE column_name NOT IN (Value1, Value2);
EXAMPLE:
FORMAT:
SELECT *
FROM table_name
WHERE column_name NOT BETWEEN Value1 AND Value;
EXAMPLE:
FORMAT:
SELECT *
FROM table_name
WHERE column_name NOT LIKE Value
EXAMPLE
FORMAT:
SELECT *
FROM table_name
WHERE column_name IS NOT NULL
EXAMPLE:
SORTING
By default, there is no definite order in which the rows returned by a SELECT statement are arranged.
The ORDER BY clause is used to define the manner in which the rows displayed by the SELECT statement are arranged.
FORMAT:
SELECT column_name FROM table_name ORDER BY column_name;
EXAMPLE:
When the ORDER BY clause is introduced to the SELECT statement, all the rows returned by the query block are displayed based on the stated column
EXAMPLE
In the above example, the first_name column becomes the column that determines how the rows in other columns are displayed.
In this case, the rows where the first_name starts with ‘A’ come first, and the rows where the first_name starts with ‘Z’ come last.
RULES
1. Numeric data are sorted from the lowest value to the highest.
2. Date values are ordered by the earliest date first.
3. Alphabets are ordered from A to Z.
EXAMPLE:
Write a SELECT statement to display all the rows in the jobs table according to their min_salary .
This default order, could however be reversed, using the ASC and DESC keyword. As a matter of principle, the ORDER BY clause; must be the last clause, in the SELEECT statement.