SQL Server – SELECT Query & JOIN Types
The notes taken while reading MCITP Self-Paced Training Kit (Exam 70-442): Designing and Optimizing Data Access by Using Microsoft SQL Server 2005 (Self-Paced Training Kits).
From the book:
“The join relates tables based on a key column, such as primary key or a foreign key.”
JOIN Types
Performance tips from Chapter 14 — Improving SQL Server Performance related to joins:
“Try to avoid nullable foreign key columns to limit the amount of outer joins that might need to be written. Outer joins tend to be more expensive to process than inner joins. ”
“Be sure to create an index on any foreign key. Because foreign keys are used in joins, foreign keys almost always benefit from having an index.”
- INNER JOIN – default join. All matching rows will be returned.
Example:
- FULL OUTER JOIN – this join returns all matching and un-matching rows from both tables.
Example:
- LEFT OUTER JOIN – this join returns all, either matching or un-matching, rows from the left table.
Example:
- RIGHT OUTER JOIN – this join returns all, either matching or un-matching, rows from the right table.
Example:
Examples are based on the sample AdventureWorks DB available here.
More Info
Related Books
- Ultra-Fast ASP.NET: Build Ultra-Fast and Ultra-Scalable web sites using ASP.NET and SQL Server
- Advanced .NET Debugging
- Debugging Microsoft .NET 2.0 Applications
Special thanks go to my SQL mentor, SQL Customer Advisory Team member Jimmy May (blog|twitter)