Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab4-SELECT statement basic features.doc
Скачиваний:
22
Добавлен:
20.02.2016
Размер:
347.14 Кб
Скачать
      1. Comparison Operators

Comparison operators compare one expression with another. The result of such a comparison can be TRUE, FALSE, or UNKNOWN. For information on conditions, see Appendix A of Lab2. The table bellow lists SQL comparison operators.

Operator

Purpose

Example

=

Equality test

SELECT * FROM DEPARTMENT

WHERE Fund = 15000;

!=, ^=, <>, ¬=

Inequality test.

SELECT * FROM DEPARTMENT

WHERE Fund != 15000;

>, <

"Greater than" and "less than" tests

SELECT * FROM FACULTY SELECT * FROM FACULTY

WHERE Fund > 1500; WHERE Fund < 1500;

>=, <=

"Greater than or equal to" and "less than or equal to" tests

SELECT * FROM FACULTY SELECT * FROM FACULTY

WHERE Fund >= 1500; WHERE Fund <= 1500;

IN

"Equal to any member of" test. Equivalent to "= ANY"

SELECT Name, Post FROM TEACHER WHERE Post IN ('professor', 'assistant');

NOT IN

Equivalent to !=ALL. Evaluates to FALSE if any member of the set is NULL

SELECT * FROM FACULTY

WHERE Fund NOT IN

(SELECT Fund FROM FACULTY WHERE Building IN ('3','5')};

ANY, SOME

Compares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Evaluates to FALSE if the query returns no rows.

SELECT * FROM TEACHER

WHERE Salary = ANY

(SELECT Salary FROM TEACHER

WHERE Post = 'professor');

ALL

Compares a value to every value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Evaluates to TRUE if the query returns no rows.

SELECT * FROM TEACHER

WHERE Salary >= ALL (350, 400, 420);

[NOT] BETWEEN x AND y

[Not] greater than or equal to x and less than or equal to y.

SELECT * FROM TEACHER

WHERE Salary BETWEEN 350 AND 420;

EXISTS

TRUE if a subquery returns at least one row. It is unary operator.

SELECT Name FROM TEACHER

WHERE EXISTS

(SELECT * FROM LECTURE

WHERE LECTURE.TchNo=TEACHER. TchNo);

x [NOT] LIKE y [ESCAPE ‘z’]

TRUE if x does [not] match the pattern y. Within y, the character "%" matches any string of zero or more characters except null. The character "_" matches any single character. Any character, excepting percent (%) and underbar (_) may follow ESCAPE. A wildcard character is treated as a literal if preceded by the character designated as the escape character.

SELECT Name FROM TEACHER

WHERE Name LIKE 'Ив%';

IS [NOT] NULL

Tests for nulls. This is the only operator that you should use to test for nulls.

SELECT Name, Tel

FROM TEACHER

WHERE Commission IS NULL;

      1. Logical Operators

A logical operator combines the results of two component conditions to produce a single result based on them or to invert the result of a single condition. Table that follows lists logical operators.

Operator

Purpose

Example

NOT

Unary operator. Returns TRUE if the condition is FALSE. Returns FALSE if it is TRUE. If it isUNKNOWN, it remainsUNKNOWN.

SELECT * FROM TEACHER

WHERE NOT (Salary BETWEEN 450 AND 500);

AND

Binary operator. Returns TRUE if both com­po­nent conditions are TRUE. Returns FALSE if either is FALSE. Otherwise returnsUNKNOWN.

SELECT * FROM TEACHER

WHERE Hiredate < TO_DATE('01-JAN-2000', 'DD-MMM-YYYY') AND Salary > 500;

OR

Binary operator. Returns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE. Otherwise returnsUNKNOWN.

SELECT *

FROM TEACHER

WHERE Post = 'assistant' AND

Salary > 500;

Logical operators have the following meaning:

AND

TRUE

FALSE

UNKNOWN

TRUE

TRUE

FALSE

UNKNOWN

FALSE

FALSE

FALSE

FALSE

UNKNOWN

UNKNOWN

FALSE

UNKNOWN

OR

TRUE

FALSE

UNKNOWN

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

UNKNOWN

UNKNOWN

TRUE

UNKNOWN

UNKNOWN

NOT

TRUE

FALSE

UNKNOWN

FALSE

TRUE

UNKNOWN

Соседние файлы в папке English