Question:
List all suppliers along with the total quantity of products they've shipped. Display the 'supplier_id', 'supplier_name', and 'total_shipped_quantity'. Include even those suppliers who haven't shipped any products by showing their 'total_shipped_quantity' as 0.
Sample Input Tables:
Suppliers Table:
supplier_id | supplier_name | contact | address |
---|---|---|---|
1 | TechSource | +1234567890 | 123 Tech Street |
2 | GadgetFlow | +0987654321 | 456 Gadget Ave |
3 | Electronix | +1122334455 | 789 Electron Road |
Shipments Table:
shipment_id | supplier_id | product_id | quantity | date |
---|---|---|---|---|
101 | 1 | 10 | 50 | 2024-01-01 |
102 | 2 | 20 | 70 | 2024-01-02 |
103 | 1 | 30 | 30 | 2024-01-03 |
Sample Output:
supplier_id | supplier_name | total_shipped_quantity |
---|---|---|
1 | TechSource | 80 |
2 | GadgetFlow | 70 |
3 | Electronix | 0 |
This output is just for illustrative purposes. The actual output could be different from the one you see above.
SELECT s.supplier_id, s.supplier_name, COALESCE(SUM(sh.quantity), 0) AS total_shipped_quantity
FROM Suppliers s
LEFT JOIN Shipments sh ON s.supplier_id = sh.supplier_id
GROUP BY s.supplier_id, s.supplier_name;
English:
Suppliers
and Shipments
tables to ensure all suppliers are included, even those without any shipments.supplier_id
and supplier_name
to consolidate shipments data for each supplier.Hinglish:
Suppliers
aur Shipments
tables ke beech ek LEFT JOIN ka aarambh karta hai taaki sabhi suppliers ko shamil kiya ja sake, unko bhi jo kisi bhi shipment me shamil nahi hain.supplier_id
aur supplier_name
ke hisaab se group karta hai taaki har supplier ke liye shipments data ko consolidate kiya ja sake.