Beginner SQL Subquery Help

  • Hello I am currently learning SQL in school and I have been having a lot of trouble trying to figure out how to create a sub query if anyone could help it would help me out greatly, thanks

    This is what I need to do,

    Query the Purchasing.PurchaseOrderHeader Table to find all PurchaseOrderId's that have the ShipMethods named ("OVERSEAS - DELUXE" or "OVERNIGHT J-FAST"). Use a subquery of the Purchasing.ShipMethod

    I have tried to do this about 40 different ways and I still can't figure it out, here are 2 different ways I tried to accomplish this,

    USE AdventureWorks2012;

    GO

    --First method

    Select ShipMethodID, Name

    FROM Purchasing.ShipMethod

    WHERE Name IN (SELECT NAME = 'OVERSEAS - DELUXE'

    OR 'OVERNIGHT J-FAST'

    FROM Purchasing.PurchaseOrderHeader);

    --Second Method

    SELECT ShipMethodID, Name

    FROM Purchasing.ShipMethod

    WHERE Id IN (SELECT Name

    FROM Purchasing.PurchaseOrderHeader

    WHERE Name = 'OVERSEAS - DELUXE');

    And of course neither is right, this has had me stuck for several days so if anyone can help thank you very much.

    -Tony

  • owninginc2k11 (10/7/2016)


    Hello I am currently learning SQL in school and I have been having a lot of trouble trying to figure out how to create a sub query if anyone could help it would help me out greatly, thanks

    This is what I need to do,

    Query the Purchasing.PurchaseOrderHeader Table to find all PurchaseOrderId's that have the ShipMethods named ("OVERSEAS - DELUXE" or "OVERNIGHT J-FAST"). Use a subquery of the Purchasing.ShipMethod

    I have tried to do this about 40 different ways and I still can't figure it out, here are 2 different ways I tried to accomplish this,

    USE AdventureWorks2012;

    GO

    --First method

    Select ShipMethodID, Name

    FROM Purchasing.ShipMethod

    WHERE Name IN (SELECT NAME = 'OVERSEAS - DELUXE'

    OR 'OVERNIGHT J-FAST'

    FROM Purchasing.PurchaseOrderHeader);

    --Second Method

    SELECT ShipMethodID, Name

    FROM Purchasing.ShipMethod

    WHERE Id IN (SELECT Name

    FROM Purchasing.PurchaseOrderHeader

    WHERE Name = 'OVERSEAS - DELUXE');

    And of course neither is right, this has had me stuck for several days so if anyone can help thank you very much.

    -Tony

    You have it backwards:

    Try this:

    select *

    from [Purchasing].[PurchaseOrderHeader]

    where ShipMethodID in (select ShipMethodID from [Purchasing].[ShipMethod] where Name in ('OVERSEAS - DELUXE','OVERNIGHT J-FAST'));

    Any questions?

  • Thank you so much

    -Tony

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply