Using the WHERE Clause

Một phần của tài liệu Beginning C# 2005 Databases From Novice to Professional phần 2 pot (Trang 22 - 26)

Queries can haveWHEREclauses. The WHEREclause allows you to specify criteria for select- ing rows. This clause can be complex, but we’ll stick to a simple example for now. The syntax for our example is

WHERE <column1> <operator> <column2>

where <operator>is a comparison operator (for example, =,<>,>, or <). See Table 3-1, which lists the T-SQL comparison operators.

Try It Out: Refining Your Query

To refine your query:

1. Add the following WHEREclause to the query in Figure 3-5:

where

country = 'USA'

2. Run the query by pressing F5, and you should see results as in Figure 3-6.

C H A P T E R 3 ■ I N T R O D U C I N G S Q L 48

Figure 3-5.Selecting specific columns

Caution SQL keywords and table and column names aren’t case sensitive, but string literals (enclosed in single quotes) are. So, use 'USA', not 'usa', for this example.

How It Works

The new query means: Return the data for columns EmployeeID,FirstName, and LastName from the Employeestable, but only for rows where the Countrycolumn equals USA.

Using Comparison Operators in a WHERE Clause

You can use a number of different comparison operators in a WHEREclause (see Table 3-1).

C H A P T E R 3 ■ I N T R O D U C I N G S Q L 49

Figure 3-6.Using aWHEREclause

Table 3-1.Comparison Operators

Operator Description Example

= Equals EmployeeID = 1

< Less than EmployeeID < 1

> Greater than EmployeeID > 1

<= Less than or equal to EmployeeID <= 1

>= Greater than or equal to EmployeeID >= 1=

<>,!= Not equal to EmployeeID <> 1

!< Not less than EmployeeID !< 1

!> Not greater than EmployeeID !> 1

Tip As mentioned earlier, every database vendor has its own implementation of SQL. This discussion is specific to T-SQL; for example, standard SQL doesn’t have the !=operator and calls <>the not equals oper- ator. In fact, standard SQL calls the expressions in a WHEREclause predicates; we’ll use that term because predicates are either true or false, but other expressions don’t have to be. If you work with another version of SQL, please refer to its documentation for specifics.

In addition to these operators, the LIKEoperator (see Table 3-2) allows you to match patterns in character data. As with all SQL character data, strings must be enclosed in single quotes (').

Table 3-2.The LIKEOperator

Operator Description Example

LIKE Allows you to specify a pattern WHERE Title LIKE 'Sales%'selects all rows where the Titlecolumn contains a value that starts with the wordSales followed by zero or more

characters.

You can use four different wildcards in the pattern (see Table 3-3).

C H A P T E R 3 ■ I N T R O D U C I N G S Q L 50

Table 3-3.Wildcard Characters

Wildcard Description

% Any combination of characters.

_ Any one character. WHERE Title LIKE '_ales'selects all rows where the Title column equals Aales, aales, Bales, bales, and so on.

[ ] A single character within a range [a-d]or set [abcd].WHERE Title LIKE '[bs]ales' selects all rows where the Titlecolumn equals either the word bales or sales.

[^] A single character not within a range [^a-d]or set [^abcd].

Sometimes it’s useful to select rows where a value is unknown. When no value has been assigned to a column, the column is NULL. (This isn’t the same as a column that con- tains the value 0or a blank.) To select a row with a column that’s NULL, use the IS NULL operator (see Table 3-4).

Table 3-4.The IS [NOT] NULLOperator

Operator Description Example

IS NULL Allows you to select rows where WHERE Region IS NULLreturns all rows where a column has no value. Regionhas no value.

IS NOT NULL Allows you to select rows where WHERE Region IS NOT NULLreturns all rows a column has a value. whereRegionhas a value.

Note You must use the IS NULLand IS NOT NULLoperators (collectively called the null predicate in standard SQL) to select or exclude NULLcolumn values, respectively. The following is a valid query but always produces zero rows:SELECT * FROM employees WHERE region = NULL, because nothing

"equals" NULL (not even another NULL). If you change =to IS, the query will return rows where regions have no value.

To select values in a range or in a set, you can use the BETWEENand INoperators (see Table 3-5).

Table 3-5.The BETWEENand INOperators

Operator Description Example

BETWEEN True if a value is within a range. WHERE extension BETWEEN 400 AND 500returns the rows whereExtensionis between 400 and 500, inclusive.

IN True if a value is in a list. The list WHERE city IN ('Seattle', 'London')returns can be the result of a subquery. the rows where Cityis either Seattle or London.

C H A P T E R 3 ■ I N T R O D U C I N G S Q L 51

Combining Predicates

Quite often you’ll need to use more than one predicate to filter your data. You can use the logical operators shown in Table 3-6.

Table 3-6.SQL Logical Operators

Operator Description Example

AND Combines two expressions, evalu- WHERE (title LIKE 'Sales%' AND lastname ating the complete expression as ='Peacock')

true only if both are true.

NOT Negates a Boolean value. WHERE NOT (title LIKE 'Sales%' AND lastname

= 'Peacock')

OR Combines two expressions, evalu- WHERE (title = 'Peacock' OR title = 'King') ating the complete expression as

true if either is true.

When you use these operators, it’s often a good idea to use parentheses to clarify the conditions. In complex queries, this may be absolutely necessary.

Một phần của tài liệu Beginning C# 2005 Databases From Novice to Professional phần 2 pot (Trang 22 - 26)

Tải bản đầy đủ (PDF)

(52 trang)