I have a table with a lot of history entries that contain customer IDs.
There is a separate customer table. Occasionally some of the customer entries are removed.
How about:
DELETE FROM history_table WHERE customer_id NOT IN (SELECT customer_id FROM customer);