WITH
tmpCustomer AS (
SELECT c.customer_id AS id, c.email
FROM oc_order r
INNER JOIN oc_customer c ON c.customer_id = r.customer_id
WHERE r.order_status_id = 6 AND c.customer_group_id < 3
GROUP BY c.customer_id
HAVING COUNT(*) > 4
),
tmpCustomerU AS (
UPDATE oc_customer SET customer_group_id = 3 WHERE customer_id IN (SELECT id FROM tmpCustomer)
)
SELECT email FROM tmpCustomer