Wednesday, January 9, 2013

AND Condition


The SQL "AND" condition allows you to create an SQL statement based on 2 or more conditions being met. It can be used in any valid SQL statement - SQL SELECT statement, SQL INSERT statement, SQL UPDATE statement, or SQL DELETE statement.
The syntax for the SQL "AND" condition is:
SELECT columns
FROM tables
WHERE column1 = 'value1'
and column2 = 'value2';
The SQL "AND" condition requires that each condition be must be met for the record to be included in the result set. In this case, column1 has to equal 'value1' and column2 has to equal 'value2'.

SQL "AND" Condition - SQL SELECT Statement example

The first SQL "AND" Condition example that we'll take a look at involves an SQL SELECT statement with 2 conditions:
SELECT *
FROM suppliers
WHERE city = 'New York'
and type = 'PC Manufacturer';
This SQL "AND" condition example would return all suppliers that reside in New York and are PC Manufacturers. Because the * is used in the SQL SELECT statement, all fields from the supplier table would appear in the result set.

SQL "AND" Condition - JOINING Tables example

Our next example demonstrates how the SQL "AND condition" can be used to join multiple tables in an SQL statement.
SELECT orders.order_id, suppliers.supplier_name
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'IBM';
This SQL "AND" condition example would return all rows where the supplier_name is IBM. And the suppliers and orders tables are joined on supplier_id. You will notice that all of the fields are prefixed with the table names (ie: orders.order_id). This is required to eliminate any ambiguity as to which field is being referenced; as the same field name can exist in both the suppliers and orders tables.
In this case, the result set would only display the order_id and supplier_name fields (as listed in the first part of the select statement.).

SQL "AND" Condition - SQL INSERT Statement example

The SQL "AND" Condition can be used in the SQL INSERT statement.
For example:.
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE customer_name = 'IBM'
or city = 'New York';
This SQL "AND" Condition example would insert into the suppliers table, all account_no and name records from the customers table whose customer_name is IBM and reside in New York.

SQL "AND" Condition - SQL UPDATE Statement example

The SQL "AND" Condition can be used in the SQL UPDATE statement.
For example:.
UPDATE suppliers
SET supplier_name = 'HP'
WHERE supplier_name = 'IBM'
and state = 'California';
This SQL "AND" Condition example would update all supplier_name values in the suppliers table to HP where the supplier_name was IBM and resides in the state of California.

SQL "AND" Condition - SQL DELETE Statement example

The SQL "AND" Condition can be used in the SQL DELETE statement.
For example:.
DELETE FROM suppliers
WHERE supplier_name = 'IBM'
and product = 'PC computers';
This SQL "AND" Condition example would delete all suppliers from the suppliers table whose supplier_name was IBM and product was PC computers.
Learn more about joining tables in SQL.