WITH target_user_ids AS (
SELECT id
FROM users
WHERE staged = false
AND active = true
AND last_seen_at IS NOT NULL)
SELECT
provider_name,
user_id,
info->>‘name’ AS name,
info->>‘email’ AS email,
info->>‘image’ AS image
FROM user_associated_accounts ua
WHERE user_id IN (SELECT id FROM target_user_ids)