Wednesday, January 25, 2012

SQL SERVER :- WHERE Clause


SQL WHERE

IT IS VERY SIMPLE AND EASY TO LEARN ABOUT WHERE CLAUSE....

The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query. We are going to use the address table, to illustrate the use of the SQL WHERE command.
Database: Adventure Works
Schema: Person
Table: Address

If we want to select all customers from our database table, having addressID '5' we need to use the following SQL Example:
Database: Adventure Works
Schema: Person
Table: Address

select * from AdventureWorks.Person.Address where AddressID =5

 -- or --
 select * from Address where AddressID =5



In this simple SQL query we used the "=" (Equal) operator in our WHERE criteria:
AddressID =5

But we can use any of the following comparison operators in conjunction with the SQL WHERE clause:
Database: Adventure Works
Schema: Person
Table: Address

<> (Not Equal)
select * from Address where AddressID <>5

> (Greater than)
select * from Address where AddressID >5

>= (Greater or Equal)
select * from Address where AddressID >=5

< (Less than)
select * from Address where AddressID <5

<= (Less or Equal)
select * from Address where AddressID <=5
LIKE (similar to)
 
select * from Address where Addressline1 like ‘1226 Shoe St.’
Note the LIKE syntax is different with the different RDBMS (SQL Server syntax used above). Check the sql Like  article for more details.
Between (Defines a range)
 
. select * from Address where AddressID between 5 and 10

Like and Share to SQL Integrity Blog

No comments:

Post a Comment

Thank You !!!!