You write SELECT first, but the database runs it last. Understanding the real order of execution explains half the SQL errors that look like magic.