KFSCKFSCNTRB-1781: Changing the sql query to be both Oracle and MySql friendly.