SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find the correct answer in the following query


Find the correct answer in the following query

Author
Message
iiit.raju
iiit.raju
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 161
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
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7196 Visits: 5306
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.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
iiit.raju
iiit.raju
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 161
No i am planning to take the exam.So preparing.If you know the answer can you clear my doubt?


Thank you
aaron.reese
aaron.reese
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3205 Visits: 919
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


Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7196 Visits: 5306
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
)

INSERT customer
SELECT 1 UNION ALL
SELECT 2

INSERT salesorder
SELECT 1

SELECT * FROM customer WHERE customer.customerid IN (SELECT customer.customerid FROM salesorder)

DROP TABLE customer
DROP TABLE salesorder



The query will return all the rows from the customer table


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Lowell
Lowell
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97578 Visits: 41185
also, modify Kingstons' execellent example with this:

INSERT salesorder
SELECT 3 UNION
SELECT NULL



and the query will return nothing, because the IN() list must be all non null values, otherwise, nothing gets returned.

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
GilaMonster
GilaMonster
SSC Guru
SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)

Group: General Forum Members
Points: 295847 Visits: 46685
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


aaron.reese
aaron.reese
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3205 Visits: 919
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?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)SSC Guru (295K reputation)

Group: General Forum Members
Points: 295847 Visits: 46685
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7196 Visits: 5306
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 :-)


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search