Java Reference
In-Depth Information
Using the OR operator
The
OR
operator is used to combine two or more comparisons, any one of which can evaluate to
TRUE
for the comparison to be valid. For example, to find all records in the Contact_Info table who live in
New York City or in New Jersey, you would use this query:
SELECT * FROM Contact_Info WHERE City = 'New York' OR State = 'NJ';
Combining logical operators using parentheses
Like arithmetic operators, logical operators can be combined using parentheses (()). For example, to
find all to find all records in the Contact_Info table with a last name of Corleone who live in New York
City or in New Jersey, you would use this query:
SELECT * FROM Contact_Info
WHERE Last_Name = 'Corleone' AND ( City = 'New York' OR State = 'NJ' );
Using the NOT operator
The
NOT
operator is used to reverse the result of a comparison. If the condition it applies to evaluates
to
TRUE
, using the
NOT
operator makes it
FALSE
. Conversely, if the condition after the
NOT
is
FALSE
, it
becomes
TRUE
when you use the
NOT
operator. For example, to find all to find all records in the
Contact_Info table with a last name of Corleone who do not live in New York City or in New Jersey, you
would use this query:
SELECT * FROM Contact_Info
WHERE Last_Name = 'Corleone' AND NOT ( City = 'New York' OR State = 'NJ' );
Arithmetic Operators
SQL supports the common arithmetic operators for addition (+), subtraction (-), multiplication (*), and
division ( / ). In addition, SQL supports the modulo operator (%), which returns the remainder from the
division of one integer by another.
Note
The modulo operator only works with integers. Dividing a float by a valid divisor always
gives a float, and thus, no remainder.
Table 7-3
shows a very simple inventory. This inventory will be used in discussing how to work with
arithmetic operators.
Table 7-3: Inventory
ID
Name
Description
Qty
Cost
1001
Corn Flakes
Cereal
130
1.95
1002
Rice Krispies
Cereal
97
1.87
1003
Shredded Wheat
Cereal
103
2.05
1004
Oatmeal
Cereal
15
0.98