What is the difference between the in and exist statements in SQL? (added left join and right join)

Source: Internet
Author: User

In and exists (excerpt from Baidu)
in is the appearance and the inner table as a hash connection, and exists is the external loop loop, each loop loop and then query the internal table.

If one of the two tables is smaller and one is a large table, then the subquery table is large with exists, and the subquery table is small in:
Example: Table A (small table), table B (large table)
1: SELECT * from A where CC in (select CC from B) is inefficient and uses the index of the CC column on table A;
SELECT * from A where exists (select cc from B where cc=a.cc) is efficient and uses the index of the CC column on table B.
the opposite 2: SELECT * from B where CC in (select CC from A) is highly efficient and uses the index of the CC column on table B;
SELECT * from B where exists (select cc from A where cc=b.cc) is inefficient and uses the index of the CC column on table A.
Not in and not existsIf the query statement uses not in so the appearance of the full table scan, not used index;
The index on the table can still be used by the subquery of not extsts. So no matter the table is large, using not exists is faster than not.

difference between in and =
Select name from student where name in (' Zhang ', ' Wang ', ' Li ', ' Zhao ');
Select name from student where Name= ' Zhang ' or name= ' li ' or
Name= ' Wang ' or name= ' Zhao '
The result is the same.


Left\right join is an external connection, inner join is an inner join
The external connection has the main table and from the table, the main table is left table, right is the table, the main table data will show all, from the table data only show the associated partial matching data, no matching data with null completion
Inner joins show only data that matches the conditions of the two tables
Note: The so-called Association condition refers to the condition of on

What is the difference between the in and exist statements in SQL? (added left join and right join)

Related Article

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.