Learning SQL association query with a small problem

Source: Internet
Author: User
Original topic: there is no technical difficulty for a leftjoin, but you may not be able to answer the correct answer if you do not want to know it clearly: the TabA table has three field IDs, Col1, in Col2, there is a table with 1, 1, and 2 Tabb data with two field IDs. Col1 has four data records. The following statement returns the following data records? If you do not write a test script

Original topic: there is no technical difficulty for a left join, but you may not be able to answer the correct answer if you do not want to know it clearly: the TabA table has three field IDs, Col1, col2 and there is a data item, 2 TabB table with two field IDs, Col1 and there are four data problems: how many data will be returned in the following statement? If you do not write a test script

Original topic:

There is no technical difficulty for a left join, but you may not be able to answer the correct answer if you do not want to know it clearly:

The TabA table has three field IDs: Col1 and Col2, with a data record of, 2.

The TabB table has two field IDs, Col1 and four data records.

Problem:

How many data entries will be returned in the following statement? Without writing a test script, if you are able to answer the answer within five minutes, and you can say something (or not guess the result by feeling ), continue with the following questions.

Select * from TabA a Left join TabB b1 on a. Col1 = b1.Col1 Left join TabB b2 on a. Col2 = b2.Col1

Extended questions:

Now, Table A adds one more row of data, 2, 3, and 4. How much data will it take to run the preceding statement? If you can answer a normal answer within 2 minutes, continue to read the questions.

Theoretical questions:

I found that many people answered the above questions incorrectly, including myself. I think there may be the following reasons:

In the first case, people cannot solve the problem within a short period of time. They can only use their own learning to remedy the problem. In the second case, they need to learn some basic theoretical knowledge, in the third case, attitude is a problem.

Concept of left join

Simply put, the left table is used as the outer loop table, and each entry goes to the inner table to find matching records. If the matching records are found, the value of join is returned. If the outer table value is not found, the value of the inner table is null. The reason for this is simply because I use the nested loop example for analysis, because it is easier for non-SQL programmers to understand, after all,. for net programmers, there are many examples of writing double or multi-layer loops. However, there are few application scenarios for hash matching and merge joins in. net programs, similar to the following two-layer loop.

Foreach (var colA in tabA) {foreach (var colB in tabB) {if (colA = colB ){......}}}

Note that the record loop in the outer table mentioned above goes to the inner table for search. Here is a problem. Let's look at this statement:

Select * from TabA a Left join TabB b1 on a. Col1 = b1.Col1

Here, TabA is the outer table, TabB is the inner table, and the outer table is a row of data. The inner table has four rows of data. From the data given above, the condition used for equivalent judgment is that the Col1 field of the outer table and the Col1 field of the inner table, when the outer table's Cole = 1 row of data is used to query the inner table, the first data in the inner table meets the condition, and the other three do not. Which of the following statements will be returned?

  • 4 records
  • A. Id a. Col1 a. Col2 B. Id B. Col1

    1 1 2 1 1

    1 1 2 null

    1 1 2 null

    1 1 2 null

  • One record
  • A. Id a. Col1 a. Col2 B. Id B. Col1

    1 1 1 2 1

    This should be understood when the data in the internal layer table is found and the difference between the data cannot be found, we need to search for the data in the outer table Col1 = 1 four times, one of the three matches indicates that the matching data is found. Therefore, only one matched data row is returned, instead, four data records are returned from the first result.

    This is a misunderstanding I encountered at the time.

    Let's look at the left join next to it.

    Select * from TabA a Left join TabB b1 on a. Col1 = b1.Col1 Left join TabB b2 on a. Col2 = b2.Col1

    This is an easy problem. When we perform the second left join operation, is the outer table the original TabA table or the result set after the first left join operation? It is easy to understand the header I listed. Here, a. Col2 is the result set after the first left join operation. (A. Id a. Col1 a. Col2 B. Id B. Col1)

    We can perform the next test. Here we use inner join for the test, because this addition is easier to perform on different business trips. Run the following statement. At this time, TabA has two pieces of data, is the 2, 3, and 4 added in the extended problem above.

    Select * from TabA a inner join TabB b1 on a. Col1 = b1.Col1 inner join TabB b2 on a. Col2 = b2.Col1

    In two steps:

    Select * from TabA a inner join TabB b1 on a. Col1 = b1.Col1

    Here, only one piece of data is returned, because the intersection returned by inner join.

    A. Id a. Col1 a. Col2 B. Id B. Col1

    1 1 2 1 1

    If the original table TablA is connected during the second join operation, the number of cyclic queries should be 2 in total, but the execution sequence can be analyzed from the execution plan diagram below.

    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.