SQL : Restriction

Restrictions
Objectives of the Lecture :
•To consider the algebraic Restrict operator;
•To consider the Restrict operator and its comparators in SQL.
Example of Restriction (1)
R
A
4
4
B
5
5
C
1
2
D
4
8
A
4
6
4
3
4
6
R Restrict[ B = 5 ]
B
5
3
8
9
5
1
C
1
2
1
7
2
2
D
4
7
5
6
8
2
Example of Restriction (2)
R
A
6
4
6
B
3
5
1
C
2
2
2
D
7
8
2
A
4
6
4
3
4
6
R Restrict[ B < D ]
B
5
3
8
9
5
1
C
1
2
1
7
2
2
D
4
7
5
6
8
2
Definition of ‘Restrict’
1. Creates a new relation formed from tuples of the operand
relation.
2. A boolean condition provided as a parameter is used to
decide which of the operand’s tuples should appear in the
result :
 The condition is applied to every tuple in the operand.
 IF the condition is true for a tuple
THEN that tuple is copied into the result
ELSE that tuple is not copied into the result
3. The result’s attribute names are those of the operand.
Restriction Conditions

Each tuple is evaluated against the condition in isolation
from all the other tuples in the operand.

The condition can :
 compare any attribute in the tuple with :
• any constant value (e.g. 3, ‘Julie’),
• any other attribute value of the same type in the
same tuple;
 have any kind of comparison allowed by that
attribute’s data type;
 be built up of a Boolean combination of
comparisons, i.e. using AND, OR and NOT.

Calculations can also be incorporated into comparisons.
Example : Salary / 3 < 500 + (2 * Bonus )
Boundary Cases


IF the restriction condition is true for all the operand’s tuples
THEN the result will be the same as the operand.
IF the restriction condition is false for all the tuples
THEN the result will be an empty relation,
i.e. a relation having no tuples.
Note : a condition containing one or more ORs in it is more likely
to evaluate to true, because only one of the individual
conditions needs to evaluate to true, so that as a consequence
the result is likely to be larger.
Conversely, a condition with one or more ANDs in it is more
likely to evaluate to false, leading to a smaller result.
SQL : Restriction
The SQL equivalent of a Restriction has the syntax :Select
*
From
RELATION_NAME
Where
condition ;
Principles :
 Put Select * to get all the attributes in the result.
 Put the relation name in the From phrase.
 Put the required condition in the Where phrase.
The SQL statement also retrieves the result from the DB.
SQL fulfils all the requirements of the Restrict operation.
SQL Examples
The 2 previous examples are written in SQL as follows :
Select *
From R
Where B = 5 ;
Select *
From R
Where B < D ;
Two more examples that include boolean operators in the condition :



Select *
From R
Where B = 5 Or D <> 4 ;
Select *
From R
Where B > 5 And B < D ;
Note the SQL
“” symbol.
Available Comparators in SQL
All the standard comparators and operators are available for use.


Every type must have the “=“ and “<>” comparators.
Many types also have :
<
<=
>
>=
For these comparators to be valid, the type must either be
numeric or orderable, i.e. it is possible to sort any set of
values of that type into an order.
Examples : alphabetic types can be sorted into alphabetic order;
date-times can be sorted into a temporal order.
Comparisons with NULL

SQL provides a special facility for use in the Where phrase to
check whether an attribute value is NULL :X Is NULL
The result is true or false, depending on whether X is Null (or
not).
One can also check whether X is not Null :X Is Not NULL

Let X and Y be attributes of the same data type.
Consider the comparison
X = Y
Suppose either of X and Y are NULL (or both are NULL )
The result is maybe not true.
Boolean Operators
Multiple comparisons can be combined into one Restriction
condition using And, Or and Not in the usual way.
Example :Where comparison1 And
Not comparison2 Or comparison3 ;
 The comparisons are combined in a left-to-right order, except
that Not has a higher priority than And and Or.
 Parentheses can be used to alter the order in which
comparisons are combined.
Example :Where comparison1 And Not
(comparison2 Or comparison3 ) ;

Different to the
previous example.
LIKE (1)
The purpose of Like is to match character strings. It is much more
powerful for this than “=” and “<>”.
 The syntax of Like is :
AttributeName Like ‘pattern’
 The type of AttributeName must be character string, although it
can be a string of any length.
 pattern is made up of character constants and/or one or more of
the following wildcard characters :
% : represents zero, one or more occurrences of any character(s);
_ (underscore) : represents any character, but just one character.
 Like returns true when an attribute value matches the pattern, and
false otherwise.
Example : Where Name Like ‘Sm_th%’ ;
would find any ‘Smith’ or ‘Smythe’, including those which are the
first part of a double-barrelled name.
LIKE (2)

If a pattern is required that needs % or _ in it, then there
is a problem in constructing the pattern.

The problem is solved by having an escape character which
prefixes the % or _ in the pattern and designates that %
or _ as standing for itself instead of a wildcard.
Example :
Where Discount Like ‘%=%’ Escape ‘=’ ;
would find any character string ending in %. The “=“ is
designated an escape character, so that the second % in the
pattern stands for itself while the first one represents a
wildcard.

The full syntax is :
AttributeName Like ‘pattern’ Escape ‘character’
The Escape clause is optional.
IN
The purpose of In is to make it easier to compare an attribute
value with a set of values.
 The syntax of In is :
AttributeName In ( set_of_values )
 AttributeName can be of any type.
 set_of_values consists of a list of the set of values, with
values being separated by commas.
 In returns true when an attribute value is equal to one of
those in set_of_values, and false otherwise.
Example : Where Number In ( 3, 5, 7, 19 ) ;
would find any of the numbers 3, 5, 7 or 19.
 Att In ( v1, v2, v3 )
is a logical shorthand for
( Att = v1 ) Or ( Att = v2 ) Or ( Att = v3)

BETWEEN
The purpose of Between is to make it easier to determine
whether an attribute value lies within a given range of values.
 The syntax of Between is :
AttributeName Between value1 And value2
 AttributeName must be a numeric or orderable type.
 Let value1 and value2 denote the minimum and maximum
values respectively of the range.
Between returns true when an attribute value lies in the range
value1 to value2 inclusive, and false otherwise.
Example : Where Number Between 10 And 50 ;
would find any number in the range 10 to 50.
 Att Between value1 And value2
is a logical shorthand for
( Att >= value1 ) And ( Att <= value2 )

Negating the Special Comparators

It is possible to have the negative of each of these
comparators.

The negatives are :
 AttributeName Not Like ‘pattern’
 AttributeName Not In ( set_of_values )
 AttributeName Not Between value And value
1
2

Note that this is in addition to being able to use the Boolean
Not in front of the comparator expression in the usual way.
The following are logically equivalent to the above :
 Not AttributeName Like ‘pattern’
 Not AttributeName In ( set_of_values )
 Not AttributeName Between value And value
1
2
Combining ‘Restrict’ & ‘Project’

By the nature of relational algebra, we could apply both operators
to a relation R in either of the following ways :
 R Project[ AttributeNames ] Restrict[ condition ]
 R Restrict[ condition ] Project[ AttributeNames ]

In SQL, we apply both operators by writing them in the standard
SQL set of phrases, as follows :
Select AttributeNames
From R
Where condition ;

As SQL has its own in-built sequence of operations, in fact it will
do the Restrict first and the Project second.