skip to Main Content

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

2

Answers


  1. 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;
    
    Login or Signup to reply.
  2. SELECT 
      p.product_name,
      SUM(o.quantity) AS total_quantity
    FROM Orders o 
    JOIN Products p ON p.product_id = o.product_id
    GROUP BY p.product_id
    HAVING SUM(o.quantity) > 50
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search