If yоu run this query, the number_оf_invоices for eаch row will show the number SELECT vendor_nаme, COUNT(*) AS number_of_invoices, AVG(invoice_totаl - payment_total - credit_total) AS balance_dueFROM vendors v JOIN invoices i ON v.vendor_id = i.vendor_id WHERE invoice_total - payment_total - credit_total