Professional Documents
Culture Documents
7.1
Using Comparisons
What it means
Operator
What it means
>
is greater than
<
is less than..
>= or !<
<= or !>
is equal to
<> or !=
is not equal to
Comparisons work with numbers, text and dates here are some examples:
Type
Comparison example
Numeric
Date
Text
Wise
Wise Owls
Owls
Hint
Hint
Copyright 2016
SQL
Page 39
7.2
What it means
AND
OR
NOT
An unnecessarily
tortuous way to do the
same thing find out if it
is true that either the film
didnt win 2 Oscars or
was 2 hours or less, and
then take the opposite!
Using Brackets
Where conditions are sufficiently complex, use brackets to determine in what order they are
evaluated:
Because of the brackets, this query will show
all films with Oscar data such that either:
The number of nominations is more than
8, but the film won 3 or less Oscars;
or
The number of nominations is more than 5,
but the film won no Oscars.
Copyright 2016
Page 40
7.3
Using Wildcards
Character
What it represents
Any character
_ (underscore character)
Both wildcards have to be used with the LIKE operator. Here are two examples:
Example
SQL
Data returned
To test your knowledge of wildcards, cover up the left-hand column and see if you can guess
which of the following queries would give Shrek:
Criteria
Explanation
Shrek contains an h
Copyright 2016
Page 41
7.4
BETWEEN and IN
These two useful words allow you to either select data in a certain range or in a list of values.
Using BETWEEN
BETWEEN is inclusive at both ends, and can be used with numbers, dates and text, as shown
by these examples:
Example
SQL
Using IN
Suppose that we want to show all films made by the
studios shown highlighted on the right:
Copyright 2016
Page 42
7.5
You can use the following criteria to either show or hide null values:
Criteria
What it shows
IS NULL
Wise
Wise Owls
Owls
Hint
Hint
Nulls are a pain in SQL Server if youre not sure you know exactly how
theyll behave, the best thing to do is to exclude them from your data.
Here is an example:
Ctrl
+ 0:
If you want to get rid of
Tom Cruises date of
birth
just press + 0.
Copyright 2016
Page 43
7.6
Sounds Like
There are two functions in SQL which determine whether one string of text sounds like another:
Function
What it does
Example
Equals
SOUNDEX
SOUNDEX('Cabaret')
C163
DIFFERENCE
DIFFERENCE('Chalk','Cheese')
DIFFERENCE('Owl','Oil')
Wise
Wise Owls
Owls
Hint
Hint
These functions are most useful for finding names whose spelling youve
forgotten: for example, when you cant remember if it was Smith or
Smythe.
Copyright 2016
Page 44