Databases Reference
In-Depth Information
SELECT CUSTNUM, CUSTNAME, HQCITY
FROM CUSTOMER
WHERE HQCITY='New York'
OR CUSTNUM > 1500;
results in:
CUSTNUM
CUSTNAME
HQCITY
0121
Main St. Hardware
New York
1525
Fred's Tool Stores
Atlanta
1700
XYZ Stores
Washington
1826
City Hardware
New York
2198
Western Hardware
New York
2267
Central Stores
New York
Notice that the OR operator really means one or the other or both .Customer
0121 is included because it is headquartered in New York. Customers 1525 and 1700
are included because they have customer numbers higher than 1500. Customers
1826, 2198, and 2267 are included because they satisfy both conditions.
Both AND and OR What if both AND and OR are specified in the same WHERE
clause? AND is said to be ''higher in precedence'' than OR, and so all ANDs are
considered before any ORs are considered. The following query, which has to be
worded very carefully, illustrates this point:
''List the customer numbers, customer names, and headquarters cities of the
customers that are headquartered in New York or that satisfy the two conditions
of having a customer number higher than 1500 and being headquartered in
Atlanta.''
SELECT CUSTNUM, CUSTNAME, HQCITY
FROM CUSTOMER
WHERE HQCITY='New York'
OR CUSTNUM > 1500
AND HQCITY='Atlanta';
The result of this query is:
CUSTNUM
CUSTNAME
HQCITY
0121
Main St. Hardware
New York
1525
Fred's Tool Stores
Atlanta
1826
City Hardware
New York
2198
Western Hardware
New York
2267
Central Stores
New York
Notice that since the AND is considered first , one way for a row to qualify
for the result is if its customer number is greater than 1500 and its headquarters city
is Atlanta. With the AND taken first, it's that combination or the headquarters city
has to be New York. Considering the OR operator first would change the whole
Search WWH ::




Custom Search