Watch 3 video solutions for Number of Trusted Contacts of a Customer, a medium level problem involving Database. This walkthrough by Frederik Müller has 2,702 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Customers
+---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | customer_name | varchar | | email | varchar | +---------------+---------+ customer_id is the column of unique values for this table. Each row of this table contains the name and the email of a customer of an online shop.
Table: Contacts
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | id | | contact_name | varchar | | contact_email | varchar | +---------------+---------+ (user_id, contact_email) is the primary key (combination of columns with unique values) for this table. Each row of this table contains the name and email of one contact of customer with user_id. This table contains information about people each customer trust. The contact may or may not exist in the Customers table.
Table: Invoices
+--------------+---------+ | Column Name | Type | +--------------+---------+ | invoice_id | int | | price | int | | user_id | int | +--------------+---------+ invoice_id is the column of unique values for this table. Each row of this table indicates that user_id has an invoice with invoice_id and a price.
Write a solution to find the following for each invoice_id:
customer_name: The name of the customer the invoice is related to.price: The price of the invoice.contacts_cnt: The number of contacts related to the customer.trusted_contacts_cnt: The number of contacts related to the customer and at the same time they are customers to the shop. (i.e their email exists in the Customers table.)Return the result table ordered by invoice_id.
The result format is in the following example.
Example 1:
Input: Customers table: +-------------+---------------+--------------------+ | customer_id | customer_name | email | +-------------+---------------+--------------------+ | 1 | Alice | alice@leetcode.com | | 2 | Bob | bob@leetcode.com | | 13 | John | john@leetcode.com | | 6 | Alex | alex@leetcode.com | +-------------+---------------+--------------------+ Contacts table: +-------------+--------------+--------------------+ | user_id | contact_name | contact_email | +-------------+--------------+--------------------+ | 1 | Bob | bob@leetcode.com | | 1 | John | john@leetcode.com | | 1 | Jal | jal@leetcode.com | | 2 | Omar | omar@leetcode.com | | 2 | Meir | meir@leetcode.com | | 6 | Alice | alice@leetcode.com | +-------------+--------------+--------------------+ Invoices table: +------------+-------+---------+ | invoice_id | price | user_id | +------------+-------+---------+ | 77 | 100 | 1 | | 88 | 200 | 1 | | 99 | 300 | 2 | | 66 | 400 | 2 | | 55 | 500 | 13 | | 44 | 60 | 6 | +------------+-------+---------+ Output: +------------+---------------+-------+--------------+----------------------+ | invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt | +------------+---------------+-------+--------------+----------------------+ | 44 | Alex | 60 | 1 | 1 | | 55 | John | 500 | 0 | 0 | | 66 | Bob | 400 | 2 | 0 | | 77 | Alice | 100 | 3 | 2 | | 88 | Alice | 200 | 3 | 2 | | 99 | Bob | 300 | 2 | 0 | +------------+---------------+-------+--------------+----------------------+ Explanation: Alice has three contacts, two of them are trusted contacts (Bob and John). Bob has two contacts, none of them is a trusted contact. Alex has one contact and it is a trusted contact (Alice). John doesn't have any contacts.
Problem Overview: Each invoice belongs to a customer. For every invoice, return the customer name, invoice price, the number of contacts that customer has, and how many of those contacts are trusted. A contact becomes trusted if the contact's email matches the email of an existing customer. The result must be produced using SQL joins and aggregations.
Approach 1: Correlated Subqueries (O(n * m) time, O(1) extra space)
A straightforward solution uses correlated subqueries for each invoice row. First join Invoices with Customers to get the invoice owner. Then compute contacts_cnt by counting rows in Contacts where Contacts.user_id = Customers.customer_id. To compute trusted_contacts_cnt, add another subquery that checks whether the contact email exists in the Customers table. Each invoice triggers multiple nested lookups, so performance degrades as the number of contacts grows. This approach works for small datasets but becomes inefficient with large tables because each row repeatedly scans related records.
Approach 2: Join + Aggregation (Optimal SQL Approach) (O(n log n) time, O(n) space)
The efficient solution uses joins and a single aggregation pass. Start by joining Invoices with Customers to identify the invoice owner. Next, left join the Contacts table on Customers.customer_id = Contacts.user_id to bring in all contacts for that customer. To detect trusted contacts, join Contacts.contact_email with Customers.email (using another alias of the Customers table). Now you can count total contacts with COUNT(Contacts.contact_email) and trusted contacts using conditional counting such as COUNT(trusted_customer.customer_id). Finally, group by invoice attributes to aggregate results correctly. This reduces repeated scans and lets the database optimizer handle indexing and join planning efficiently.
This pattern is common in relational queries where you need both total relationships and filtered relationships in the same result set. Mastering joins and conditional aggregation is critical for database interview questions and real production analytics queries.
Recommended for interviews: The join + aggregation solution is what interviewers expect. It demonstrates understanding of SQL joins and relational aggregation. Showing the subquery approach first proves you understand the logic, but the optimized join-based solution shows you know how to write scalable SQL.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Correlated Subqueries | O(n * m) | O(1) | Quick prototype or small datasets where query readability matters more than performance |
| Join + Aggregation (Optimal) | O(n log n) | O(n) | Production queries and interview settings requiring efficient relational joins |