Chapter 5: SQL Clause

SQL WHERE

WHERE clause in SQL is a DML(data manipulation language)statement.it can be used to limit the number of rows returned by a query.

It returns only those queries which fulfill the specific conditions.it filters the records.

WHERE clause is used in SELECT, UPDATE, DELETE statement etc.

Syntax for sql where:

SELECT column1, column 2, ... column n  
FROM    table_name  
WHERE [conditions]  

WHERE clause uses some conditional selection

=equal
>greater than
<less than
>=greater than or equal
<=less than or equal
< >not equal to

SQL AND

In SQL query to create two or more conditions to be met, the SQL AND condition is used.

It is used in SQL SELECT, INSERT, UPDATE and DELETE statements.

Let’s see the syntax for SQL AND:

SELECT columns  
FROM tables  
WHERE condition 1  
AND condition 2;  

The SQL AND condition also can be used to join multiple tables in a SQL statement.

SQL “AND” example with “INSERT” statement

This is how an 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'  
AND employees =1000;  
SQL “AND” example with “UPDATE” statement

For example:

UPDATE suppliers  
SET supplier_name = 'HP'  
WHERE supplier_name = 'IBM'  
AND offices = 8; 
SQL “AND” example with “DELETE” statement

For example:

DELETE FROM suppliers  
WHERE supplier_name = 'IBM'  
AND product = 'PC computers';  

SQL OR

The SQL OR condition is used in a SQL query to create a SQL statement where records are returned when any one of the condition met. It can be used in a SELECT statement, INSERT statement, UPDATE statement or DELETE statement.

Let’s see the syntax for OR condition:

SELECT columns  
FROM tables  
WHERE condition 1  
OR condition 2;  
SQL “OR” example with SQL SELECT
SELECT *   
FROM suppliers  
WHERE city = 'New York'  
OR available_products >= 250;  
SQL “OR” example with SQL INSERT

For example:

INSERT INTO suppliers(supplier_id, supplier_name)  
SELECT account_no, name  
FROM customers  
WHERE city = 'New Delhi'  
OR city = 'Ghaziabad';  
SQL “OR” example with SQL UPDATE

For example:

UPDATE suppliers   
SET supplier_name = 'HP'  
WHERE supplier_name = 'IBM'  
OR available_product >36;  
SQL “OR” example with SQL DELETE

For example:

DELETE FROM suppliers  
WHERE supplier_name = 'IBM'  
OR employee <=100;  

SQL SELECT AS

SQL AS is used to assign a new name to a table column temporarily.

The example of select as: 

Let us take a table named orders, it contains:2

Day_of_orderCustomerProductQuantity
1-05-2014AjinkyaMobile20
13-08-2014MayurLaptop2
6-12-2015BalakrishanaWater cannon32

After applying this SQL AS example syntax

SELECT day_of_order AS "Date"    
Customer As "Client",    
Product,    
Quantity,    
FROM orders;    

Output will be shown as this table:

DateClientProductQuantity
1-05-2014AjinkyaMobile20
13-08-2014MayurLaptop2
6-12-2015BalakrishanaWater cannon32
Note: SQL AS is same as SQL ALIAS.