Alex Deiwor
Alex Deiwor

Reputation: 117

SQL Select and find rows that not exists in other table

I have two tables with phone numbers.

I want check if there is new rows in addresbook table that not exist in users table.

As I am newbie with SQL, is this query correct?

SELECT * 
FROM addressbook
WHERE NOT EXISTS (
  SELECT Phone1
  FROM users 
  WHERE addressbook.phone = users.phone1
)

EDIT: I user MySQL with PHPMyAdmin Interface, sorry for not to specify before

Upvotes: 0

Views: 3411

Answers (2)

peter.petrov
peter.petrov

Reputation: 39477

Seems OK but I would do it like this.

select a.* from addressbook a
left outer join users u on a.phone = u.phone1
where
(u.phone1 is null)

This is simpler and probably faster.

Upvotes: 5

Vishal Patel
Vishal Patel

Reputation: 973

In SQL Server your query is

SELECT * from addressbook WHERE Phone NOT in (SELECT Phone1 from users)

Upvotes: 1

Related Questions