Usage of exists,not exists in SQL

Source: Internet
Author: User

Both exists and in are representations of existence, and exists emphasizes whether to return a result set, the difference between exists and in is that the in-boot clause can only return one field.

Not exists and not are mutually exclusive conditions for exists and in.

Assuming that tables A and B, the data record of a is not present in B, and the key in output a (the output cannot contain duplicate keys), SQL is as follows:

SELECT DISTINCT

a.ID

, A.name

From

Table A

where exists

(SELECT

1

From

B

where

a.x=b.x

and A.Y=B.Y

);

Execute the above SQL to output the data record in B in A;

If you want to output a data record that is not in B, as long as you change the exists to not exists, you can change the not exists condition to the negation of the associated condition of table A and B below, namely:

a.x<>b.x

OR A.Y<>B.Y

When the condition is changed to the above conditions, the output record will be repeated output several, although the above has distinct operation, but affect the efficiency.

Usage of exists,not exists in SQL

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.