Home >Database >Mysql Tutorial >How to Find Calls Without Corresponding Phone Book Entries?
Identifying Unmatched Call Records
This guide demonstrates how to find call records in a "Call" table that lack corresponding entries in a "Phone_book" table. The goal is to identify calls from numbers not listed in the phone book. We'll explore several SQL approaches to achieve this.
Method 1: Using NOT IN
This method uses the NOT IN
clause to filter out phone numbers present in the "Phone_book" table.
<code class="language-sql">SELECT * FROM Call WHERE phone_number NOT IN (SELECT phone_number FROM Phone_book);</code>
This query selects all columns from the "Call" table where the phone_number
is not found in the list of phone_number
s from the "Phone_book" table.
Method 2: Employing NOT EXISTS
This approach utilizes a subquery with NOT EXISTS
to check for the absence of matching phone numbers.
<code class="language-sql">SELECT * FROM Call WHERE NOT EXISTS (SELECT 1 FROM Phone_book WHERE Phone_book.phone_number = Call.phone_number);</code>
The subquery searches for a match. If no match is found (NOT EXISTS
), the outer query includes the record.
Method 3: Leveraging LEFT OUTER JOIN
This method employs a LEFT OUTER JOIN
to combine both tables, then filters for null values in the "Phone_book" table.
<code class="language-sql">SELECT * FROM Call LEFT OUTER JOIN Phone_Book ON Call.phone_number = Phone_book.phone_number WHERE Phone_book.phone_number IS NULL;</code>
A LEFT OUTER JOIN
returns all rows from the "Call" table. If there's no match in "Phone_book", the corresponding columns in "Phone_book" will be NULL
. The WHERE
clause filters to show only these unmatched rows. This approach is often preferred for its performance advantages, especially with larger datasets.
The above is the detailed content of How to Find Calls Without Corresponding Phone Book Entries?. For more information, please follow other related articles on the PHP Chinese website!