mysql> EXPLAIN SELECT COUNT(*) FROM `postman_message` LEFT OUTER JOIN `postman_message` T3 ON (`postman_message`.`thread_id` = T3.`id`) WHERE (`postman_message`.`id` IN (SELECT MAX(U0.`id`) AS `pk__max` FROM `postman_message` U0 WHERE (U0.`thread_id` IS NOT NULL AND (U0.`recipient_archived` = False AND U0.`moderation_status` = a AND U0.`recipient_id` = 201 AND U0.`recipient_deleted_at` IS NULL)) GROUP BY U0.`thread_id` ORDER BY NULL) OR ((`postman_message`.`recipient_archived` = False AND `postman_message`.`moderation_status` = a AND `postman_message`.`recipient_id` = 201 AND `postman_message`.`recipient_deleted_at` IS NULL) AND T3.`id` IS NULL)); ERROR 1054 (42S22): Unknown column 'a' in 'where clause' mysql> EXPLAIN SELECT COUNT(*) FROM `postman_message` LEFT OUTER JOIN `postman_message` T3 ON (`postman_message`.`thread_id` = T3.`id`) WHERE (`postman_message`.`id` IN (SELECT MAX(U0.`id`) AS `pk__max` FROM `postman_message` U0 WHERE (U0.`thread_id` IS NOT NULL AND (U0.`recipient_archived` = False AND U0.`moderation_status` = "a" AND U0.`recipient_id` = 201 AND U0.`recipient_deleted_at` IS NULL)) GROUP BY U0.`thread_id` ORDER BY NULL) OR ((`postman_message`.`recipient_archived` = False AND `postman_message`.`moderation_status` = "a" AND `postman_message`.`recipient_id` = 201 AND `postman_message`.`recipient_deleted_at` IS NULL) AND T3.`id` IS NULL)); +----+--------------------+-----------------+--------+---------------------------------------------------+--------------------------+---------+-------------------------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-----------------+--------+---------------------------------------------------+--------------------------+---------+-------------------------------------------+------+----------------------------------------------+ | 1 | PRIMARY | postman_message | ALL | postman_message_fcd09624 | NULL | NULL | NULL | 3387 | Using where | | 1 | PRIMARY | T3 | eq_ref | PRIMARY | PRIMARY | 4 | teleforma_crfpa.postman_message.thread_id | 1 | Using where; Using index | | 2 | DEPENDENT SUBQUERY | U0 | ref | postman_message_fcd09624,postman_message_9a6ed576 | postman_message_fcd09624 | 5 | const | 1 | Using where; Using temporary; Using filesort | +----+--------------------+-----------------+--------+---------------------------------------------------+--------------------------+---------+-------------------------------------------+------+----------------------------------------------+ 3 rows in set (0.00 sec)