IF Condition in MySql with Select Statement

How to use IF condition in MySql with a select statement. In this tutorial, You are going to learn how to use IF condition in MySql to select column value conditionally.

If Condition in MySql

If Condition in MySql

IF Condition Syntax in MySql

Let’s first see the syntax of MySql if condition.

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.

If Condition in MySql – Query Examples

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.

If Condition in MySql with Aggregate Functions

Let’s check another example with an aggregate function. To demonstrate this example I am creating the 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 clause without using IF condition.

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

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.