You are on page 1of 2

INNER JOIN Although the original syntax still works, there is a newer version of the join u sing the

INNER JOIN syntax. It works exactly the same as the original join, but is written slig htly different. The following syntax is for a two-table INNER JOIN: SELECT [<table-name1>.]<column-name> [[,<table-name2>.]<column-name> ] FROM <table-name1> [AS <alias-name>] [INNER] JOIN <table-name2> [AS <alias-name>] ON [<table-name1>.]<column-name> = [<table-name2>.]<column-name> [ WHERE <condition-test> ] ; There are two primary differences between the new INNER JOIN and the original jo in syntax. The first difference is that a comma (,) no longer separates the table n ames. Instead of a comma, the words INNER JOIN are used. As shown in the above syntax format, the word INNER is optional. If only the JOIN appears, it defaults to an INNER JOIN. The other difference is that the WHERE clause for the join condition is changed to an ON to declare an equal comparison on the common domain columns. If the ON is omitted, a syntax error is reported and the SELECT does not execute. So, the result is not a Carte sian product join as seen in the original syntax. Therefore, it is safer to use. Although the INNER JOIN is a slightly longer SQL statement to code, it does have advantages. The first advantage, mentioned above, is fewer accidental Cartesian product joins because the ON is required. In the original syntax, when the WHERE is omit ted the syntax is still correct. However, without a comparison, all rows of both tables are joined with each other and results in a Cartesian product. The last and most compelling advantage of the newer syntax is that the INNER JOI N and OUTER JOIN statements can easily be combined into a single SQL statement. The OU TER JOIN syntax, explanation and significance are covered in this chapter. The following is the same join that was performed earlier using the original joi n syntax. Here, it has been converted to use an INNER JOIN: SELECT cust.Customer_number ,Customer_name ,Order_number ,Order_total (FORMAT'$$$,$$9.99' ) FROM Customer_table AS cust INNER JOIN Order_table AS ord ON cust.customer_number = ord.customer_number ORDER BY 2 ; 5 Rows Returned Customer_number Customer_name Order_number Order_total 31323134 ACE Consulting 123552 $5,111.47 11111111 Billy's Best Choice 123456 $12,347.53 11111111 Billy's Best Choice 123512 $8,005.91 87323456 Databases N-U 123585 $15,231.62

57896883 XYZ Plumbing 123777 $23,454.84 Like the original syntax, more than two tables can be joined in a single INNER J OIN. Each consecutive table name follows an INNER JOIN and associated ON clause to tell wh ich columns to match. Therefore, a ten-table join has nine JOIN and nine ON clauses to identify each table and the columns being compared. There is always one less JOIN / ON combination than the number of tables referenced in the FROM.

You might also like