Beginning SQL
Beginning SQL Beginning SQL
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
- Page 114: Depending on which RDBMS you’re u
- Page 118: The FilmCategory table is very smal
- Page 122: 2 Entering Information The last cha
- Page 126: like this: INSERT INTO Category VAL
- Page 130: Inserting Data into the Case Study
- Page 134: The WHERE Clause So far you’ve se
- Page 138: Try It Out Using Logical Operators
- Page 142: Exercises 1. Three new members have
- Page 148: Chapter 3 54 MemberId FirstName 1 K
- Page 152: Chapter 3 The results are as follow
- Page 156: Chapter 3 The results to this query
- Page 160: Chapter 3 FavCategory (CategoryId,
- Page 164: Chapter 3 Logical Operators and Ope
- Page 170: It simply tells the database system
- Page 174: The BETWEEN operator functions exac
- Page 178: The final condition required is a c
- Page 182: In some database systems, the LIKE
- Page 186: The second query is a negative, in
- Page 190: Because both conditions (YearReleas
- Page 194: YearReleased 1984 1980 1977 1975 19
- Page 198: First, the database system orders t
- Page 202: LastName FirstName DateOfBirth Nigh
- Page 206: The preceding SQL returns the follo
- Page 210: If you put square brackets around i
- Page 214: Finally, if you want to use an alia
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