Home > Database > Mysql Tutorial > How to Find Disjoint Records in SQL Using ID Exclusion?

How to Find Disjoint Records in SQL Using ID Exclusion?

Linda Hamilton
Release: 2025-01-05 09:16:39
Original
826 people have browsed it

How to Find Disjoint Records in SQL Using ID Exclusion?

Finding Disjoint Records with SQL: ID Exclusion

Consider a scenario where you have two tables with primary key bindings and wish to identify the disjoint set of records. Suppose you have two tables, Table1 and Table2, with ID serving as the primary key. Your objective is to retrieve the row from Table1 whose ID is not present in Table2.

To achieve this, you can employ the following SQL query:

SELECT ID, Name 
FROM Table1 
WHERE ID NOT IN (SELECT ID FROM Table2)
Copy after login

This query retrieves the ID and Name from Table1 where the ID is not among the IDs present in Table2. The NOT IN operator compares the ID column of Table1 with the ID column of Table2, excluding any matching IDs.

For instance, in your provided example, where Table1 contains the ID of John (1), Peter (2), and Mary (3), and Table2 has the ID of address2 (1) and address2 (2), the query will return the row for Mary (3) because her ID is not in Table2.

By utilizing this query, you can effectively find disjoint records between tables, allowing you to identify unmatched or missing data.

The above is the detailed content of How to Find Disjoint Records in SQL Using ID Exclusion?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template