SQL statement tip: Query for data records that exist in one table and not in another

Source: Internet
Author: User
Original: SQL statement tip: Query for data records that exist in one table and not in another

Method One (only for single fields)
Easy to understand, low efficiency with not in

Select a.ID from A where a.id not in (select ID from B)

Method Two (for multiple field matches)
Use left Join...on ..., "b.ID isnull" indicates a record in which the b.ID field is null after the

Select a.ID from A left join B in a.id=b.id where b.id is null

Method Three (for multiple field matches)

SELECT * FROM B where (select COUNT (1) as Num from A where a.id = b.id) = 0

Method Four (for multiple field matches)

SELECT * from A where NOT exists (select 1 from B where a.id=b.id)

Sync posted on my personal site: http://www.zuowenjun.cn/post/2014/10/09/54.html

SQL statement tip: Query for data records that exist in one table and not in another

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.