select records in one table not found in another?

Posted on 2013-06-04
Last Modified: 2013-06-04

i want to select all postalcodes.CityName and postalcodes.ProvinceAbbr in dbo.postalcodes
that are not found as identical match in populations.City and populations.State

Question by:arthurh88
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Accepted Solution

chaau earned 500 total points
ID: 39221226
Select postalcodes.CityName, postalcodes.ProvinceAbbr
FROM postalcodes LEFT JOIN populations
ON postalcodes.CityName = populations.City AND postalcodes.ProvinceAbbr = populations.State
WHERE populations.City IS NULL AND populations.State IS NULL

Open in new window

LVL 41

Expert Comment

ID: 39221236
You can also try this.
select *
  from postalcodes pc
 where not exists (select 1 
                     from populations p 
					where pc.CityName = p.CityName 
					  and pc.ProvinceAbbr  = p.ProvinceAbbr)

Open in new window


Author Closing Comment

ID: 39221247

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question