SQLServer-DBA.com Links

Subscribe to newsletter

Dba_db2_button

Powered by TypePad
JOIN IN SQL FAQ

19 October,2011 by Jack Vamvas

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 the FROM

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)

******** *******

Subscribe to SQLServer-DBA.com

Latest posts delivered to you daily

Delivered by FeedBurner

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer