Sunday, February 10, 2013

How to use WHERE Clause in SQL Server

SQL WHERE Clause 
The WHERE clause is used to filter the Records from a Table which satisfies the Condition specified.

Syntax:
SELECT column_name(s) FROM table_name 
WHERE column_name operator value

Example:

Suppose we have an EMP Table as follows :
Emp_ID
E_Name
Job
Dept_ID
Salary
1001
Ravi Teja Reddy
Analyst
1
20000
1002
Karun
Team Lead
2
35000
1003
Ravi Kumar Rao
Analyst
1
10000
1004
Tamatam
Team Lead
3
25000
1005
Sai Kumar Teja
Analyst
2
15000
1006
William
Manager
3
50000
1007
Vikram Teja Reddy
Analyst
2
18000
1008
Varun
Team Lead
1
33000
1009
Smith
Team Lead
3
50000
-----------------------------------------------------
Criteria - I :
Extracting records from above Table where Job = 'Analyst'
Select*from EMP WHERE Job='Analyst'

Result :
Emp_ID
E_Name
Job
Dept_ID
Salary
1001
Ravi Teja Reddy
Analyst
1
20000
1003
Ravi Kumar Rao
Analyst
1
10000
1005
Sai Kumar Teja
Analyst
2
15000
1007
Vikram Teja Reddy
Analyst
2
18000
-----------------------------------------------------
Criteria - II :
Extracting records from above Table where Emp_Id IN(1002,1004,10016)
Select*from EMP WHERE Emp_ID IN(1002,1004,10016)

Result :
Emp_ID
E_Name
Job
Dept_ID
Salary
1002
Karun
Team Lead
2
35000
1004
Tamatam
Team Lead
3
25000
1006
William
Manager
3
50000

Note:
 In some versions of SQL the <> operator may be written as != for Not Equality.
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog