20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3<br />

The statement returns these results, which are exactly what you want:<br />

State DateOfJoining<br />

New State 2005-11-21<br />

New State 2005-08-22<br />

Golden State 2005-08-27<br />

Golden State 2005-09-02<br />

Using brackets is the key to ensuring operator precedence. Additionally, brackets can make the <strong>SQL</strong> easier<br />

to read because they make it clear which conditions are evaluated first, which is quite handy if the<br />

conditions are quite complex. Otherwise you have to remember the order of operator precedence.<br />

To illustrate operator precedence, try out a more complex WHERE statement where lots of brackets are<br />

necessary. What’s required this time is a list of all the names, cities, and dates of birth of members who<br />

live in either Townsville or Big City and are either older than 60 or younger than 16. Again, assume that<br />

today’s date is May 31, 2005. Members under 16 years of age must have been born after May 31, 1989,<br />

and members over 60 years of age must have been born on or before May 31, 1945.<br />

Try It Out Increasing Operator Precedence Using Brackets<br />

1. The film club chairperson wants to know which members live in either Townsville or Big City<br />

and were born either before May 31, 1945, or after May 31, 1989. The <strong>SQL</strong> to answer this question<br />

is as follows:<br />

SELECT FirstName, LastName, City, DateOfBirth<br />

FROM MemberDetails<br />

WHERE<br />

( City = ‘Townsville’ OR City = ‘Big City’ )<br />

AND<br />

(DateOfBirth > ‘1989-05-31’ OR DateOfBirth #1989-05-31# OR DateOfBirth ‘31 May 1989’ OR DateOfBirth

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

Saved successfully!

Ooh no, something went wrong!