2. Sample Test 2.1 preparations Create two test tables table_user and table_dep (including data ): Table_user: Table_dep:
Inner join/join) If no join condition is specified for an inner join, the result of the cross join is the same as that of the Cartesian product. However, unlike the Cartesian product, the data table is not as complex as the Cartesian product, the internal join efficiency is higher than that of cartesian products. However, when using inner join, you must specify the connection conditions. In SQL Server2000, inner join is equivalent to join. -- Inner join Select * From table_user A, table_dep B Where a. dep_id = B. dep_id -- Note: The above method is SQL Server's support for relational calculus. Select * From table_user a inner join table_dep B On a. dep_id = B. dep_id -- Note: This is SQL Server's support for relational algebra (similar to the following) -- Join (this is equivalent to inner join in SQL Server) Select * From table_user a join table_dep B On a. dep_id = B. dep_id The execution results of the three statements are the same: 2.2 Outer Join ([outer] Join) Left Outer Join: The result set includes all rows in the left table specified in the left outer clause, not just the rows matched by the join column. If a row in the left table does not match a row in the right table, all the selection list columns in the right table in the row of the associated result set are null ). The right outer join is a reverse join of the left Outer Join. All rows in the right table are returned. If a row in the right table does not match a row in the left table, a null value is returned for the left table. Full (full) External join returns all rows in the left and right tables. If a row does not match a row in another table, the selection list column of the other table contains a null value. If there are matched rows between tables, the entire result set row contains the data value of the base table. -- About outer join [outer] Join Note: outer is optional -- Left Outer Join Select * From table_user a left Outer Join table_dep B On a. dep_id = B. dep_id Result: Left Outer Join
-- Right Outer Join Select * From table_user a right Outer Join table_dep B On a. dep_id = B. dep_id Result: Outer right join
-- Full outer join Select * From table_user a full outer join table_dep B On a. dep_id = B. dep_id Result: Complete (full) External Connection
2.3 cross join) Cross join is a concrete embodiment of Cartesian Product in SQL. -- Relationship algebra angle (cross join) Select * From table_user a cross join table_dep B Result: The Cartesian product of the two tables. (The data table is too large to be added !) In addition, the keyword of the Condition Clause of cross join is where, rather than the on keyword. Select * From table_user a cross join table_dep B Where a. dep_id = B. dep_id |