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