cswork / libraryOpen practice workspace

Coding questions · Meta

Calculate Total Interactions for Each Product

This question evaluates a candidate's competence in SQL-based data aggregation and relational joins for computing product-level metrics from transaction logs.

Interactions

+----------+-----------+------------+--------------+
| buyer_id | seller_id | product_id | interactions |
+----------+-----------+------------+--------------+
| 101      | 201       | 501        | 3            |
| 102      | 202       | 502        | 5            |
| 103      | 201       | 501        | 2            |
| 101      | 203       | 503        | 1            |
+----------+-----------+------------+--------------+

Products

+------------+--------------+-----------+
| product_id | product_name | category  |
+------------+--------------+-----------+
| 501        | Phone Case   | Accessory |
| 502        | Headphones   | Audio     |
| 503        | Charger      | Accessory |
+------------+--------------+-----------+
Scenario

Marketplace platform records every buyer–seller exchange. Table Interactions(buyer_id, seller_id, product_id, interactions) stores the counted exchanges; Products(product_id, product_name, category) stores catalog metadata.

Question

Write an SQL query that returns each product_id (and optionally product_name) with the total number of interactions across all buyers and sellers. Ensure you aggregate the interaction field correctly.

Hints

JOIN products and use SUM(interactions) rather than COUNT(*).

Total interactions per product

Given the tables Interactions(buyer_id, seller_id, product_id, interactions) and Products(product_id, product_name, category), write an SQL query that returns each product_id and its product_name with the total number of interactions across all buyers and sellers. Aggregate the interactions column correctly so that each product appears once with the sum of its interactions.

Table Interactions

columntypeconstraints
buyer_idINTEGER
seller_idINTEGER
product_idINTEGER
interactionsINTEGER

Sample data:

buyer_idinteractionsproduct_idseller_id
1013501201
1025502202
1032501201
1011503203

Table Products

columntypeconstraints
product_idINTEGER
product_nameVARCHAR
categoryVARCHAR

Sample data:

categoryproduct_idproduct_name
Accessory501Phone Case
Audio502Headphones
Accessory503Charger

Expected output

One row per product that appears in Interactions with the sum of its interactions across all buyer-seller pairs, including the product name.

product_idproduct_nametotal_interactions
501Phone Case5
502Headphones5
503Charger1

Hints

  1. JOIN Products to Interactions on product_id.
  2. Use SUM(interactions) to aggregate the interaction counts rather than COUNT(*).