Home > Articles > Data > SQL

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

The Where Clause

The where clause is used to choose which rows of data you want to retrieve. Because a table can have thousands of rows, this clause must be flexible enough to specify many different conditions. This makes it more complex than the other clauses we examine in this chapter.

2-8 Overview of the where clause

The where clause specifies a condition that is true for all the rows you want in the result table. For all other rows the condition is false or unknown. The following table summarizes the conditions you can use. All of these conditions can be used with any of the main types of data — text, numbers, and dates.

Each condition has both a positive form and a negative form. The negative form is always the exact opposite of the positive form. For example, the is not null condition is true for every row for which the is null condition is false. And the not between condition is true for every row where the between condition is false.

Comparison conditions that can be used in the where clause.

Condition

Meaning

Examples

EQUAL — and other comparison tests

=

equal

with numbers: credit_limit = 25.00

 

 

with text: first_name = 'sue'

 

 

with dates: hire_date = '01-jun-2010'

<

less than

credit_limit < 25.00

<=

less than or equal

first_name <= 'm'

>

greater than

hire_date > '01-jan-2010'

>=

greater than or equal

credit_limit >= 30.00

<> and others

not equal

first_name <> 'alice'

SET INCLUSION TEST — a list of specific values

in

in a set

credit_limit in (15.00, 25.00)

not in

not in a set

dept_code not in ('exe', 'mkt', 'act')

RANGE TEST — anywhere between two values

between

in a range

credit_limit between 21.00 and 27.00

not between

not within a range

dept_code not between 'act' and 'sal'

PATTERN MATCHING TEST — using wildcard characters

like

matches a pattern

phone_number like '%48%'

not like

does not match a pattern

dept_code not like '%a%'

NULL TEST — find nulls

is null

is a null value

manager_id is null

is not null

is not a null value

manager_id is not null

BOOLEAN CONNECTORS — joining simple conditions together

and

both of the conditions are true

(credit_limit = 25.00)

and (first_name = 'sue')

or

one of the conditions is true

(credit_limit = 25.00)

or (first_name = 'sue')

not

the condition is false

not (credit_limit = 25.00)


2-9 Using an Equal condition in the where clause

This section shows a query in which the where clause uses an Equal (=) condition. All the rows from the beginning table that have manager_id values equal to 203 are shown in the result table.

Note that the employees who have a null value in the manager_id column are not shown. This affects employees 201 and 206. The null value means that the value is missing in the database. The value could be equal to 203, but we do not know this, so the row for the employee is not shown in the result table.

Task

For all employees who report to employee 203, Martha Woods, list the following:

employee_id
first_name
last_name
manager_id 

Oracle & Access SQL

select employee_id,  
    first_name,
    last_name,
    manager_id
from l_employees
where manager_id = 203; 

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Notes

The select clause lists four columns, and the result table shows these four columns.

The where clause contains only one condition:

manager_id = 203

Three rows of the beginning table satisfy this condition, and the result table shows all these rows.

Task: Variation

The task is the same as the preceding one, except include the rows for employees 201 and 206, which have a null value in the manager_id column.

Oracle & Access SQL:

Variation — Include nulls

select employee_id,
    first_name,
    last_name,
    manager_id
from l_employees
where manager_id = 203
  or manager_id is null; 

Result table: Variation

Result table: Variation

Notes

Adding this line includes the rows that have a null value in the manager_id column.

2-10 Using a Less Than condition in the where clause

This section shows an example of a query that uses a Less Than (<) condition in the where clause. If there were rows with a null value in the credit_limit column, they would not be included in the result table.

Task

List all employees who have a credit limit less than $17.50. Show the columns:

employee_id
first_name
last_name
credit_limit  

Oracle & Access SQL

select employee_id,
    first_name,
    last_name,
    credit_limit
from l_employees
where credit_limit < 17.50; 

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Notes

The where clause contains only one condition:

where credit_limit < 17.50

This condition uses the less than (<) sign. The numeric value in the SQL code, 17.50, cannot contain a dollar sign or a comma. This can be confusing because often dollar signs and commas are displayed when you see the data in a table. The beginning table has two rows that satisfy this condition. The result table shows those two rows.

Task: Variation

Show another way to write this query, using the greater than or equal to (>=) sign and negating the condition with a Boolean not.

Oracle & Access SQL:

Variation

select employee_id,
    first_name,
    last_name,
    credit_limit
from l_employees
where not (credit_limit >= 17.50); 

Result table: Variation — Same as above

Notes

This is another way to write the Less Than condition.

2-11 Using a Not Equal condition in the where clause

This section shows an example of a query that uses a Not Equal condition in its where clause.

Most SQL products support several ways to write the Not Equal condition. Unfortunately, some of the ways that work in one product may not work in another product. I prefer the method shown here because it works in all products and it is easy for both people and computers to understand.

When possible, it is best to avoid using a Not Equal condition because it is much less efficient for the computer to process than conditions such as Equal (=) or between.

Task

List all employees who do not report to employee 203, Martha Woods. Show the following columns:

employee_id
first_name
last_name
manager_id

Oracle & Access SQL

select employee_id,
    first_name,
    last_name,
    manager_id
from l_employees
where not (manager_id = 203); 

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Notes

The Boolean not reverses the meaning of the condition that follows it. It only applies to that one condition. Here it changes the Equal condition into the Not Equal condition.

Variations

Some other ways to write the Not Equal condition are

where manager_id <> 203
where not manager_id = 203
where manager_id != 203
where manager_id ^= 203

You might find these variations in code you inherit, or you might prefer to use some of them yourself.

SQL uses 3-valued logic

The result table in this section does not show the rows that have a null value in the manager_id column. To show all the rows from the beginning table, we need to consider three different conditions:

where manager_id = 203
where not (manager_id = 203)
where manager_id is null

This is an example of what is meant when people say SQL uses 3-valued logic.

2-12 Using the in condition in the where clause

This section shows an example of a query that uses an in condition in its where clause. The in condition is used to show membership in a set. It is used when there is a list of discrete values that satisfy the condition. The set of all these valid values is placed in parentheses as a comma-delimited list.

All the values must have the same datatype — numbers, text, or dates. All the values can be numbers, or they can all be text, or they can all be dates. It does not make sense to mix these categories. More specifically, the values must have the same datatype as the column being tested.

It would not make sense to include null in the list of valid values because the in condition is never satisfied by a null in the data.

Sometimes in production code an in condition checks for 10 to 50 different values. In this situation it is much more efficient to write the code using an in condition rather than many Equal conditions. The examples in this book do not show this efficiency because they check for only two or three values.

Task

List all employees who report to employees 202 or 203, Jim Kern or Martha Woods. Show the following columns:

employee_id
first_name
last_name
manager_id

Oracle & Access SQL

select employee_id,
    first_name,
    last_name,
    manager_id
from l_employees
where manager_id in (202, 203); 

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Notes

This condition means that the manager_id column is equal to either 202 or 203.

Oracle & Access variation:

Using Equal conditions

Show another way to write the same query. Use two Equal conditions combined together with a Boolean or.

select employee_id,
    first_name,
    last_name,
    manager_id
from l_employees
where manager_id = 202
   or manager_id = 203;

Notes

You must repeat the column name, manager_id, within each Equal condition.

Result table: Variation — Same as above

2-13 Using the between condition in the where clause

This section shows an example of a query that uses the between condition in its where clause. Note that the end points, August 16, 1999, and July 1, 2003, are both included in the result table. Some people prefer not to use the between condition with dates because a date can also contain a time, which can create some confusion.

