Zero To Hero In MS SQL Server - Part Six

In this article, I have shared some knowledge on WHERE CLAUSE and Update SQL Statement.


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, 
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 to the created table in the database.  
  • Inserting a bulk data into the table in the database. 
Where Condition 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.  
  1. SELECT * FROM tableName WHERE condition
  1. SELECT * FROM [Students].[StudentDetails]  
 When the above SQL Statement is executed, we will get all the data in the table. 
Fig.1 Table with All Data (Where Clause is not applied)

Now, let us apply the where clause, 
  1. 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 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 Where clause.
  1. SELECT * FROM tableName WHERE Column1=Condition AND Column2=Condition AND ColumnN=ConditionN  
  1. 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 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 which does not satisfy the Where Condition. 
  1. 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 in from Table. 
Fig.4 Table with empty records
When OR operator is used, if either of the conditions is satisfied, the data will be displayed in the table.
  1. SELECT * FROM tableName WHERE Column1=Condition OR Column2=Condition  
  1. 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

When IN Operator is used in 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.    
  1. SELECT * FROM tableName WHERE IN (Conditions....)  
  1. SELECT * FROM Students.StudentDetails WHERE FirstName IN ('Sundaram','Saravana','Karthik')  
Fig.6 Table with where condition with IN Operator

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 
In this article, we have learned about Where Clause in MS SQL Server. I hope this was very useful. Please share your feedback and knowledge in the comment section.