I need to select products (their names) and prices records that have an above average price, but cheaper than 33
My request:
select ProductName, Price
from Products
where Price>(select avg(Price) from Products)
and
Where Price < 33;
It's not working. Why?
Thank you in advance.
Remove the duplicate where like so:
select ProductName, Price
from Products
where Price > (select avg(Price) from Products)
and Price < 33;
Related
In SQL, Please is this correct?
SELECT COUNT (Price)
FROM orders
WHERE Price > AVG(Price);
you can try sth like this
SELECT customers FROM orders WHERE price > (select AVG(price) from orders) ;
I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID
Calculate each supplier's total sales quantity and get the sales person's name if the sales person supplies parts more than 1000 units in total.
Table info:
Supplier {s_num, s_name, status, city}
Spj {s_num, p_num, j_num, qty}
This is what I have:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
AND qty > 1000
GROUP BY s_name;
I think my error is in this line:
AND qty > 1000
maybe I am using the "GROUP BY" incorrectly...
No suppliers quantity is above 800, so I get a blank result.
I want to test this:
"sum of quantity for each supplier" > 1000
Use HAVING to access aggregate results after a group by clause:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
GROUP BY s_name
HAVING qty > 1000;
See this post for more info on having vs where.
**** SOLVED ****
SELECT price
FROM inventory
WHERE price > ANY (SELECT price FROM inventory WHERE type = 'new');
Thank you to Mohammed Shafeek and every one else that commented.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am trying to check if a price is higher than any of the prices in a sub-query.
SELECT price
FROM inventory
WHERE price > price IN (SELECT price FROM inventory WHERE type = 'new');
So I want to be able to check if a price is higher than at least one of the values from the sub-query.
Any help would be much appreciated.
**** EDIT ****
Example of what i mean
$20 > $15, $30, $50
So because $20 is greater than $15 it would be selected
Would this be Min(price)
Like mentioned in below comments
Thanks in advance.
One method uses aggregation:
SELECT price
FROM inventory
WHERE price > (SELECT MAX(price) FROM inventory WHERE type = 'new');
Another uses the ALL operator:
SELECT price
FROM inventory
WHERE price > ALL (SELECT price FROM inventory WHERE type = 'new');
SELECT price
FROM inventory
WHERE price > ANY (SELECT price FROM inventory WHERE type = 'new');
I think this is the query actually u want.Check it
i have relation with three columns: ProductName, CategoryID and Price. i need to SELECT only those products which Price is higher than average product Price in given category.
(eg when apple(ProductName) is a fruit(CategoryID) it should be selected because its price is higher than average fruit price). How do i do that?
I'm thinking of something like this, but its obviously wrong since I'm comparing Price with Price and CategoryID:
SELECT ProductName, AVG(Price) FROM `products`
WHERE (SELECT AVG(Price)) > (SELECT CategoryID, AVG(Price))
GROUP BY ProductName;
Something like this
SELECT ProductName, Price, CategoryID FROM `products` p
WHERE Price > (SELECT AVG(Price) from `products` where CategoryID = p.CategoryID)