Checking in query before going for subqueries next.
Ugliest query ever, but it does the job.