The SQL If Else Statement is one of the most useful decision-making statements in real-time programming. SQL If statement will test the condition first and depending upon the result it will execute the statements.
If the test condition in SQL If statement is true, the statements inside the if block will be executed. Otherwise, statements inside the Else block will be executed. Let us see the syntax of the SQL Server If Else condition:
SQL If Else Statement Syntax
The syntax of the If Else in SQL Server is as follows:
IF (Test condition or Expression) BEGIN -- If the condition is TRUE then these statements will be executed True statements; END ELSE BEGIN -- If the condition is FALSE then these statements will be executed False statements; END
This accepts any test condition as the argument. If the test condition or an expression present in the above structure is true then True statements will be executed, if the condition is false then False statements will be executed.
SQL If Else Flow chart
Let us see the flow chart of the SQL Server If Else statement for better understanding.
If the test condition is true then STATEMENT 1 is executed followed by STATEMENT N. If the condition is False then STATEMENT 2 will execute followed by STATEMENT N is executed because it is out of the if else condition block and it has nothing to do with the condition result.
SQL If Else Example 1
In this Sql Server if else statement example we are going to place four different statements. If the condition is true we will display two different statements, if the condition is false we will display another two statements.
-- SQL If Else Example --Declaring Number and Total Variables DECLARE @Marks INT = 72 ; IF @marks > = 50 BEGIN PRINT ' Congratulations '; PRINT ' You pass the Examination '; END ELSE BEGIN PRINT ' You Failed '; PRINT ' Better Luck Next Time '; END
OUTPUT 1: Here marks = 72. Here, the Condition 72 >= 50 is TRUE. That’s why, statements inside the Sql If Statement display’s the Message output
OUTPUT 2: Here we changed the marks variable to 42, and the Condition is FALSE. That’s why, statements inside the Else Statement is displayed as Message output
SQL If Else Statement Example 2
In this program we are going to check whether the Employee Sales is greater than or equal to 2000 or not using our SQL Server If Else Statement.
- If the condition in SQL if statement is TRUE, We are going to display the Employee records Whose Sales is Greater than or Equal to 2000
- If the condition is FALSE, the query returns the Employee records Whose Sales is Less than 2000
Before we start writing our query, Let us see the data that we are going to use for this demonstration is
Let us see the code behind this SQL Else If statement example
-- SQL If Else Example --Declaring Number and Total Variables DECLARE @Sales INT = 2500 ; IF @Sales > = 2000 BEGIN SELECT [FirstName],[LastName] ,[Education],[Occupation] ,[YearlyIncome],[Sales],[HireDate] FROM [SQL Tutorial].[dbo].[Employee] WHERE [Sales] >= 2000 ORDER BY [Sales] ASC END ELSE BEGIN SELECT [FirstName],[LastName] ,[Education],[Occupation] ,[YearlyIncome],[Sales],[HireDate] FROM [SQL Tutorial].[dbo].[Employee] WHERE [Sales] < 2000 ORDER BY [Sales] ASC END
OUTPUT 1: Here, we specified the Sales as 500. The Condition in this SQL If Statement is 500 >= 2000 which is FALSE. That’s why, Output is displaying 6 out 14 records whose sales is less than 2000
OUTPUT 2: Here we changed the Sales variable to 2500. The Condition 2500 >= 2000 is TRUE. That’s why, Output is displaying 8 out 14 records whose sales is greater than or equal to 2000
Thank You for Visiting Our Blog