Skip to content Skip to sidebar Skip to footer

Display Only Those Rows, Whose Sum(price) Is More Than A Certain Number

I have 3 tables: customers(name, id), items(name, price, id), purchases(id, items_id, customers_id) When I run: select customers.name, SUM(items.price) as 'price' from i

Solution 1:

This is where you use a HAVING clause. It's similar to a WHERE clause, but applied after the GROUP BY.

HAVING SUM(items.price) > 100

Solution 2:

If you want only SUM(prices) greater than 100 include a HAVING clause as below.

select 
 customers.name,
 SUM(items.price) as "price"
from items
INNERJOIN purchases
ON items.id = purchases.item_id
INNERJOIN customers
ON purchases.customer_id = customers.id
GROUPBY customers.name
havingsum(items.price) >100

Solution 3:

Here is a solution with the WHERE function

select 
   customers.name,
   SUM(items.price) as price
from items, purchases, customers
where 
   items.id = purchases.item_id and purchases.customer_id = customers.id and 
   items.price > 100GROUPBY customers.name
ORDERBY price;

Post a Comment for "Display Only Those Rows, Whose Sum(price) Is More Than A Certain Number"