IF Statement in MySql

How to use IF condition in MySql to print or select values based on condition . In this tutorial, You are going to learn how to use IF statement  to print or select output based on conditions.

IF Condition in MySql

Syntax –

For better understanding, Let’s start with  simple examples.

In the above query, we print  1 if the value of timestamp column is NULL otherwise 0.

Let’s take some more examples.

How to compare NULL values in MySql

Difference between inner join and left join

NOTE – There is another IF statement in MySql version 5.6 which is different from if() function.

MySql Query using if() Function

To understand it’s proper usage. Let’s create a table emp_data.

Let’s print the name and designation of all the employees. We’ll print the designation  either Manager or Other.

Subscribe Our Tutorials

Get Latest Updates on Facebook

If condition with aggregate functions

Let’s take another table products.

Let’s write a query which will print  the count of active, disabled and pending products.

You can achieve the same result through group by without using IF condition as well.

Difference between Inner Join and Left Join

MySql Interview Questions

For more reference :

MySql Documentation

MySql Recommended Books

MySql Books on Amazon.in

MySql Books on Flipkart

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.