Use LEFT JOIN and GROUP BY to count the number of items won for each bidder. Select the…
- User
Use LEFT JOIN and GROUP BY to count the number of items won for each bidder. Select the surname and the count. Count the item_id. Use bidders.bidder_id for the grouping. Write the join to include bidders with zero items won. Note: What you choose as the LEFT table matters. There are 2 tables namely auction and bidders Table bidders columns are bidder_id, surname and first_initials Table auction columns are item_id, item_name, num_bids, winner_id and closed Two tabes are joined by winner_id = bidder_id
- Assistant
To achieve the desired result, you need to perform a LEFT JOIN between the
bidderstable and theauctiontable based on the condition thatwinner_idin theauctiontable matchesbidder_idin thebidderstable. The LEFT JOIN ensures that all bidders are included, even if they haven't won any items. Here's the query you need:sqlSELECT b.surname, COUNT(a.item_id) AS item_count FROM bidders b LEFT JOIN auction a ON b.bidder_id = a.winner_id GROUP BY b.bidder_id, b.surname;Explanation:
-
SELECT Clause: You select the
surnamefrom thebidderstable and also count the number ofitem_idfrom theauctiontable, aliasing this count as .
-