Sams Teach Yourself SQL in 24 Hours

Sams Teach Yourself SQL in 24 Hours

By Ron Plew and Ryan Stephens

Conjunctive Operators

newterm_icon.gif

What if you want to use multiple conditions to narrow data in a SQL statement? You must be able to combine the conditions, and you would do this with what is call conjunctive operators. These operators are

These operators provide a means to make multiple comparisons with different operators in the same SQL statement. The following sections describe each operator's behavior.

AND

The AND operator allows the existence of multiple conditions in a SQL statement's WHERE clause. For an action to be taken by the SQL statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.

Example

Meaning

WHERE EMPLOYEE_ID = '333333333' AND SALARY = '20000'

The EMPLOYEE_ID must match 333333333 and the SALARY must equal 20000

   input_icon.gif

   SELECT *

   FROM PRODUCTS_TBL

   WHERE COST > 10
  
   AND COST < 30;

   output_icon.gif
PROD_ID    PROD_DESC                       COST
---------- ------------------------------ ------
11235      WITCHES COSTUME                29.99
90         LIGHTED LANTERNS                14.5

2 rows selected.

In this output, the value for cost had to be both greater than 10 and less than 30 for data to be retrieved.

   input_icon.gif

   SELECT *

   FROM PRODUCTS_TBL

   WHERE PROD_ID = '7725'
  
   AND PROD_ID = '2345';

   output_icon.gif
no rows selected

This statement retrieved no data because each row of data has only one product identification.

OR

The OR operator is used to combine multiple conditions in a SQL statement's WHERE clause. For an action to be taken by the SQL statement, whether it is a transaction or query, at least one of the conditions that are separated by OR must be TRUE.

Example

Meaning

WHERE SALARY = '20000' OR SALARY = '30000'

The SALARY must match either 20000 or 30000

   input_icon.gif

   SELECT *

   FROM PRODUCTS_TBL

   WHERE PROD_ID = '7725'
   
   OR PROD_ID = '2345';

   output_icon.gif
PROD_ID    PROD_DESC                       COST
---------- ------------------------------ ------
2345       OAK BOOKSHELF                  59.99

1 rows selected.

In this output, either one of the conditions had to be TRUE for data to be retrieved. Two records that met either one or the other condition were found.

In the next example, notice the use of the AND and two OR operators. In addition, notice the logical placement of the parentheses to make the statement more readable.

   input_icon.gif

   SELECT *

   FROM PRODUCTS_TBL

   WHERE COST > 10
  
   AND ( PROD_ID = '222'
   
   OR   PROD_ID = '90'
   
   OR   PROD_ID = '11235' );

   output_icon.gif
PROD_ID    PROD_DESC                       COST
---------- ------------------------------ ------
11235      WITCHES COSTUME                29.99
90         LIGHTED LANTERNS               14.5

2 rows selected.

The cost in this output had to be greater than 10, and the product identification had to be any one of the three listed. A row was not returned for PROD_ID 222 because the cost for this identification was not greater than 10.

Share ThisShare This

Informit Network