Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another SQL Test question
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2014
Application:
Web
Miscellaneous
Thread ID:
01622734
Message ID:
01622744
Views:
54
>Suppose we have 2 tables....a simple vendor master and a simple orders table
>
>
>CREATE TABLE dbo.VendorMaster  (VendorPK int, VendorName varchar(100))
>CREATE TABLE dbo.OrderTable  (OrderID int, VendorFK int, OrderDate Date, OrderAmount money)
>
>INSERT INTO dbo.VendorMaster VALUES (1, 'Vendor A'), 
>                                                                     (2, 'Vendor B')
>
>INSERT INTO dbo.OrderTable VALUES (1, 1, '1-1-2008', 100), 
>                                                                  (2, 2, '1-1-2009', 50)
>
>
>Suppose I want the names of those Vendors with an order in 2008. Assume the inserts above worked fine, and I have 2 vendor rows and 2 order rows. Yes, I could do this with a JOIN. But I'll do it with a subquery. I write the following:
>
>
>SELECT * FROM dbo.VendorMaster   
>   WHERE VendorPK IN (SELECT VendorPK FROM dbo.OrderTable WHERE YEAR(OrderDate) = 2008)
>
>
>Will I get back...(choose only one answer)
>
>A) zero rows
>B) 1 row
>C) 2 rows
>D) 4 rows
>E) an error message
>
>Again, don't copy/paste the code and run it.....that would be cheating :)
>
>Is this a trick question? Give it a try!

Well, I don't have to try it. You'll get as many rows as you have in the Vendor Master table as you used VendorPK column and not a VendorFk. That phenomena was discussed just a few days ago again in MSDN SQL forum.

There are couple of things written badly.

I would use instead
select V.* from Vendors V where exists (select 1 from Orders O where O.VendorFK = V.VendorPK and
O.[Date] between '2008-01-01' and '2008-12-31')
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform