Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL IF ELSE

by suresh

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.

SQl Server If Else flow chart

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

SQL IF ELSE 1

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 2

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

SQL IF ELSE 3

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

SQL IF ELSE 4

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

SQL IF ELSE 5

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021ยท All Rights Reserved.
About | Contact | Privacy Policy