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 SQL if then else
 

SQL if then else

Updated March 8, 2023

SQL if then else

 

 

Introduction to SQL if then else

When dealing with conditional statements and executing code or statements in SQL based on the fulfillment of certain conditions, we can use conditional statements that are available in SQL. One such basic and crucial functionality is that of if then else statement in which we check for the expression or condition that includes variable comparison, retrieved data from the query comparison, or any literal value or parameter variable value checking that is retrieved from functions or stored procedure parameters.

Watch our Demo Courses and Videos

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

If the condition or expression evaluates to true or value that can be deduced to a true value, then the statements mentioned after if that is between BEGIN and END clauses or after THEN clause is executed and if condition or expression evaluates to false or false-like value then the statements that are mentioned after ELSE clause are executed. In this article, we will learn about some of the implementations of the if then else statements in SQL that help us to execute conditional behavior in SQL with the help of some examples.

Syntax 

The syntax of if then else statements are as shown below –

IF condition or expression evaluating to a boolean value
BEGIN
{ statements to be executed if the condition evaluates to true }
END
ELSE
BEGIN
{ statements to be executed if the condition evaluates to false }
END

The syntax consists of keywords like IF, BEGIN, END, and ELSE. Other than this, there is a necessity to mention the condition or expression after the IF keyword that ultimately evaluates to a boolean value. Further, whichever statements are to be executed if the boolean expression or condition evaluates to true, then they are to be mentioned between BEGIN and END keywords that are specified after IF keyword and expression and also mention the statements are to be executed if the boolean expression or condition evaluates to false then they are to be mentioned between BEGIN and END keywords that are specified after THEN keyword.

Examples

Let us consider an existing table named educba_articles that exists in our educba named database. We will check the contents and structure of the table by executing the following query statement –

SELECT * FROM educba_articles;

The output of the execution of the above query statement is as follows –

SQL if then else output 1

Now, we have to check the sum of the total cost of the articles that have been submitted. For this, we will calculate the cost of a single article by calculating the product of rate and pages. Further, we will use the SUM() function to calculate the total cost. Following will be our query statement –

SELECT
SUM(` rate` * ` pages`)
FROM
educba_articles
WHERE
` status` = "Submitted";

The output of the execution of the above query statement is as follows –

SQL if then else output 2

Now, we will use the IF and ELSE keywords as mentioned in the above syntax that is specified in the following manner to display the appropriate outputs –

BEGIN
DECLARE @totalCost INT;
SELECT
@totalCost = SUM(` rate` * ` pages`)
FROM
educba_articles
WHERE
` status` = "Submitted";
SELECT @totalCost;
IF @totalCost > 10000
BEGIN
PRINT 'The submitted articles seem to be great in terms of quality and quantity!';
END
ELSE
BEGIN
PRINT 'The submission rate needs to be increased!';
END
END

The output of the execution of the above query statement follows as the total cost does not exceed 10000, the flow executes the statement mentioned in else block printing “The submission rate needs to be increased!” message –

SQL if then else output 3

Now, suppose that we execute the following statements to print the messages depending on the pending articles that are not yet submitted –

BEGIN
DECLARE @totalCost INT;
SELECT
@totalCost = SUM(` rate` * ` pages`)
FROM
educba_articles
WHERE
` status` = "Pending";
SELECT @totalCost;
IF @totalCost > 10000
BEGIN
PRINT 'The writers need to speed up with their work!';
END
ELSE
BEGIN
PRINT 'More topics and work should be allocated to the writers!';
END
END

The output of the execution of the above query statement is as follows –

output 4

The expression mentioned in the IF keyword evaluates to true as the total cost of pending articles exceeds the 10000 amount, and then the message mentioned in the if block gets printed in the output. Let us execute the following query statement to confirm the total amount for pending articles and cross-check our working of IF then ELSE statements.

SELECT
SUM(` rate` * ` pages`)
FROM
educba_articles
WHERE
` status` = "Pending";

The output of the execution of the above query statement is as follows –

output 5

Using CASE statement

We can alternatively use the CASE statement to execute the same functionality as that of IF-ELSE keywords shown above in a single query statement compacting the implementation in a single query. Consider the same example to print the message ‘The writers need to speed up with their work!’ for pending costs exceeding 10000 amount. We will use the following query statement to implement case statement to implement if else functionality in SQL –

SELECT
CASE
WHEN SUM(` rate` * ` pages`) > 10000
THEN 'The writers need to speed up with their work!'
ELSE 'More topics and work should be allocated to the writers!'
END AS output
FROM
educba_articles
WHERE ` status` = "Pending" ;

The output of the execution of the above query statement follows that is the same as that of if-else keyword usage –

output 6

Let us also execute the case statement to implement our first example for submitted articles –

SELECT
CASE
WHEN SUM(` rate` * ` pages`) > 10000
THEN 'The submitted articles seem to be great in terms of quality and quantity!'
ELSE 'The submission rate needs to be increased!'
END AS output
FROM
educba_articles
WHERE ` status` = "Submitted" ;

The output of the execution of the above query statement is as follows –

output 7

Conclusion – SQL if then else

We can use IF and ELSE keywords in SQL to perform conditional execution of the statements depending on the condition evaluating to a boolean value. Alternatively, we can use the CASE statement to implement if then else functionality in SQL.

Recommended Articles

We hope that this EDUCBA information on “SQL if then else” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL DROP TRIGGER
  2. SQL Select Top
  3. MySQL Cross Join
  4. SQL Cluster

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
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?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW