MySQL IF Statement – Tutorialpath

MySQL IF Statement– Welcome to all our readers on our website at As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which help you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world.

As you know technology is one of the trending part in this world. So, here again we came up with a latest and new tech information which is related to the process of creating Basic MySQL IF statement for Beginners. With the help of this article you will get to know the complete process of how to create MySQL IF Statement for Beginners in a step by step manner. So, simply have a look to this article and grab all the useful information which is going to be very helpful for you.

MySQL IF Statement

MySQL IF Statement
MySQL IF Statement

Summaryin this tutorial, you will learn how to use MySQL IF statement to execute a block of SQL code based on conditions.

MySQL IF Statement – Must Follow Process

In MySQL, the IF-THEN-ELSE statement is used to execute code when a condition is TRUE, or execute different code if the condition evaluates to FALSE.

Syntax: MySQL IF ELSE statement
MySQL If Else Statement
MySQL If Else Statement

If the expression evaluates to TRUE , then the statements will be executed, otherwise, the control is passed to the next statement following the END IF.

The following flowchart demonstrates the IF statement:


Optional. You would use the ELSEIF condition when you want to execute a set of statements when a second condition (ie: condition2) is TRUE.
Optional. You would use the ELSE condition when you want to execute a set of statements when none of the IF or ELSEIF conditions evaluated to TRUE.


  • Once a condition is found to be TRUE, the IF-THEN-ELSE statement will execute the corresponding code and not evaluate the conditions any further.
  • If no condition is met, the ELSE portion of the IF-THEN-ELSE statement will be executed.
  • It is important to note that the ELSEIF and ELSE portions are optional.


The following example how to use the IF-ELSEIF-ELSE statement. The GetCustomerLevel()  stored procedure accepts two parameters customer number and customer level.

Image format:

Text Format:

  2. CREATE PROCEDURE GetCustomerLevel(
  3. INpCustomerNumber INT,
  4. OUT pCustomerLevelVARCHAR(20))
  5. BEGIN
  7. SELECTcreditLimit
  8. INTO credit
  9. FROM customers
  10. WHERE customerNumber = pCustomerNumber;
  11. IF credit > 50000 THEN
  12. SET pCustomerLevel = PLATINUM;
  13. ELSE
  14. SET pCustomerLevel = NOT PLATINUM;
  15. END IF;
  16. END$$

In this new stored procedure, we include the ELSE branch. If the credit does not exceed 50,000, we set the PLATINUM NOT customer level in the block between ELSE and END IF

The following flowchart demonstrates the logic of determining customer level.

In this tutorial, you have learned how to use MySQL IF statement to execute a block of SQL code based on conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *