|
More Complex Conditions: Compound Conditions.
The AND operator joins two or more conditions, and displays a row
only if that row's data satisfies ALL conditions listed (i.e. all
conditions hold true). For example, to display all staff making over
$40,000, use:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY > 40000 AND
POSITION = 'Staff'
The OR operator joins two or more conditions, but returns a row if
ANY of the conditions listed hold true. To see all those who make
less than $40,000 or have less than $10,000 in benefits, listed
together, use the following query:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY < 40000 OR BENEFITS
< 10000
AND & OR can be combined, for example:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' AND
SALARY > 60000 OR BENEFITS > 12000
First, SQL finds the rows where the salary is greater than $60,000
and the position column is equal to Manager, then taking this new
list of rows, SQL then sees if any of these rows satisfies the
previous AND condition or the condition that the Benefits column is
greater then $12,000. Subsequently, SQL only displays this second new
list of rows, keeping in mind that anyone with Benefits over $12,000
will be included as the OR operator includes a row if either
resulting condition is True. Also note that the AND operation is done first.
To generalize this process, SQL performs the AND operation(s) to
determine the rows where the AND operation(s) hold true (remember:
all of the conditions are true), then these results are used to
compare with the OR conditions, and only display those remaining rows
where the conditions joined by the OR operator hold true.
To perform OR's before AND's, like if you wanted to see a list of
employees making a large salary (>$50,000) or have a large benefit
package (>$10,000), and that happen to be a manager, use parentheses:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' AND
(SALARY > 50000 OR BENEFITS > 10000)
Index
<< Prev
1
2
3
4
5
6
7
8
9
10
11
12
13
Next >>
|