I need some help. I'm really new to SQL. I’m not sure what I'm missing here and why this isn’t working.
Background:
I have 2 tables related by an “ID_Key”. Each table contains an ExpDt field which represents an expiration date. These expiration dates may be different between these two tables. A record may not even exist on Table 2. Table 1 has the correct information. Database relationship has been set to Table1.ID_Key to Table2.ID_Key.
Query Objective:
I need the query display only the records that have different data in the “ExpDt” field.
Here’s the SQL code I’ve tried so far:
When I run this, the query results excludes records from Table2 that do not have a matching ID_Key with Table1 but lists all remaining records regardless if the ExpDt matches or not. Have I got the SQL code wrong? Or, maybe I created the relationship incorrectly?
Background:
I have 2 tables related by an “ID_Key”. Each table contains an ExpDt field which represents an expiration date. These expiration dates may be different between these two tables. A record may not even exist on Table 2. Table 1 has the correct information. Database relationship has been set to Table1.ID_Key to Table2.ID_Key.
Query Objective:
I need the query display only the records that have different data in the “ExpDt” field.
Here’s the SQL code I’ve tried so far:
Code:
SELECT Table1.Last_Name, Table1.First_Name, Table1.ExpDt, Table2.ExpDt
FROM Table1 LEFT JOIN Table2 ON Table1.ID_Key = Table2.ID_Key
WHERE Table1.ExpDt<>Table2.ExpDt;
When I run this, the query results excludes records from Table2 that do not have a matching ID_Key with Table1 but lists all remaining records regardless if the ExpDt matches or not. Have I got the SQL code wrong? Or, maybe I created the relationship incorrectly?