The SQL If Else statement is one of the most useful decision-making statements. 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 execute. Otherwise, statements inside the Else block 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
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 Sql Server if else statement accepts any test condition as the argument. If the test condition or expression in the above structure is true, then True statements will execute. If the condition is false, then False statements will run.
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 STATEMENT1 will run, followed by STATEMENTN. If the condition is False, then STATEMENT2 will run, followed by STATEMENTN. Because it is out of the if else condition, and it has nothing to do with the SQL Server 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 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 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 If Else 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 of 14 records whose sales is less than 2000
Here, we changed the Sales variable to 2500. The Condition 2500 >= 2000 is TRUE. That’s why Output is displaying 8 out of 14 records whose sales is greater than or equal to 2000