June 19, 2013 at 5:48 am
Hi,
I have two tables named customer and salesorder.
In the customer table i have 1000 customers,Of which 900 customers have orders in the salesorder table.
i execute the following query to list all customer sthat have had at least one sale.
Select * from customer where customer.CustomerID in (Select Customer.CustomerID from salesorder)
you need to identify the result of the query? which result will the query return?
1) No rows
2) A Warning message
3) The 100 rows in the customer table
4 The 900 rows in the customer table with matching rows in the salesorder table.
I am thinking the answer is 4 but some are telling that the answer is 3.So can you plese tell me the correct answer with explanation.
Thank you
June 19, 2013 at 5:58 am
Are you in the middle of some exam or is your exam over?
This does not seem to be some real time scenario that you are facing in your assignment.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 19, 2013 at 6:05 am
No i am planning to take the exam.So preparing.If you know the answer can you clear my doubt?
Thank you
June 19, 2013 at 6:16 am
you will get an error message
Select Customer.CustomerID from salesorder
table Customer has not been defined in the subquery.
The subquery should read
Select salesorder.CustomerID from salesorder
or
Select CustomerID from salesorder
There is no need to do a subquery, you could (and should) use a join
Select distinct
C.CustomerID
FROM
Customer C
INNER JOIN
salesorder S on S.customerID = C.CustomerID
June 19, 2013 at 6:32 am
aaron.reese (6/19/2013)
you will get an error message
Select Customer.CustomerID from salesorder
table Customer has not been defined in the subquery.
Are you sure? Check this
CREATE TABLE customer
(
customerid INT
)
CREATE TABLE salesorder
(
customerid INT
)
INSERTcustomer
SELECT1 UNION ALL
SELECT2
INSERTsalesorder
SELECT1
SELECT * FROM customer WHERE customer.customerid IN (SELECT customer.customerid FROM salesorder)
DROP TABLEcustomer
DROP TABLEsalesorder
The query will return all the rows from the customer table
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 19, 2013 at 6:34 am
also, modify Kingstons' execellent example with this:
INSERTsalesorder
SELECT3 UNION
SELECT NULL
and the query will return nothing, because the IN() list must be all non null values, otherwise, nothing gets returned.
Lowell
June 19, 2013 at 6:35 am
iiit.raju (6/19/2013)
I am thinking the answer is 4 but some are telling that the answer is 3.So can you plese tell me the correct answer with explanation.
Why don't you create and populate the tables, run the query and see which it is?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 19, 2013 at 6:47 am
interesting (and worrying!)
SELECT * FROM customer WHERE customer.customerid in (SELECT customer.customerid FROM salesorder)
If you just run the subquery, then you get an error.
The answer you get (two records) is wrong by any standard as customer ID 2 does not exist in the subquery table
Interestingly, if you drop the table name from the subquery
SELECT * FROM customer WHERE customer.customerid in (SELECT customerid FROM salesorder)
you get the right answer
Any idea why the first query does not throw an error?
June 19, 2013 at 6:54 am
aaron.reese (6/19/2013)
Any idea why the first query does not throw an error?
Because when you have a subquery the tables in both the outer query and subquery are in scope. It's not worrying, it's essential for correlated subqueries (and it's documented behaviour)
The query is probably written incorrectly, but the behaviour its showing (the two rows) is correct for the way the query is written.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 19, 2013 at 7:02 am
SELECT * FROM customer WHERE customer.customerid in (SELECT customer.customerid FROM salesorder)
In the above query, the term customer.customerid references the customer table outside as the customer table is still in scope
For all rows in customer table, the customerid will always match the same customerid and hence, the 2 rows are displayed as result
(SELECT customerid FROM salesorder)
In the above query the column customerid not preceded with table name as customer and hence, it becomes equivalent to salesorder.customerid
This will give you the correct result
As Gail has stated, this is documented behavior of correlated subqueries and there is nothing to worry about 🙂
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 20, 2013 at 3:34 am
Thanks for the explanation guys, I learned something today.
Actually 2 things. 1) scope and 2) yet another reason not to use subqueries
@kingston, It is a result but I would argue not the correct result. I can't imagine a scenario where you would write the query like that for the result described.
Personally I avoid subqueries whereever possible; I find they produce untidy code that is difficult to maintain and whereever possible I will use Common Table Expressions and then use an appropriate join in the main query. This has Three main benefits:
1) it keeps code clean
2) the same CTE is reusable a number of times
3) it preserves scope.
E.g Return all the details of the last order a customer placed.
SELECT
C.CustomerID,
O,*
FROM
Customer C
JOIN
(/* Last Order For Each Customer*/
SELECT CustomerID,max(orderID) from Orders O Group by O.CustomerID
) LO on LO.CustomerID = C.CustomerID
JOIN
Orders O on O.OrderID = LO.OrderID
Compared to
WITH CTE_LastOrder as
(/* Last Order For Each Customer*/
SELECT CustomerID,max(orderID) from Orders O Group by O.CustomerID
)
SELECT
C.CustomerID,
O,*
FROM
Customer C
JOIN
CTE_LastOrder LO on LO.CustomerID = C.CustomerID
JOIN
Orders O on O.OrderID = LO.OrderID
I find the second easier to read and maintain. When looking at the main code I don't have to rethink what the subquery is doing each time, I can get the gist from the suitably named CTE. I can fiddle with the CTE and test the code independently of the main query. When building very complex queries, you can build a series of CTEs that build upon one another and re-use them.
anayway I have gone WAAAAAY off topic. We should really answer the OPs question which is 100: All the customers in the customer list.
June 20, 2013 at 3:47 am
aaron.reese (6/20/2013)
Thanks for the explanation guys, I learned something today.Actually 2 things. 1) scope and 2) yet another reason not to use subqueries
Correlated subqueries can be incredibly powerful, they're not something to avoid, just to understand.
It is a result but I would argue not the correct result. I can't imagine a scenario where you would write the query like that for the result described.
The query is likely written incorrectly, however the results are correct for the way it's written.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 20, 2013 at 3:51 am
Ya i created and saw the result.
Thanks all
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply