Introduction
This is part six of "Zero To Hero In MS SQL Server". In this article series, we will learn about WHERE Clause or Where Condition in MS SQL Server. To learn from my previous articles in this series, please follow the below-mentioned links.
The following are the topics of this article
- A glimpse at the previous series.
- Where Condition in MS SQL Server.
- Points to remember.
- Conclusion.
A Glimpse at the Previous Series
In Part five
- In Part Five, we learned about selecting data from the database table.
- Selecting a particular table column from the database table.
- Inserting data into the created table in the database.
- Inserting bulk data into the table in the database.
Where conditions in MS SQL server
Where Condition is also called Where Clause, it's mainly used to filter the records from the table. To be precise, Where the condition is used to select data from the database table with specific conditions and only those data will be displayed.
Syntax
SELECT * FROM tableName WHERE condition
Example
SELECT * FROM [Students].[StudentDetails]
When the above SQL Statement is executed, we will get all the data in the table.
Fig1. Table with All Data (Where Clause is not applied)
Now, let us apply the where clause.
SELECT * FROM [Students].[StudentDetails] WHERE StudentId=1
In the above given example, WHERE is the keyword and the condition is to select the record whose ID is 1. So only that particular data will be displayed. In our table, StudentId is a Unique value and it will be allocated only for one record.
Fig 2. Table with condition applied data (Where Clause is applied)
In Where Clause, N number of conditions are applicable. Let's see another example with multiple conditions in the Where clause.
And
Syntax
SELECT * FROM tableName WHERE Column1=Condition AND Column2=Condition AND ColumnN=ConditionN
Example
SELECT * FROM [Students].[StudentDetails] WHERE StudentId=2 AND FirstName='Saravana'
In the above-given example, StudentId and FirstName are set as conditions. In this condition, we have data which means, it will be displayed, otherwise empty table will be displayed. When the AND operator is used, then all the conditions should be satisfied.
Fig 3. Table with multiple conditions applied data
Now, let us see another example that does not satisfy the Where Condition.
Example
SELECT * FROM [Students].[StudentDetails] WHERE StudentId=2 AND FirstName='Sundaram'
In the above-given example, the condition is not true, so we will not get any data from the Table.
Fig 4. Table with empty records
OR
When the OR operator is used, if either of the conditions is satisfied, the data will be displayed in the table.
Syntax
SELECT * FROM tableName WHERE Column1=Condition OR Column2=Condition
Example
SELECT * FROM [Students].[StudentDetails] WHERE StudentId=2 OR FirstName='Sundaram'
When the above statement is executed, we have data in both conditions. So two rows will be displayed in the table.
Fig 5. Table with where condition with OR Operator
IN
When IN Operator is used in a where clause, it's to apply multiple conditions for the same column in the SQL Table. In other words, IN Operator is the shorthand form of Multiple OR Condition.
Syntax
SELECT * FROM tableName WHERE IN (Conditions....)
Example
SELECT * FROM Students.StudentDetails WHERE FirstName IN ('Sundaram','Saravana','Karthik')
Fig 6. Table with where condition with IN Operator
Note. Those who are learning or new to SQL, don't worry about operators. Operators in SQL will be explained in detail in my next article.
Points to Remember
- Where Clause is used to select the data from the table by using certain conditions
- We can apply more than one condition in Where Clause using And, OR, IN Operators
- In Where Clause, N number of conditions are applicable
Conclusion
In this article, we have learned about the Where Clause in MS SQL Server. I hope this was very useful. Please share your feedback and knowledge in the comment section.