The between condition can be applied to numbers, text, and dates. In this example, it is applied to dates. In Oracle, dates must be enclosed in single quotes (''). In Access, they must be enclosed in pound signs (##). That is the only difference between the Oracle SQL and the Access SQL in this example.

Task

List all employees hired between August 16, 1999, and July 1, 2003. Show the following columns:

employee_id
first_name
last_name
hire_date

Oracle SQL

select employee_id,
    first_name,
    last_name,
    hire_date
from l_employees
where hire_date between '16-aug-1999'
                    and '01-jul-2003';

Access SQL

select employee_id,
    first_name,
    last_name,
    hire_date
from l_employees
where hire_date between #16-aug-1999#
                    and #01-jul-2003#;

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Variation:

Using an in condition

Write the same query as in the preceding task with an in condition. This requires you to write about 1,400 dates and demonstrates the usefulness of the between condition. Even when the code can be written in another way, the code is more compact and less prone to errors when the between condition is used.

Oracle SQL

select employee_id,
    first_name,
    last_name,
    hire_date
from l_employees
where hire_date in ('16-aug-1999',
          '17-aug-1999',
          '18-aug-1999',

(about 1,400 more dates)

          '29-jun-2003',
          '30-jun-2003',
          '01-jul-2003');

Access SQL

select employee_id,
    first_name,
    last_name,
    hire_date
from l_employees
where hire_date in (#16-aug-1999#,
          #17-aug-1999#,
          #18-aug-1999#,

(about 1,400 more dates)

          #29-jun-2003#,
          #30-jun-2003#,
          #01-jul-2003#);

Result table: Variation — Same as above

Notes on the dates in this variation

Actually, these two methods of writing the code are not quite equivalent. A date in SQL always includes a time, although often the time is not shown when the data is displayed. With the SQL code using the between condition, all the times of all the dates are included. But with the code using the in condition, the time must be midnight on the dates listed. Between always specifies a range and in always specifies a series of points.

2-14 Using the like condition in the where clause

This section shows an example of a query that uses the like condition in its where clause. The like condition is used for finding patterns in the data. Patterns are specified using wildcard characters, which are used only with the like condition. When the same characters are used with another condition, such as the between condition, they are no longer wildcards. A column of any of the major datatypes — text, number, or date — can be searched with a pattern. Case sensitivity is often an issue, but here I have turned it off. For details, see section 3-13.

In both Oracle and Access SQL, the pattern specification should be enclosed in single quotes. Patterns are specified differently in Oracle than they are in Access. Access allows a greater variety of patterns than Oracle. The wildcard characters are different. These wildcard characters are shown in the following table.

Wildcard characters and their meanings.

Oracle

Access

Meaning

% (percent sign)

* (asterisk)

A string of characters of any length, or possibly no characters at all (a zero-length string).

_ (underscore)

? (question mark)

One character.

(not available)

# (pound sign)

One digit (numeric character).

(not available)

[c-m] (square brackets with a dash)

Range of characters.

(not available)

[!c-m]

Outside a range of characters.

\% or \_

(backslash)

[*] or [?] or [#]

(square brackets)

In Access, putting a character in square brackets means to take it literally, rather than using it as a wildcard character.


The following table shows some examples of patterns.

Examples of wildcard patterns.

Pattern

Oracle

Access

Examples

Text string beginning with an n

'n%'

'n*'

'none'

'n123'

'No credit'

'n'

Four characters ending with an e

'_ _ _ e'

'???e'

'none'

'123e'

'1 3e'

Starting with a letter between a and g, followed by two digits

(not available)

'[a-g]##'

'a47'

'b82'


Notes

Sometimes this code can be used: 'c' <= value and 'm' > value

Sometimes this code can be used: 'c' > value or 'm' <= value

In Oracle, you can set up the backslash to be an Escape character. Any character placed after it is treated as a literal value rather than given a special meaning. To activate the backslash as an Escape character, use the SQLplus command

set escape \;

Task

List all employees who have the letter n in their last name. Show the following columns:

employee_id
first_name
last_name

Oracle SQL

select employee_id,
    first_name,
    last_name
from l_employees
where last_name like '%n%';

Access SQL

select employee_id,
    first_name,
    last_name
from l_employees
where last_name like '*n*';

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

2-15 Using the is null condition in the where clause

This section shows an example of a query that uses an is null condition in its where clause. A null value is used to show where data is missing in the database tables.

Note that you must write this condition "is null," rather than "= null." This is to remind you that a null is missing data and it is not like any other value in the table, because it does not have a particular value.

Nulls receive special treatment in several situations within a database. Throughout this book I point out when they are treated differently from other data.

Task

List all employees who have a null in the manager_id column. Show the following columns:

employee_id
first_name
last_name
manager_id

Oracle & Access SQL

select employee_id,
    first_name,
    last_name,
    manager_id
from l_employees
where manager_id is null;

Beginning table (l_employees table)

Beginning table (l_employees table)

Result table

Result table

Why databases use nulls

Before nulls were invented, computer systems often used spaces or special values, such as 99, to designate that data was missing. This caused two problems.

One problem was a lack of uniformity. Each computer system used different values to designate missing data. Often a single application used three of these special values: one for numbers, one for text, and one for date fields.

The special values for numbers were often all 9s, but one application might use 999, whereas another used 999999. Sometimes the various fields within a single application would use different numbers of digits.

The special values for text were often spaces. However, some applications used a single space. Others would fill the field with spaces. The computer would not always consider these to be equal. Some applications even used a zero-length string, which just confused things even more.

For date fields, January 1, 1900, often designated missing data, but some applications used other dates.

The second problem was that these special data values were sometimes processed as if they were actual data. This could lead to errors that were difficult to detect, particularly if some calculation was done that changed the values of these fields.

To solve these problems, nulls were created to designate missing data. A rigid distinction is made between nulls and other types of data. Nulls do not have datatypes, meaning there is no distinction between a null in a numeric column and one in a text or date column.

  • + Share This
  • 🔖 Save To Your Account