DELETE FROM session_data WHERE session_data.name = 'user_id' AND NOT EXISTS (SELECT * FROM users WHERE CAST(session_data.value AS integer) = users.id);