Querying for unmatched records in two tables

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Querying for unmatched records in two tables

    I have two tables - One contains a list of all the zip codes in the US. The
    other table is basically a mailing list containing addresses of people both
    inside and outside of the US. I want to find invalid and non-US zip codes by
    using a query that will tell me all of the records in the mailing list table
    that have zip codes that DON'T match a zip code in the master zip code table.
    I can do it with ColdFusion by selecting all the distinct zips and looping over
    the results to check to see if they are in the zip code table, but that takes
    forever. Is there a fast and easy way to do it in SQL? Any ideas? Thanks!

    OldNapkin Guest

  2. Similar Questions and Discussions

    1. Unmatched records from Join
      I have 2 tables. table1: ssn, name, date, status table2: ssn, name, dept I wanted to know if there is it possible to get all the unmatched...
    2. Querying related tables
      Simple query question - I have a real estate table with various fields (Beds, Baths, SqFt, etc). One of the fields is BrokerID, which is foreign...
    3. Querying one-to-many tables
      Hi, I have a database with, for the sake of simplicity, two tables with a one-to-many relationship. The first table is the user info and the...
    4. Querying data that matches in two different tables
      The code is attached. Basically there are two tables, contact and recruiter. When you initially add someone to contact, it also adds certain...
    5. querying a view of a lot tables
      I have setup a database to record our IP Account from ALL of our routers in a form. It's very slow and sometimes times out.. which is NO GOOD.. I...
  3. #2

    Default Re: Querying for unmatched records in two tables

    Perhaps something like this:

    SELECT *
    FROM mailing_list m
    WHERE NOT EXISTS(SELECT 1
    FROM zip_table z
    WHERE z.zip_code = m.zip_code)

    or maybe something like this:

    SELECT m.*
    FROM mailing_list m
    INNER JOIN zip_table z
    ON z.zip_code <> m.zip_code

    Phil

    paross1 Guest

  4. #3

    Default Re: Querying for unmatched records in two tables

    Use a LEFT JOIN or whatever your particular database engine calls it. For
    example, in MS-Access you can get one in Design Mode by relating ZipCode from
    your people table to ZipCode in your zip table. You then right-click on the
    line that connects the two tables select Join Properties and select the radio
    button that says something like 'Return all rows from people and only those
    rows from zip where the join keys are equal'. Make sure to also include the
    ZIPCode from the zip table as one of the Returned columns. When you run this
    query, the ZIPCode from the zip table will be a NULL whenever there is no
    match. You can also include a WHERE ziptable.ZipCode IS NULL to get only the
    mismatches (peopletable.ZipCode). If you just want a list of the mismatches
    rather than each mismatched record, add in a GROUP BY peopletable.ZipCode.

    JMGibson3 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139