Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 3 NOT Operator Examples thus far have been filtered based on true conditions. The NOT operator, however, selects a record if the condition is false. The following SQL selects records where the State field is not equal to Golden State. SELECT FirstName FROM MemberDetails WHERE NOT State = ‘Golden State’; The preceding example is the same as this example: SELECT FirstName FROM MemberDetails WHERE State ‘Golden State’; The only difference is the use of the “not equal to” operator () instead of the NOT operator. In this situation, the “not equal to” operator () reads easier than the NOT operator. You can also use the NOT operator with brackets: SELECT City FROM MemberDetails WHERE NOT (City = ‘Townsville’ OR City = ‘Orange Town’ OR City = ‘New Town’); The preceding SQL selects all records where the conditions inside the brackets are not true. In this case, the condition inside the brackets is that the City is equal to Townsville or it is equal to Orange Town or it is equal to New Town. Using the NOT operator is the same as saying “is not true,” which is the same as saying “is false.” So, you could rephrase the explanation to say that the query is looking for values that are false, that do not equal Townsville, Orange Town, or New Town. The results returned are shown here: City Big City Windy Village Windy Village Big City Big City As you see shortly, you can use the NOT operator in combination with other operators such as BETWEEN, ANY, SOME, AND, OR, or LIKE. BETWEEN Operator 66 The BETWEEN operator allows you to specify a range, where the range is between one value and another. Until now, when you needed to check for a value within a certain range, you used the “greater than or equal to” operator (>=) or the “less than or equal to” (

Chapter 3<br />

NOT Operator<br />

Examples thus far have been filtered based on true conditions. The NOT operator, however, selects a<br />

record if the condition is false. The following <strong>SQL</strong> selects records where the State field is not equal to<br />

Golden State.<br />

SELECT FirstName<br />

FROM MemberDetails<br />

WHERE NOT State = ‘Golden State’;<br />

The preceding example is the same as this example:<br />

SELECT FirstName<br />

FROM MemberDetails<br />

WHERE State ‘Golden State’;<br />

The only difference is the use of the “not equal to” operator () instead of the NOT operator. In this situation,<br />

the “not equal to” operator () reads easier than the NOT operator.<br />

You can also use the NOT operator with brackets:<br />

SELECT City<br />

FROM MemberDetails<br />

WHERE NOT (City = ‘Townsville’ OR City = ‘Orange Town’ OR City = ‘New Town’);<br />

The preceding <strong>SQL</strong> selects all records where the conditions inside the brackets are not true. In this case,<br />

the condition inside the brackets is that the City is equal to Townsville or it is equal to Orange Town or<br />

it is equal to New Town. Using the NOT operator is the same as saying “is not true,” which is the same as<br />

saying “is false.” So, you could rephrase the explanation to say that the query is looking for values that<br />

are false, that do not equal Townsville, Orange Town, or New Town. The results returned are shown<br />

here:<br />

City<br />

Big City<br />

Windy Village<br />

Windy Village<br />

Big City<br />

Big City<br />

As you see shortly, you can use the NOT operator in combination with other operators such as BETWEEN,<br />

ANY, SOME, AND, OR, or LIKE.<br />

BETWEEN Operator<br />

66<br />

The BETWEEN operator allows you to specify a range, where the range is between one value and another.<br />

Until now, when you needed to check for a value within a certain range, you used the “greater than or<br />

equal to” operator (>=) or the “less than or equal to” (

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!