| Start | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Inner Join
It can also be simplified as just Example Using inner join with left outer join (Substitute for Not exists) This query will return data from table 1 where fields matching with table2 with a key and data not in Table 1 when comparing with Table2 with a condition and key |
||||||||||||||||||||
| Outer Join Left Outer Join
Right Outer Join
Full Outer Join
Examples LEFT OUTER JOIN Results for RIGHT OUTER JOIN Results for FULL OUTER JOIN Results for |
||||||||||||||||||||
| Using Join in an Update Joins can also be used in an Update the
Update with left outer join statements Update tables with inner join and aggregate function |
||||||||||||||||||||
| Join on a Subquery Joining on a subquery is often used when you want to get aggregate data (such as Count, Avg, Max, or Min) from a child/details table and display that along with records from the parent/header table. For example, you may want to retrieve the top/first child row based on Date or Id or maybe you want a Count of all Child Rows or an Average. This example uses aliases which makes queries easier to read when you have multiple tables involved. In this case we are retrieving all rows from the parent table Purchase Orders and retrieving only the last (or most recent) child row from the child table PurchaseOrderLineItems. This example assumes the child table uses incremental numeric Id's. |
||||||||||||||||||||
| Cross Join
Example: Results: Note that there are other ways that a CROSS JOIN can be applied. This is a an "old style" join (deprecated since ANSI SQL-92) with no condition, which results in a cross/Cartesian join: This syntax also works due to an "always true" join condition, but is not recommended and should be avoided, in favor of explicit |
||||||||||||||||||||
| Self Join A table can be joined onto itself in what is known as a self join, combining records in the table with other records in the same table. Self joins are typically used in queries where a hierarchy in the table's columns is defined. Consider the sample data in a table called
Each employee's Executing this query will output the following results:
|
||||||||||||||||||||
| Accidentally turning an outer join into an inner join Outer joins return all the rows from one or both tables, plus matching rows. Left joining the tables: Returns: If you wanted to return all the people, with any applicable math scores, a common mistake is to write: This would remove Eve from your results, in addition to removing Bob's science score, as The correct syntax to remove non-Math records while retaining all individuals in the |
||||||||||||||||||||
| Delete using Join Joins can also be used in a We can delete rows from the Here |
Read Article
https://develop.horje.com/learn/1434/reference