EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial T-SQL ELSE IF
 

T-SQL ELSE IF

Updated June 8, 2023

T-SQL ELSE IF

 

 

Introduction to T-SQL ELSE IF

The ELSE…IF condition in T-SQL is defined as, it is a control flow statement that can acknowledge us to implement or omit a block of the statement which has a particular set of conditions, in which we can say that ELSE…IF is a conditional statement that can manage the flow of the statement and implement conditionally with the help of ELSE…IF block statement, if the condition assessed as true then statement can evaluate true, after that the statement which is come after if the condition that will be executed, the syntax of it can have Boolean-expression and block of statement in between BEGIN and END.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is T-SQL else if?

The ELSE IF statement is convenient to verify various conditions at one time and the SQL ELSE IF statement is a supplement to the if and then else if then else statement can only implement the statements when the provided condition is true or false, but in an authentic way we can able to verify more than two conditions, in such cases we can able to use else if statement.

The ELSE IF statement in T-SQL can manage various statements effectively by implementing them in sequential order, it will examine the first condition then if the condition is true then will implement the statements which are available in that block, and if the condition is false then it will examine the else…if condition, let us see the syntax below,

IF (Expression 1)

BEGIN

Statement 1;

END

ELSE IF (Expression 2)

BEGIN

Statement 2;

END

……….

ELSE

BEGIN

Default Statement;

END

T sql

In the above syntax, we have used various statements under the BEGIN and END statement,

Let us see the flow chart of the ELSE IF statement,

flowchart

How to use T-SQL else if statement?

The If else statement can manage the flow of the program and it can acknowledge us to implement or omit the block of a statement depending on a particular condition,

  • IF statement:

Let us see the syntax of the IF statement,

IF Boolean_Expression

BEGIN

{Statement_Block}

END

lllll

In the above syntax, if the ‘Boolean_Expression’ assess to be true then the block of statement is implemented which is available in BEGIN…END block, otherwise the block of statement has been omitted and manage the program is passed to the statement later END keyword.

Let us see if Boolean_Expression carry ‘SELECT’ statement,

BEGIN

DECLARE @deals INT;

SELECT

@deals = SUM (data_prize * total)

FROM

deals.order_items i

INNER JOIN deals.orders o ON o.order_id = i.order_id

WHERE

YEAR (order_date) = 2019;

SELECT @deals;

IF @deals > 100000

BEGIN

PRINT ‘Wow! The sales total in 2019 is more than 1,00,000’;

END

END

hhhh

Output:

After that, we will get a screen as given below and we have to click on ‘Messages’ to see the output.

ooo

  • IF ELSE statement:

If the ‘Boolean expression’ can assess to FALSE then we have to implement the ELSE block, so we can say that the ELSE clause has been used if the first condition gets false.

IF Boolean_expression

BEGIN

— This block can implement when the Boolean expression is TRUE

END

ELSE

BEGIN

— This block can implement when the Boolean expression is FALSE

END

If a statement can have the conditions if the condition evaluates to true then the block of statement in the ‘IF’ clause is implemented, and if the condition is ‘false’, then the block of code in the ‘else’ block is implemented.

  • Nested IF ELSE:

We can able to use nested IF-ELSE statements under the another IF…ELSE statement, let us see the example for it,

BEGIN

DECLARE @m INT = 20,

@m INT = 30;

IF (@m > 0)

BEGIN

IF (@x < @y)

PRINT ‘m > 0 and m < n’;

ELSE

PRINT ‘m > 0 and m >= n’;

END

END

T-SQL else if rules

  • When we try to evaluate the conditions then it should be in Boolean Expression.
  • If the IF ELSE statement can able to manage a single block statement conditionally.
  • The block of the statement will have to start with BEGIN and ends with the END keyword.
  • With the help of BEGIN and END it can able to verify a block of the statement which we want to implement and it can differ from the other SQL statements that are not related to the IF-ELSE block in the T-SQL.
  • The ELSE is optional in the statement.

Example #1

BEGIN

DECLARE @deals INT;

SELECT @deals = SUM (file_cost * amount) FROM deals.order_record I INNER JOIN deals.orders o ON o.order_id = i.order_id WHERE YEAR (order_date) = 2014;

SELECT @deals;

IF @deals > 10000000

BEGIN

PRINT ‘Ohhh! The deals amount in 2014 is more than 10,000,000’;

END

ELSE

BEGIN

PRINT ‘Deals amount in 2014 did not reach 10,000,000’;

END

END

T-SQL ELSE IF END

Output:

T-SQL ELSE IF op

In the above example, we have to use the If clause to check amount whether the given amount is greater than 10 million, also we have used BEGIN to start the execution of the code, and then we have declared the variables and then we have used the select query to get the details, we have printed a message and closes with the END statement.

Example #2

BEGIN

DECLARE @x INT = 30,

@y INT = 400;

IF (@x > 0)

BEGIN

IF (@x < @y)

PRINT ‘x > 0 and x < y’;

ELSE

PRINT ‘x > 0 and x >= y’;

END

END

Output:

T-SQL ELSE IF op1

In the above example, we have used an IF statement within another statement which can make the code hard to read and manage, we have declared the two variables, x, and y and then we try to print two ways if the condition is true then it will print the message or after false.

Conclusion

In this article, we conclude that the else if is a conditional statement that can be used when we have more than two conditions, so we have also discussed how to use the else if statement, the rules of the else if statement, so this article will help to understand the concept of else if.

Recommended Article

This is a guide to T-SQL ELSE IF. Here we discuss the definition, What is T-SQL else if, How to use T-SQL else if statement, and examples along with code implementation and output. You may also have a look at the following articles to learn more –

  1. T-SQL INSERT
  2. What is T-SQL?
  3. T-SQL String Functions
  4. T-SQL Commands

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW