How do I get records from 2 tables that don't match a condition?

Last post 06-07-2009 5:34 PM by Naom. 5 replies.

Sort Posts:

  • How do I get records from 2 tables that don't match a condition?

    06-06-2009, 7:26 PM
    • Member
      9 point Member
    • aninona
    • Member since 03-08-2008, 3:12 PM
    • Posts 47

    Hello everyone.

     I have 2 tables:

    drivers
    (key)DriverID
    MotorbikeID

    motorbikes
    (key)MotorbikeID

    I would like to receive all the records in motorbikes table that one of the drivers doesn't have the MotorbikeID.

    for example:

    drivers

    DriverID        MotorbikeID
    123                000
    234                999
    345              (nothing)

    motorbikes

    000
    555
    999
    777

    the search has to return the records 555 and 777

     Thanks!!

  • Re: How do I get records from 2 tables that don't match a condition?

    06-06-2009, 11:09 PM
    • Member
      382 point Member
    • abonslater
    • Member since 01-04-2007, 1:57 PM
    • Chicago, IL
    • Posts 81

    SELECT *
    FROM Motorbikes
    WHERE MotorbikeID NOT IN (SELECT MotorbikeID FROM Drivers)

    Check out my ASP.NET blog at http://www.zookster.com/abonslater/blog/
  • Re: How do I get records from 2 tables that don't match a condition?

    06-06-2009, 11:21 PM
    • All-Star
      86,881 point All-Star
    • limno
    • Member since 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 4,939
    • Moderator
      TrustedFriends-MVPs

     

    declare @drivers table
    (DriverID varchar(3),MotorbikeID varchar(10) )
    
    INSERT INTO @drivers 
    SELECT '123','000' 
    UNION ALL SELECT '234','999'
    UNION ALL SELECT '345','(nothing)'
    
    declare @motorbikes table(MotorbikeID varchar(10) )
    INSERT INTO @motorbikes 
    SELECT  '000' 
    UNION ALL SELECT  '555'
    UNION ALL SELECT  '999'
    UNION ALL SELECT  '777'
     
    SELECT MotorbikeID FROM (SELECT a.MotorbikeID as MotorbikeID
    ,b.MotorbikeID bMotorbikeID FROM @motorbikes a
    LEFT JOIN @drivers b ON a.MotorbikeID=b.MotorbikeID
    WHERE b.MotorbikeID is NULL) t
     
    Limno

    Format your SQL query with instant sql formatter:
    http://www.dpriver.com/pp/sqlformat.htm
  • Re: How do I get records from 2 tables that don't match a condition?

    06-07-2009, 3:09 AM
    • Member
      382 point Member
    • abonslater
    • Member since 01-04-2007, 1:57 PM
    • Chicago, IL
    • Posts 81

    Limno,

     I'm by no means saying you are incorrect, but I'm just trying to understand your logic here compared to mine. The last statement in his questions said "the search has to return the records 555 and 777". I'm not understanding why you are creating two temp tables and doing a left join if he only wants to motorbike IDs that are not matched with a driver. Maybe I misunderstood so if so I'm hoping you can elaborate.

    Check out my ASP.NET blog at http://www.zookster.com/abonslater/blog/
  • Re: How do I get records from 2 tables that don't match a condition?

    06-07-2009, 5:33 PM
    Answer
    • All-Star
      30,775 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,819

     Select D.* from Drivers D LEFT Join  Morotbikes M on D.MotorbikeID = M.MotorbikeID where M.MotorbikeID IS NULL - this is faster than SELECT NOT IN.

    In SQL Server 2005 and up look at EXCEPT clause as well.

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: How do I get records from 2 tables that don't match a condition?

    06-07-2009, 5:34 PM
    • All-Star
      30,775 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,819

     Limno, just curious why did you introduce derived table here? :)

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
Page 1 of 1 (6 items)