Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    Unanswered: Issue in select query

    I have two tables t1(col1, col2, col3) and t2(col1, col2). The combination of (col1, col2) is the primary key for table t1. I want to retrieve all the records from t1 for which (col1, col2) is not present in t2.

    My query is giving some error...

    select * from t1 where (col1, col2) not in (select col1, col2 from t2)

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT t1.* 
      FROM t1 
    LEFT OUTER
      JOIN t2
        ON t2.col1 = t1.col1
       AND t2.col2 = t1.col2
     WHERE t2.col1 IS NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Or

    Code:
    SELECT * 
      FROM t1 
     WHERE NOT EXISTS (SELECT * FROM t2
    					WHERE t1.Col1 = t2.Col1
    					  AND t1.Col2 = t2.Col2)
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •