Find the correct answer in the following query

  • 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

  • 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/

  • No i am planning to take the exam.So preparing.If you know the answer can you clear my doubt?

    Thank you

  • 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

  • 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


    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/

  • 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


    --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!

  • 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
  • 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?

  • 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
  • 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/

  • 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.

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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