Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
all labs.pdf
Скачиваний:
74
Добавлен:
25.02.2016
Размер:
2.04 Mб
Скачать

Using the NULL Conditions

Test for nulls with the IS NULL operator.

SELECT last_name, manager_id

FROM employees

WHERE manager_id IS NULL ;

Copyright © 2009, Oracle. All rights reserved.

Using the NULL Conditions

Academy

 

The NULL conditions include the IS NULL condition and the IS NOT NULL condition.

The IS NULL condition tests for nulls. A null value me ns that the value is unavailable, unassigned, unknown, or inapplicable. Therefore, you cannot test with =, because a null cannot be equal or

unequal to any value. The slide example retrieves the last names and managers of all employees who

do not have a manager.

Oracle

&

 

 

Here is another example: To display the ast name, job ID, a d commission for all employees who

are not entitled to receive a commission, use the followingOnlySQL statement:

SELECT

last name, job id, commission_pct

FROM

employ s

 

WHERE

commission pct IS NULL;

 

 

Use

Internal

Oracle

 

 

Oracle Database 11g: SQL Fundamentals I 2 - 14

Three logical operators are available in SQL:

Defining Conditions Using the Logical Operators

Operator

Meaning

 

 

AND

Returns TRUE if both component conditions

 

are true

 

 

OR

Returns TRUE if either component condition

 

is true

 

 

NOT

Returns TRUE if the condition is false

 

 

Copyright © 2009, Oracle. All rights reserved.

Academy

Defining Conditions Using the Logical Operators

A logical condition combines the result of two componentOracleconditions to produce a single result based

on those conditions or it inverts the result of a single condition. A row is returned only if the overall result of the condition is true.

AND

&

Only

NOT

OR

 

 

Use

Internal

 

All the examples so far have specified only one condition in the WHERE clause. You can use several

conditions in a single WHERE clause using the AND and OR operators.

Oracle

 

 

Oracle Database 11g: SQL Fundamentals I 2 - 15

Using the AND Operator

AND requires both the component conditions to be true:

SELECT

employee_id, last_name, job_id, salary

FROM

employees

WHERE

salary >= 10000

 

AND

job_id LIKE '%MAN%'

;

 

 

 

Copyright © 2009, Oracle. All rights reserved.

Using the AND Operator

Academy

 

In the example, both the component conditions must be true for any r cord to be selected. Therefore,

only those employees who have a job title that contains the string ‘MAN’ and earn $10,000 or more

are selected.

 

 

 

 

 

 

All character searches are case-sensitive, that is no rows are returned if ‘MAN’ is not uppercase.

 

 

 

 

Oracle

 

 

Further, character strings must be enclosed with quotation marks.

 

 

AND Truth Table

 

&

Only

 

 

 

 

 

 

 

 

 

The following table shows the results of combining two expressions with AND:

 

AND

TRUE

 

FALSE

NULL

 

 

 

 

UseFALSE

NULL

 

 

TRUE

TRUE

 

 

FALSE

FALSE

 

FALSE

FALSE

 

 

 

Internal

 

 

 

 

 

NULL

NULL

 

FALSE

NULL

 

 

Oracle

 

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 2 - 16

Using the OR Operator

OR requires either component condition to be true:

SELECT

employee_id, last_name, job_id, salary

FROM

employees

WHERE

salary >= 10000

 

OR

job_id LIKE '%MAN%'

;

 

 

 

 

 

 

 

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

 

 

 

Using the OR Operator

 

 

Oracle

 

 

 

 

 

 

 

In the example, either component condition can be true for any r cord to be selected. Therefore, any

 

employee who has a job ID that contains the string ‘MAN’ or earns $10,000 or more is selected.

 

OR Truth Table

 

 

 

 

 

 

 

 

The following table shows the results of combining two expressions with OR:

 

 

 

 

 

&

Only

 

 

 

 

 

OR

 

TRUE

 

FALSE

 

NULL

 

 

 

 

Internal

 

 

 

 

 

 

TRUE

 

TRUE

 

TRUE

 

TRUE

 

 

 

 

 

 

Use

 

 

 

 

 

FALSE

 

TRUE

 

FALSE

 

NULL

 

 

 

NULL

 

TRUE

 

NULL

 

NULL

 

Oracle

 

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 2 - 17

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]