oracle sql: part 2

49

Upload: digital-experts

Post on 06-Jan-2017

129 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Oracle SQL: Part 2
Page 2: Oracle SQL: Part 2

4. RESTRICTING AND SORTING DATA .

Page 3: Oracle SQL: Part 2

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.

Page 4: Oracle SQL: Part 2

A simple SELECT statement would display all the rows in the specified columns of the stated table.

Page 5: Oracle SQL: Part 2
Page 6: Oracle SQL: Part 2

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.

Page 7: Oracle SQL: Part 2

EXAMPLE:

This query returns only the employees that earn a salary of 17,000.

FORMAT:

SELECT * FROM table_name WHERE column_name (condition);

Page 8: Oracle SQL: Part 2

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.

Page 9: Oracle SQL: Part 2

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)

Page 10: Oracle SQL: Part 2

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.

Page 11: Oracle SQL: Part 2

EXAMPLE:

Only the employees whose salary value are members of the IN condition are returned by this query.

Page 12: Oracle SQL: Part 2

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:

Page 13: Oracle SQL: Part 2

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.

Page 14: Oracle SQL: Part 2

FORMAT:

SELECT column_name

FROM table_name

WHERE column_name

LIKE ‘%x’

Page 15: Oracle SQL: Part 2

EXAMPLE:

Page 16: Oracle SQL: Part 2

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

Page 17: Oracle SQL: Part 2

EXAMPLE:

Query all the columns where the last name starts with the letter A and end with any sequence of characters whatsoever.

Page 18: Oracle SQL: Part 2

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.

Page 19: Oracle SQL: Part 2

FORMAT:

SELECT column_name

FROM table_name

WHERE column_name

LIKE ‘_xyz’

Page 20: Oracle SQL: Part 2

EXAMPLE:

Query the employees database for those employees whose last name start with and single character and ends with ‘ee’.

Page 21: Oracle SQL: Part 2

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.

Page 22: Oracle SQL: Part 2

Observe the last_name column of all the rows returned by this query.

Page 23: Oracle SQL: Part 2

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.

Page 24: Oracle SQL: Part 2

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.

Page 25: Oracle SQL: Part 2

FORMAT:

SELECT *

FROM table_name

WHERE column_name IS NULL;

Page 26: Oracle SQL: Part 2

EXAMPLE:

Query the employees table for data of those employees without a manager_id.

Page 27: Oracle SQL: Part 2

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.

Page 28: Oracle SQL: Part 2

EXAMPLE:

Query the employees table for employees without a commission percentage.

Page 29: Oracle SQL: Part 2

FORMAT:

SELECT *

FROM table_name

WHERE column_name NOT NULL

Page 30: Oracle SQL: Part 2

LOGICAL CONDITIONS

Logical conditions are used to place more than one restriction on a table.

Page 31: Oracle SQL: Part 2

EXAMPLE:

Query the employees table for those employees whose salaries are above 15000 and do not earn a commission percentage.

Page 32: Oracle SQL: Part 2

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.

Page 33: Oracle SQL: Part 2

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);

Page 34: Oracle SQL: Part 2

EXAMPLE:

Query the employees table for those employees whose do not earn a commissions percentage and whose salary is above 15000.

Page 35: Oracle SQL: Part 2

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);

Page 36: Oracle SQL: Part 2

EXAMPLE:

Query the employees table for those who either do not earn a commission percentage or whose salary is more than 15000.

Page 37: Oracle SQL: Part 2

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);

Page 38: Oracle SQL: Part 2

EXAMPLE:

Page 39: Oracle SQL: Part 2

FORMAT:

SELECT *

FROM table_name

WHERE column_name NOT BETWEEN Value1 AND Value;

Page 40: Oracle SQL: Part 2

EXAMPLE:

Page 41: Oracle SQL: Part 2

FORMAT:

SELECT *

FROM table_name

WHERE column_name NOT LIKE Value

Page 42: Oracle SQL: Part 2

EXAMPLE

Page 43: Oracle SQL: Part 2

FORMAT:

SELECT *

FROM table_name

WHERE column_name IS NOT NULL

Page 44: Oracle SQL: Part 2

EXAMPLE:

Page 45: Oracle SQL: Part 2

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.

Page 46: Oracle SQL: Part 2

FORMAT:

SELECT column_name FROM table_name ORDER BY column_name;

EXAMPLE:

Page 47: Oracle SQL: Part 2

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.

Page 48: Oracle SQL: Part 2

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.

Page 49: Oracle SQL: Part 2

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.