Join query with nested queries

Join query

  1. Equijoins
  2. Their connection
  3. Outer join
  4. Multi-table joins

First, the equivalent connection

  • Connection symbol "="

Several algorithms operatively connected

  1. Nested loop method (NESTED-LOOP)
    • Two for loops corresponding to when the data amount is not efficient
    • First find first tuple in Table 1, and then traverse the table from the beginning all the tuples 2. After the splicing table and find a tuple of Table 2, Table 1 to find the second tuple, then traverse the table 2.
    • Repeating the above operation, knowing all the tuples in Table 1 have been processed.
  2. Sort-merge join algorithm (SORT-MERGE)
    • Commonly used in equi-equivalent variant of a nested loop join
    • First by connection attributes Table 1 and Table 2排序
    • Set the pointer point to a tuple in Table 1 and Table 2, in this case 表1指针为参照, 表2扫描if they meet the connection conditions, the two-tuple pointers, and splicing together a movement.
    • If not, places 表2指针为参照, , 表1扫描(ie textbook puts it: worth less after the pointer moves a tuple.)
    • Repeat the above steps ( 核心:Table 2 shifts have to scan pointer)
  3. Connection index (INDEX-JOIN)好像很少用,网上关于它的信息很少
    • Also give equivalent variants nested loop join
    • Table 2 for indexing by connecting field
    • For each tuple of a table, followed by the index look-up table 2.

Second, self-ligation

  • A table connected to its own, is a special connector
  • Since the need for a table 别名to show the difference
  • Since all attribute names are property of the same name, so you must use 别名前缀.

Example 1 . Prerequisite directly query name of each course

SELECT FIRST.Cname , SECOND.Cname
FROM Course FIRST , Course SECOND
WHERE FIRST.Cpno=SECOND.Cno;

result:
Here Insert Picture Description

Third, the external connection

Connected to the common external connection distinction :

  • Output only to meet the conditions of the general connection tuple
  • External connection to the specified table as the main connection, the subject of the table does not meet the tuple also output together (how to say it ???)
  • Left outer join:
    • All are listed on the left in relation tuples
  • Right outer join
    • Listed on the right relationships in all tuples

2 examples:

//普通连接
SELECT Student.Sno,Sname, Cno 
FROM Student , SC
WHERE Student.Sno = SC.Sno;

//左外连接
SELECT Student.Sno,Sname, Cno 
FROM Student LEFT OUT JOIN  SC ON
	(Student.Sno = SC.Sno);    //WHERE都省去了
这样写会出现 ‘OUT’ 不是可以识别的 join 选项 的报错。需要将OUT改成OUTER,或者将OUT去掉也行。

//左外连接 ,有些商业系统更简介的表达
SELECT Student.Sno,Sname, Cno 
FROM Student , SC
WHERE Student.Sno (+= SC.Sno;

Here Insert Picture Description

Fourth, multi-table joins

  • Connecting two or more tables

** example 3: ** query each student number, name, name, and the results of elective courses

SELECT Student.Sno,Sname,Cname,Grade
FROM Student,SC,Course
WHERE Student.Sno = SC.Sno
		AND SC.Cno = Course.Cno;

Here Insert Picture Description

Fifth, nested query

  • Subquery restrictions: You can not use the ORDER BY clause, because the result is used for
    irrelevant queries
  • Subquery does not depend upon the outer query, the query layer by layer from the inside out
  • 带IN谓词的子查询

[Example 3.55] query in student learning with a line "Liu Chen."
Method One: Step by Step to complete
① OK "Liu Chen" where the department name:

SELECT  Sdept  
FROM  Student                            
WHERE  Sname= '刘晨';

Results: CS
Find all CS students studying in the Department of ②:

SELECT   Sno, Sname, Sdept     
FROM   Student                 
WHERE   Sdept= 'CS';

Method two: the first step to embed the query conditions in the second step of the query

SELECT Sno, Sname, Sdept
FROM Student
WHERE Sdept IN
      (SELECT Sdept
       FROM Student
       WHERE Sname= '刘晨');

Method three: self-ligation

SELECT  S1.Sno, S1.Sname,S1.Sdept
FROM  Student S1,Student S2
WHERE  S1.Sdept = S2.Sdept 
       AND S2.Sname = '刘晨';
  • Subqueries with comparison operators
    when the inner query can know exactly when to return a single value of available comparison (>, <, =,> =, <=,! =, Or <>).
    In [Example 3.55], since a student may learn only one system, it can be replaced by = IN:
SELECT Sno,Sname,Sdept
FROM Student
WHERE Sdept =
      (SELECT Sdept
      FROM Student
      WHERE Sname='刘晨');

[Example 3.57] to identify each student over his elective course grade point average number of courses.

SELECT Sno, Cno
FROM SC x
WHERE Grade >=(SELECT AVG(Grade)
               FROM SC y
               WHERE y.Sno=x.Sno);

Correlated subquery : query subquery dependent on the parent query
first retrieve the first tuple in the outer query tables, according to its associated value to the inner layer and the inner query processing query, returns true if the WHERE clause, this is taken into the result table tuple. And then take the next tuple in the outer table. This process is repeated until all have been checked until the outer table.

Execution:
remove a tuple x SC from the outer query, the Sno value (201,215,121) tuple x is transferred to the inner query.

SELECT AVG(Grade)
FROM SC y
WHERE y.Sno='201215121;

Inner query execution, the value of 88 obtained (approx.), The value used in place of the inner query, the outer query to obtain:

SELECT Sno,Cno
FROM SC x
WHERE Grade >=88;

Execute the query results in:

201215121,1)
(201215121,3

Outer query then fetches the next tuple repeat the above step ① to ③, the outer layer SC until all tuples processed. The results are:

201215121,1)
(201215121,3)
(201215122,2
Published 22 original articles · won praise 0 · Views 145

Guess you like

Origin blog.csdn.net/weixin_42649617/article/details/104898633