Show/Hide Toolbars

Filopto Help Manual

 

Compound Conditions / Logical Operators

 

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 earning a salary over $40,000, use:

 

 SELECT EMPLOYEEIDNO

 FROM EMPLOYEESTATISTICSTABLE

 WHERE SALARY > 40000 AND Title = '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 earn 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 Title = '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 sees if any of these rows satisfies the previous AND condition, or the condition that the Benefits column is greater than $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, since 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) holds true. (Remember: all of the conditions are true.) Then these results are used to compare with the OR condition(s), and only display those remaining rows where any of the conditions joined by the OR operator hold true. (Where a condition or the result from an AND is paired with another condition, or if the AND results are used to evaluate the OR condition, the overall results will be true if either the AND or OR value is true).

 

Mathematically, SQL evaluates all of the conditions, then evaluates the AND "pairs", and then evaluates the OR (where both operators evaluate left to right).

 

As an example, for a given row for which the database management system (DBMS) is evaluating the SQL statement, the WHERE clause determines whether to include the row in the query result (the whole WHERE clause evaluates to True), the DBMS has evaluated all of the conditions, and is ready to do the logical comparison on this result:

 

 Example of WHERE condition -        True AND False OR True AND True OR False AND False

 

First simplify the AND pairs, left to right:

 False OR True OR False

 

Now do the OR's, left to right:

 True OR False

 

The answer is

 True

 

The result is True, and the row passes the query conditions. Be sure to see the next section on NOT's, and the order of logical operations. I hope that this section has helped you understand AND's or OR's, as it's a difficult subject to explain briefly.

 

To perform OR's before AND's, in the case where you want to see a list of employees earning 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 Title = 'Manager' AND (SALARY > 50000 OR BENEFITS > 10000);