SQL question correllating 2 tables

  1. 5 months ago

    Dale A

    Nov 28 San Diego, California, USA

    I've been working on a program and am having trouble with correlating data from two tables.

    Both tables have the same structure but the data comes from different sources. The tables contain columns, besides their separate primary keys, named 'Name' and 'City'. Duplicate Names are allowed in a table if the City is different. What I need to do is determine what Name/City combination(s) exist in the first table but not in the second.

    I've been wrangling with this for two days now and am about ready to yank out the rest of my hair. BTW, I'm working with a SQLite database.

    Thanks.

    SELECT name , city
    FROM table_1 a
    WHERE NOT EXISTS (
        SELECT 1 
        FROM table_2 b
        WHERE a.name=b.name and a.city=b.city)
  2. Dave S

    Nov 28 Answer San Diego, California USA
    Edited 5 months ago
    SELECT name , city
    FROM table_1 a
    WHERE NOT EXISTS (
        SELECT 1 
        FROM table_2 b
        WHERE a.name=b.name and a.city=b.city)
  3. Dale A

    Nov 29 San Diego, California, USA

    Thanks, Dave. That does it and I can even see what I was doing wrong.

or Sign Up to reply!