20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The second query is a negative, in that it requires that the Street field should not end in either Road or<br />

Street. Using the NOT and LIKE operators in conjunction with one another is the obvious choice. Another<br />

obvious choice is to use the AND operator, which tells the database to search the Street field for streets<br />

that don’t end in Street and don’t end in Road.<br />

SELECT FirstName, LastName, Street<br />

FROM MemberDetails<br />

WHERE Street NOT LIKE ‘% Road’ AND Street NOT LIKE ‘% Street’;<br />

Executing the <strong>SQL</strong> provides the results you want:<br />

FirstName LastName Street<br />

John Jackson Long Lane<br />

John Jones Newish Lane<br />

Jenny Jones Newish Lane<br />

Seymour Botts Long Lane<br />

Jamie Hills Newish Lane<br />

Stuart Dales Long Lane<br />

IN Operator<br />

So far you’ve used the OR operator to check whether a column contains one of two or more values. For<br />

example, if you want to check whether a member lives in Townsville, Windy Village, Dover, or Big City,<br />

you’d write the following:<br />

SELECT City<br />

FROM MemberDetails<br />

WHERE<br />

City = ‘Townsville’<br />

OR<br />

City = ‘Windy Village’<br />

OR<br />

City = ‘Dover’<br />

OR<br />

City = ‘Big City’;<br />

That query is a bit long-winded, and that’s where the IN operator helps: it functions exactly like the OR<br />

operator but requires much less typing!<br />

Using the IN operator, you can rewrite the preceding <strong>SQL</strong> like this:<br />

SELECT City<br />

FROM MemberDetails<br />

WHERE<br />

City IN (‘Townsville’, ‘Windy Village’, ‘Dover’, ‘Big City’);<br />

Extracting Information<br />

73

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

Saved successfully!

Ooh no, something went wrong!