How to check main blog in a multisite using if statement?

You can use is_main_site conditional tag like this:

if (is_main_site()) {
  execute this sql;
}
else {
  execute this sql
}