Home > Articles > Data > SQL

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020