Blog Post

JOIN IN SQL FAQ

,

1) What types of JOINS exist?

Inner Joins

Outer Joins (LEFT JOIN, LEFT OUTER JOIN, RIGHT JOIN, RIGHT OUTER JOIN, FULL JOIN , FULL OUTER JOIN)

Cross Joins    (aka Cartesian Join)

2) Where are JOINS used ?

Joins are implemented in either the FROM or WHERE clause

3) Why use JOINS ?

JOINS are used to connect two tables or more based on a logical relationship

4) What are some patterns of JOIN use?

One example is  a Parent – Foreign Key relationship.

Another example is using a logical operator , such as =  or <> to define  a relationship between values in columns .

5) Are there data types where columns can’t be joined directly?

Yes.   Image, ntext , text. Although they can be joined indirectly via SUBSTRING

SELECT * FROM myTable1 JOIN myTable2 ON SUBSTRING(myTable1.textCol, 1, 10) = SUBSTRING(myTable2.textCol,1,10)

6) Can NULL values be compared?

When NULLS exist  in columns and an INNER JOIN is used , NULLS will not be matched.

Nulls can only be returned by an OUTER JOIN. If they’re excluded with the WHERE clause, then they won’t be returned

7) What is the ISO standard for INNER JOINS?

The ISO 99 Standard supports the INNER JOIN in either the FROM or WHERE clause.

8) What is a Cartesian Product ?

The resultset of a CROSS JOIN without a WHERE clause.  The resultset count will be the number of rows in the first table multipled by every row in the second table.

 If 1,000,000 rows exist in the first table and 100 exist in the second the resultset count is 100,000,000

The Cartesian product is named after the Renee Descartes and is based on his work on analytic geometry

9) Can a subquery be used instead of an INNER JOIN?

It is semantically possible to use a subquery rather than an INNER JOIN. Before making the decision to use the subquery rather an INNER JOIN, analyse and report of  performance differences

 

10) Can a table JOIN to itself to create a recordset ?

 

Yes , as long as one of the table references uses a table alias. These types of JOINS are called self-join.

11) When would you use a SELF JOIN ?

Some examples are :

a)       self reference

b)       find duplicates . An alternative is ROW_NUMBER() ranking function   

Author: Jack Vamvas (http://www.sqlserver-dba.com)


Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating