orders table with columns: order_id, product_id, and quantity.
products table with columns: product_id, product_name, and price.
want to find the products total quantity greater than 50
sample
| order_id | product_id | quantity |
|---|---|---|
| 1 | 1 | 10 |
| 2 | 2 | 25 |
| 3 | 3 | 30 |
| 4 | 1 | 20 |
| 5 | 2 | 30 |
| 6 | 3 | 50 |
| product_id | product_name | price |
|---|---|---|
| 1 | Product 1 | 100 |
| 2 | Product 2 | 200 |
| 3 | Product 3 | 300 |
The expected output
| product_name | total_quantity |
|---|---|
| Product 3 | 50 |
| Product 2 | 55 |
in mysql i need sql :i need to query , not able to get result . Help is appreciated
>Solution :
Try this
SELECT product_name, SUM(quantity) AS total_quantity
FROM products
JOIN orders ON product_id = product_id
GROUP BY product_name
HAVING SUM(quantity) > 50;