- •Laboratory work 4
- •Description and examples
- •The Building Blocks of Data Retrieval: select and from clauses
- •The select clause
- •The froMclause
- •The where clause
- •Use of simple conditions
- •Use of comparison operators
- •Logical operators.
- •Column expressions in where clause
- •Special operators
- •Operator in
- •Operator between
- •Operator like
- •Operators is null and is not null.
- •Oracle Lab tasks
- •Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 6
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 17
- •Вариант 18
- •Control questions
- •Appendices
- •Appendix a. Sql Oracle operators
- •Unary and binary operators
- •Precedence
- •Arithmetic Operators
- •Concatenation Operator
- •Comparison Operators
- •Logical Operators
- •Set Operators
- •Appendix b. Expressions
- •Simple Expressions
- •Cursor Expressions
- •Object Access Expressions
- •Decode Expressions
- •Expression List
- •Appendix c. Commentsin sql
The where clause
The WHERE close allows to set condition that result rows must satisfy. We already consider how to use WHERE clause to join two tables.
Use of simple conditions
Use of comparison operators
Example 1: Select all professors:
SELECT Name AS List_of_all_professors
FROM TEACHER
WHERE Post = 'professor';
professor is a string literal that is why it enclosed in quotation marks.
Example 2: List all faculties with fund more than 30000:
SELECT Name
FROM FACULTY
WHERE Fund > 300000;
Here Fund is a numeric field that is why 30000 does not enclosed in quotation marks.
Logical operators.
Example 3 – Logical AND: List faculties from building 5 with funds mode 20000:
SELECT Name
FROM FACULTY
WHERE Building ='5' AND Fund < 200000;
Example 4 – Logical OR: List departments in building 7 or building 3:
SELECT Name
FROM DEPARTMENT
WHERE Building ='7' OR Building ='3';
Example 5 – Logical NOT: List of all faculties that are not “informatics”:
SELECT Name
FROM FACULTY
WHERE NOT Name ='informatics';
Example 6. Combining logical operators:List assistants with salary less than 150 or Commission more that 100:
SELECT Name
FROM TEACHER
WHERE Post ='assistant' AND ( Salary < 150 OR Commission > 100 );
Column expressions in where clause
Example 7. Column expression: Select teachers whose salary plus Commission exceeds 300:
SELECT Name
FROM TEACHER
WHERE Salary + Commission > 300;
Special operators
Operator in
Operator IN. This operator test inclusion of right hand argument into the set defined by second argument.
Example 8. Operator IN:Select lecture types that are taught in Monday, Tuesday or Wednesday:
SELECT Type
FROM LECTURE
WHERE Day IN ('Mon', 'Tue', 'Wed');
Any IN operator with set literal of the right hand argument may be represented with OR operator. For example the previous query has the following equivalent representation:
SELECT Type
FROM LECTURE
WHERE Day = 'Mon' OR Day = 'Tue' OR Day = 'Wed');
IN operator also equivalent to "=ANY" operator (see later).
NOT IN operator. Operator NOT IN is negation of IN operator.
Example 9. NOT IN operator:List of faculties that are located in all buildings except 1, 7, 8, 11:
SELECT Name
FROM FACULTY
WHERE Building NOT IN ('1', '7', '8', '10');
Right hand operand of [NOT] IN may be a subquery. This possibility will be studied in Lab ???.
NULLS in NOT IN operator. If any item in the list following a NOT IN operation is null, all rows evaluate to UNKNOWN (and no rows are returned). For example, the following statement returns faculties that are not located in building 1 or 4:
SELECT Name
FROM FACULTY
WHERE Building NOT IN ( '1', '4' );
However, the following statement returns no rows:
SELECT Name
FROM FACULTY
WHERE Building NOT IN ( '1', '4' );
The above example returns no rows because the WHERE clause condition evaluates to:
Num != 5 AND Num != 15 AND Num != null
Because all conditions that compare a null result in a null, the entire expression results in a null. This behaviour can easily be overlooked, especially when the NOT IN operator references a subquery.
Operator between
BETWEEN operator. Operator evaluates to TRUE if left hand argument in between the interval of two values of right hand argument. The two values of right hand argument are included into the interval.
Example 10. BETWEEN operator: List teachers with salary in interval 150 -350:
SELECT Name
FROM TEACHER
WHERE Salary BETWEEN 150.00 AND 350.00;
BETWEEN operator have equivalent notation. For example the following query is equivalent to the previous one:
SELECT Name
FROM TEACHER
WHERE Salary >= 150.00 AND Salary <= 350.00;
NOT BETWEEN operator. NOT BETWEEN operator is negation of BETWEEN operator.
Example 11. NOT BETWEEN operator: List of departments that have funds not between 20000-50000:
SELECT Name
FROM DEPARTMENT
WHERE Fund NOT BETWEEN 20000.00 AND 50000.00;
SELECT Name
FROM DEPARTMENT
WHERE Fund < 20000.00 AND Fund > 50000.00;
As you can see values 20000 and 50000 are not included into NOT BETWEEN operator.
NULL values in [NOT] BETWEEN. If left hand argument is NULL the operator evaluates to UNKNOWN and the row does not satisfy this condition.