Beginning SQL

Beginning SQL Beginning SQL

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

State DateOfJoining New State 2005-11-21 New State 2005-08-22 Golden State 2005-08-27 Golden State 2005-09-02 However, you won’t see the preceding results, because the AND operator has a higher precedence than the OR operator, which is found in the WHERE clause of the SQL statement: WHERE State = ‘New State’ OR State = ‘Golden State’ AND DateOfJoining >= ‘2005-08-01’; The WHERE clause is actually evaluated like this: Is the State column equal to Golden State AND is the DateOfJoining on or after August 1, 2005, OR is the State column equal to New State? This interpretation gives quite different results: State DateOfJoining New State 2004-02-22 New State 2005-01-02 New State 2005-01-02 New State 2005-11-21 New State 2005-08-22 Golden State 2005-08-27 Golden State 2005-09-02 If you want the database to provide a list of members in New State or Mega State who joined on or after August 1, 2005, then clearly the query results are wrong. To solve this problem, use brackets to increase the precedence of the operators inside them, just as you would in math to differentiate between (1 + 1) _ 2 and 1 + 1 _ 2. Brackets are right at the top of the precedence hierarchy, so they are always evaluated first. If you add brackets to the SQL statement, the condition inside the brackets is evaluated first: SELECT State, DateOfJoining FROM MemberDetails WHERE (State = ‘New State’ OR State = ‘Golden State’) AND DateOfJoining >= ‘2005-08-01’; Extracting Information Because brackets are the highest precedence, the SQL statement now reads as follows: State is equal to New State OR Golden State, AND the DateOfJoining must be greater than or equal to August 1, 2005. 63

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 />

However, you won’t see the preceding results, because the AND operator has a higher precedence than<br />

the OR operator, which is found in the WHERE clause of the <strong>SQL</strong> statement:<br />

WHERE State = ‘New State’ OR State = ‘Golden State’<br />

AND DateOfJoining >= ‘2005-08-01’;<br />

The WHERE clause is actually evaluated like this: Is the State column equal to Golden State AND is the<br />

DateOfJoining on or after August 1, 2005, OR is the State column equal to New State?<br />

This interpretation gives quite different results:<br />

State DateOfJoining<br />

New State 2004-02-22<br />

New State 2005-01-02<br />

New State 2005-01-02<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 />

If you want the database to provide a list of members in New State or Mega State who joined on or after<br />

August 1, 2005, then clearly the query results are wrong. To solve this problem, use brackets to increase<br />

the precedence of the operators inside them, just as you would in math to differentiate between (1 + 1) _ 2<br />

and 1 + 1 _ 2. Brackets are right at the top of the precedence hierarchy, so they are always evaluated first.<br />

If you add brackets to the <strong>SQL</strong> statement, the condition inside the brackets is evaluated first:<br />

SELECT State, DateOfJoining<br />

FROM MemberDetails<br />

WHERE (State = ‘New State’ OR State = ‘Golden State’)<br />

AND DateOfJoining >= ‘2005-08-01’;<br />

Extracting Information<br />

Because brackets are the highest precedence, the <strong>SQL</strong> statement now reads as follows: State is equal to<br />

New State OR Golden State, AND the DateOfJoining must be greater than or equal to August 1, 2005.<br />

63

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

Saved successfully!

Ooh no, something went wrong!