Execute a sql query inside IF() function Wherever I find an example of IF () function I only see something like below. 

SELECT IF(500&lt;1000, "YES", "NO");
+---------------------------+
| IF(500&lt;1000, "YES", "NO") |
+---------------------------+
| YES                       |
+---------------------------+


But can I do something like below i.e executing a query?

IF(500&lt;1000, CREATE DATABASE test1; , CREATE DATABASE test2;);


One way is to using prepared statements. But I want to directly execute the query inside it.
Learn about Stored Programs.  Write a Procedure that uses IF statements, not functions, to conditionally perform SQL statements.

Then invoke the Stored Procedure with a CALL statement.