Detect stale invoices
SELECT i.id, i.customer_id, i.due_date FROM invoices i LEFT JOIN payments p ON p.invoice_id = i.id WHERE p.id IS NULL AND i.due_date < date('now', '-30 day');
Identify outstanding invoices that have surpassed the 30-day window without any payment events.
Use a LEFT JOIN to find invoices lacking payment rows and filter by due_date older than 30 days relative to today, ensuring insurance for missing data.
SELECT invoice.id, invoice.customer_id, invoice.due_date FROM invoices LEFT JOIN payments ON payments.invoice_id = invoice.id WHERE payments.id IS NULL AND invoice.due_date < TODAY() - 30
Create a query that finds invoices older than 30 days without payment records.