I have a question in sql server with joins and subqueries
And the question is...
Part 1: Inner query
Write a SELECT statement that retrieves the minimum standard cost for products
grouped in colour as Blue, Yellow and Black. Make sure the standard cost is above 0.0.
Part 2: Outer query
Write a SELECT statement that retrieves the product name, list price, colour from the
product table and minimum standard cost (which will come from the inner query of part
1).
Now add an inner join statement that has the part 1 query as the table as the first part of
the expression. Make sure this is in brackets. Give this statement and alias, such as msc.
For the ON part of the join statement, make sure the colour from the inner query is the
same as the colour from the outer query AND the minimum standard cost of the msc
table, is the same as the standard cost of the product.
In part 1 the query that I have tried is...
SELECT Color, MIN(StandardCost) AS [MinimumStandardCost] FROM SalesLT.Product
WHERE Color='Blue' OR Color='Black' OR Color='Yellow'
GROUP BY Color
And it is showing me the correct answer.
In part 2 my query upto the outer query is....
SELECT Name,ListPrice,Color,StandardCost FROM SalesLT.Product
WHERE StandardCost IN(SELECT MIN(StandardCost) AS [MinimumStandardCost] FROM SalesLT.Product
WHERE Color='Blue' OR Color='Black' OR Color='Yellow'
GROUP BY Color)
a. So the problem arising in this part is when I'm using Group BY it is showing me this table....
Name ListPrice Color StandardCost
Sport-100 Helmet, Red 34.99 Red 13.08632
Sport-100 Helmet, Black 34.99 Black 13.08633
Sport-100 Helmet, Blue 34.99 Blue 13.08634
Half-Finger Gloves, S 24.49 Black 9.15935
Half-Finger Gloves, M 24.49 Black 9.15936
Half-Finger Gloves, L 24.49 Black 9.15937
Short-Sleeve Classic Jersey, S 53.99 Yellow 41.57238
Short-Sleeve Classic Jersey, M 53.99 Yellow 41.57239
Short-Sleeve Classic Jersey, L 53.99 Yellow 41.572310
Short-Sleeve Classic Jersey, XL 53.99 Yellow 41.5723
So my question is why am I seeing Red color's row when I've not given this in the inner query?
b. When I am not using GROUP BY it is showing me this table....
Name ListPrice Color StandardCost
Half-Finger Gloves, S 24.49 Black 9.15932
Half-Finger Gloves, M 24.49 Black 9.15933
Half-Finger Gloves, L 24.49 Black 9.1593
Why is it showing me only rows for the balck color???
- Edited by Smita Sil Thursday, September 03, 2015 3:58 PM