DBA Hub

📋Steps in this guide1/5

SQL for Beginners (Part 5) : Joins

In this article we take a look at some of the common joins, both ANSI and non-ANSI, available in SQL.

oracle miscconfigurationintermediate
by OracleDba
13 views
1

LEFT [OUTER] JOIN

This is the fifth part of a series of articles showing the basics of SQL. In this article we take a look at some of the common joins, both ANSI and non-ANSI, available in SQL. - Setup - Introduction - [INNER] JOIN ... ON - LEFT [OUTER] JOIN - RIGHT [OUTER] JOIN - FULL [OUTER] JOIN - CROSS JOIN - NATURAL JOIN - [INNER] JOIN ... USING - Additional Joins Related articles. You can perform all these queries online for free using SQL Fiddle . The examples in this article require the following tables to be present. These tables are a variant of the EMP and DEPT tables from the SCOTT schema. You will see a lot of Oracle examples on the internet using the tables from the SCOTT schema. You can find the original table definitions in the "$ORACLE_HOME/rdbms/admin/utlsampl.sql" script. Joins are used to combine data from multiple tables to form a single result set. Oracle provides two approaches to joining tables, the non-ANSI join syntax and the ANSI join syntax, which look quite different. The non-ANSI join syntax has historically been the way you perform joins in Oracle and it is still very popular today. The tables to be joined are listed in the clause and the join conditions are defined as predicates in the clause. Even if you don't like it, you are going to have to get used to it as there is a lot of code out there that still uses it. If you are not familiar with the syntax you will struggle to bug fix any existing code and some of the examples on the internet will look rather mysterious to you. The ANSI join syntax was introduced in Oracle 9i . It has a number of advantages over the original syntax. - It reads more like English, so it is much clearer. - The tables and join conditions are all kept together in the clause, so the clause only contains filters, not join conditions. - The syntax makes it difficult, if not impossible, to forget to include the join condition. - Filters on columns from outer joined tables are handled in a much clearer manner. - It is more portable, being supported by a number of relational database engines. - It provides some functionality that is not supported directly by the non-ANSI join syntax, without using significantly more effort. Despite all these advantages, many Oracle developers still use the non-ANSI join syntax. Partly this is just because of habit. Partly this is because the Oracle optimizer transforms most ANSI join syntax into the non-ANSI join syntax equivalent before it is executed. For a beginner, my personal opinion is you should focus on the ANSI join syntax, but be aware of the non-ANSI equivalent. In this article I will show the ANSI and non-ANSI syntax for each example, where relevant. Some join methods are more popular than others, so initially focus your attention on those you are most likely to see. The most common joins you are likely to see in code are the following. - The following are less common. - The following are very rare at this point. - If a word is surrounded by "[]" it means it is an optional keyword. Without any other qualifier, a join is an inner join, so using the keyword is redundant. If the join includes the words , or , it is by definition an outer join, so the keyword is redundant. The choice to include or exclude these words is really personal preference, so follow the standard in your company, or do what feels right to you. With all that in mind, lets have a look at some examples. An combines data from two tables where there is a match on the joining column(s) in both tables. Remember, the keyword is optional. In the examples below, we are returning the DEPARTMENT_NAME and the EMPLOYEE_NAME for each employee. The OPERATIONS department has a DEPARTMENT_ID of 40, so it is not removed by the filter condition, but there are no employees in this department, so there is no match and it is not returned in the result set. Here is an example of an ANSI . Here is the non-ANSI equivalent of the previous statement. A returns all valid rows from the table on the left side of the keyword, along with the values from the table on the right side, or NULLs if a matching row doesn't exist.
Step 1
2

RIGHT [OUTER] JOIN

Using the previous example, but switching to a means we will see the OPERATIONS department, even though it has no employees. Here is an example of an ANSI . Here is the non-ANSI equivalent of the previous statement. Notice the "(+)" is used to indicate the side of the join condition that may be missing. For a multi-column join condition, each column must have the "(+)" present. Unlike the ANSI join syntax, the non-ANSI join syntax is not affected by the order of the tables. Adding filters to columns returned from an outer joined table is a common cause for confusion. If you test for a specific value, for example "salary >= 2000", but the value for the SALARY column is NULL because the row is missing, a regular condition in the WHERE clause will throw the row away, therefore defeating the object of doing an outer join. Both the ANSI and non-ANSI methods have a way of dealing with this. Using the ANSI join syntax, filters on columns from the outer joined table are included in the join itself, rather than being placed in the clause. Using the non-ANSI join syntax, the "(+)" is used to indicate a column may have a NULL value as a result of an outer join. The is the opposite of the . It returns all valid rows from the table on the right side of the keyword, along with the values from the table on the left side, or NULLs if a matching row doesn't exist. All points raised in the previous section apply here also.
Step 2
3

FULL [OUTER] JOIN

The following example has altered the order of the tables so a is now required. Remember, the non-ANSI outer join syntax is not dependent on table order, so there is no real concept of right or left outer joins, just outer joins. A combines all the rows from the tables on the left and right sides of the join. If there is a conventional match it is made. If either side has missing data, it is replaced by NULLs, rather than throwing the row away.
Step 3
4

CROSS JOIN

To see a working example, we need to add another employee who is not assigned to a department. Here is an example of an ANSI . There is no direct equivalent of a full outer join using the non-ANSI join syntax, but we can recreate it by combining two outer join queries using a , as shown below. Let's remove that extra employee so it doesn't affect any other examples. A is the deliberate creation of a Cartesian product. There are no join columns specified, so every possible combination of rows between the two tables is produced.
Step 4
5

Additional Joins

Here is an example of an ANSI . Here is the non-ANSI equivalent of the previous statement. Notice, there are no join conditions in the clause. A is a variant on an . The join columns are determined implicitly, based on the column names. Any columns that share the same name between the two tables are assumed to be join columns. Here is an example using the ANSI join syntax. There is no non-ANSI equivalent of this, as all join conditions must be specified. Using a is a bad idea. If someone adds a new column to one of the tables that happens to have the same name as a column in the other table, they may break any existing natural joins. It is effectively a bug waiting to happen. You can't apply any aliased filters to columns used in natural joins, as shown in the following example. Instead you must remove the alias, which in other circumstances would result in an ambiguous reference error. The is almost a half-way house between a conventional and a . The join is made using columns with matching names in each table, but you have to specify the columns to be used, not the whole condition. This allows you to join on a subset of the columns common to both tables. This is a safe join syntax as it can't be affected by addition of columns to either table. Similar to the , you can't apply any aliased filters to columns used in the join, but if you remove the alias it works. The CROSS APPLY and OUTER APPLY joins are available in Oracle, but they have only been supported for use in your application code from Oracle 12c onward, so you are unlikely to see them in application code for some time. For more information see: Hope this helps. Regards Tim...
Step 5

Comments (0)

Please to add comments

No comments yet. Be the first to comment!