SELECT COUNT(DISTINCT e.id) as count
FROM emails e
INNER JOIN email_folder_map efm ON e.id = efm.email_id
INNER JOIN email_folders f ON efm.folder_id = f.id
WHERE f.user_id = ? 
  AND f.folder_type = 'spam'
  AND e.user_id = ?;