| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Self Join A table may be joined to itself, with different rows matching each other by some condition. In this use case, aliases must be used in order to distinguish the two occurrences of the table. In the below example, for each Employee in the example database Employees table (opens new window), a record is returned containing the employee's first name together with the corresponding first name of the employee's manager. Since managers are also employees, the table is joined with itself: This query will return the following data:
So how does this work?The original table contains these records:
The first action is to create a Cartesian product of all records in the tables used in the FROM clause. In this case it's the Employees table twice, so the intermediate table will look like this (I've removed any fields not used in this example):
The next action is to only keep the records that meet the JOIN criteria, so any records where the aliased
Then, each expression used within the SELECT clause is evaluated to return this table:
Finally, column names
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Differences between inner/outer joins SQL has various join types to specify whether (non-)matching rows are included in the result: Cross Join SQL Pictorial Presentation (reference (opens new window)) : Below are examples from this (opens new window) answer. For instance there are two tables as below : Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B. Inner JoinAn inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common: Left outer joinA left outer join will give all rows in A, plus any common rows in B: Right outer joinSimilarly, a right outer join will give all rows in B, plus any common rows in A: Full outer joinA full outer join will give you the union of A and B, i.e., all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JOIN Terminology: Inner, Outer, Semi, Anti... Let's say we have two tables (A and B) and some of their rows match (relative to the given JOIN condition, whatever it may be in the particular case): We can use various join types to include or exclude matching or non-matching rows from either side, and correctly name the join by picking the corresponding terms from the diagram above. The examples below use the following test data: Inner JoinCombines left and right rows that match. Left Outer JoinSometimes abbreviated to "left join". Combines left and right rows that match, and includes non-matching left rows. Right Outer JoinSometimes abbreviated to "right join". Combines left and right rows that match, and includes non-matching right rows. Full Outer JoinSometimes abbreviated to "full join". Union of left and right outer join. Left Semi JoinIncludes left rows that match right rows. Right Semi JoinIncludes right rows that match left rows. As you can see, there is no dedicated IN syntax for left vs. right semi join - we achieve the effect simply by switching the table positions within SQL text. Left Anti Semi JoinIncludes left rows that do not match right rows. WARNING: Be careful if you happen to be using NOT IN on a NULL-able column! More details here (opens new window). Right Anti Semi JoinIncludes right rows that do not match left rows. As you can see, there is no dedicated NOT IN syntax for left vs. right anti semi join - we achieve the effect simply by switching the table positions within SQL text. Cross JoinA Cartesian product of all left with all right rows. Cross join is equivalent to an inner join with join condition which always matches, so the following query would have returned the same result: Self-JoinThis simply denotes a table joining with itself. A self-join can be any of the join types discussed above. For example, this is a an inner self-join: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Left Outer Join A Left Outer Join (also known as a Left Join or Outer Join) is a Join that ensures all rows from the left table are represented; if no matching row from the right table exists, its corresponding fields are The following example will select all departments and the first name of employees that work in that department. Departments with no employees are still returned in the results, but will have NULL for the employee name: This would return the following from the example database (opens new window):
So how does this work?There are two tables in the FROM clause:
and
First a Cartesian product is created from the two tables giving an intermediate table. As this is a LEFT OUTER JOIN all records are returned from the LEFT side of the join (Departments), while any records on the RIGHT side are given a NULL marker if they do not match the join criteria. In the table below this will return Tech with
Finally each expression used within the SELECT clause is evaluated to return our final table:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Implicit Join Joins can also be performed by having several tables in the All RDBMSs support it, but the syntax is usually advised against. The reasons why it is a bad idea to use this syntax are:
The following example will select employee's first names and the name of the departments they work for: This would return the following from the example database (opens new window):
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic explicit inner join A basic join (also called "inner join") queries data from two tables, with their relationship defined in a The following example will select employees' first names (FName) from the Employees table and the name of the department they work for (Name) from the Departments table: This would return the following from the example database (opens new window):
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CROSS JOIN Cross join does a Cartesian product of the two members, A Cartesian product means each row of one table is combined with each row of the second table in the join. For example, if Using example database (opens new window) Which returns:
It is recommended to write an explicit CROSS JOIN if you want to do a cartesian join, to highlight that this is what you want. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Joining on a Subquery Joining a subquery is often used when you want to get aggregate data from a child/details table and display that along with records from the parent/header table. For example, you might want to get a count of child records, an average of some numeric column in child records, or the top or bottom row based on a date or numeric field. This example uses aliases, which arguable makes queries easier to read when you have multiple tables involved. Here's what a fairly typical subquery join looks like. In this case we are retrieving all rows from the parent table Purchase Orders and retrieving only the first row for each parent record of the child table PurchaseOrderLineItems. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CROSS APPLY & LATERAL JOIN A very interesting type of JOIN is the LATERAL JOIN (new in PostgreSQL 9.3+), The basic idea is that a table-valued function (or inline subquery) gets applied for every row you join. This makes it possible to, for example, only join the first matching entry in another table. Syntax: PostgreSQL 9.3+ left | right | inner JOIN LATERAL SQL-Server: CROSS | OUTER APPLY
Example usage (PostgreSQL 9.3+): And for SQL-Server |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FULL JOIN One type of JOIN that is less known, is the FULL JOIN. A FULL OUTER JOIN returns all rows from the left table, and all rows from the right table. If there are rows in the left table that do not have matches in the right table, or if there are rows in right table that do not have matches in the left table, then those rows will be listed, too. Example 1 : Example 2: Note that if you're using soft-deletes, you'll have to check the soft-delete status again in the WHERE-clause (because FULL JOIN behaves kind-of like a UNION); Also, if you are doing a FULL JOIN, you'll usually have to allow NULL in the WHERE-clause; forgetting to allow NULL on a value will have the same effects as an INNER join, which is something you don't want if you're doing a FULL JOIN. Example: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Recursive JOINs Recursive joins are often used to obtain parent-child data. In SQL, they are implemented with recursive common table expressions (opens new window), for example: Syntax
RemarksJoins, as their name suggests, are a way of querying data from several tables in a joint fashion, with the rows displaying columns taken from more than one table. |
Read Article
https://develop.horje.com/learn/1434/reference