You are on page 1of 6

Chapter 7 - Using WHERE

CHAPTER 7 - USING WHERE


This chapter shows how you can use WHERE to get exactly the records you want from a query.

7.1

Using Comparisons

You can use the following symbols to make comparisons:


Operator

What it means

Operator

What it means

>

is greater than

<

is less than..

>= or !<

greater than or equal to

<= or !>

is less than or equal to

is equal to

<> or !=

is not equal to

Comparisons work with numbers, text and dates here are some examples:
Type

Comparison example

Numeric

Show any films winning 10 or more


Oscars (there are two: Titanic and The
Return of the King).

Date

Show all directors born in 1964, by


showing those directors who were born
on or after 1st January 1964 and on or
before 31st December 1964. Note that
this isnt necessarily the best way to do
this.

Text

Show all directors whose gender isnt


Male (it might have been easier to
show all directors whose gender was
Female!).

Wise
Wise Owls
Owls
Hint
Hint

Copyright 2016

SQL

Text comparisons work in the same way as a


telephone directory or dictionary. Thus the
query on the right will include (for example)
Never Say Never Again, since this would come
after N in the dictionary.

Page 39

Chapter 7 - Using WHERE

7.2

Using Logical Operators

You can use and combine the following operators in SQL:


Operator

What it means

AND

Both conditions have to be true

OR

Either or both of two conditions can be true

NOT

Returns the opposite of a particular value

These conditions are often interchangeable. The


queries below would both show long films (over
120 minutes) which have won 1 Oscar exactly.
The 6 films which
meet both criteria.

A simple version of the query


both conditions have to be true.

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

Chapter 7 - Using WHERE

7.3

Using Wildcards

There are two main wildcards in SQL:

Character

What it represents

Any character

_ (underscore character)

Any single character

Both wildcards have to be used with the LIKE operator. Here are two examples:
Example

SQL

Data returned

Show all films which


begin with a D

Show all films which


begin with a D and
have 8 letters

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

WHERE FilmName like 'Shrek%'

The % is an all-forgiving wildcard, and includes nothing


at all

WHERE FilmName like 'sHReK%'

SQL Server is, by default, not case-sensitive

WHERE FilmName like '%h%'

Shrek contains an h

WHERE FilmName like 'S_rek'

There is a single character between S and rek

WHERE FilmName like 'S_r%k%'

Shrek is made up of: an S followed by a single character


h followed by an r followed by an e followed by a k
followed by nothing

The answer all of them!


Wise
Wise Owls
Owls
Hint
Hint

Copyright 2016

Page 41

Chapter 7 - Using WHERE

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

Show all actors born in the 1980s (this will


include any actors born on 1st January 1980
and any actors born on 31st December 1989)

Show all actors whose names come in the first


half of the alphabet (strictly speaking, this
would also show someone called N too)

Using IN
Suppose that we want to show all films made by the
studios shown highlighted on the right:

We want to show all films made


by studios 3, 8 and 10.

You could do this using the IN operator:


Show all films where the studio
id lies in this (short) list.

The results of this query: films


with a Disney connection.

Copyright 2016

Page 42

Chapter 7 - Using WHERE

7.5

Filtering Out Null Values

You can use the following criteria to either show or hide null values:
Criteria

What it shows

IS NULL

All records where a particular field value is empty


All records where a particular field value is not empty

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:

This table has films where no


certificate has been input.

This query would


omit these films.

Entering Null into a Table


If you want to clear the field in a table, press

Ctrl

+ 0:
If you want to get rid of
Tom Cruises date of
birth

just press + 0.

Copyright 2016

Page 43

Chapter 7 - Using WHERE

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

Returns a 4-character code describing


the sound of a string of text.

SOUNDEX('Cabaret')

C163

DIFFERENCE

Rather more usefully, returns the


difference between two sounds as a
number between 0 (no similarity) and 4
(no phonetic 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.

These functions sometimes yield surprising results:


Its possible to believe that War of the Worlds
sounds a bit like Wise Owl, but Das Boot?

Copyright 2016

Page 44

You might also like