Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2007, 07:46 AM
Authorized User
 
Join Date: Jun 2003
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default Joins in SQL Server

Dear Readers,

I have a question for you all for which I didn't get a suitable answer
on the net. I will really appreciate if somebody can help me out in this.

Question:
==========
We all know what left join and right join means.
Let's suppose we have a HR application which contains tables Employees
and Projects and some other tables too.

Now let's say...I need to find out the information about all employees, their projects, their
Manager names, and the start date of the project and end date of the project.

I write a query like this...

Select emp.EmployeeName, proj.ProjectName, emp.ManagerName, proj.StartDate, proj.EndDate
FROM Employees emp
        LEFT JOIN Projects proj
        ON emp.EmployeeID = proj.EmployeeID

This will return me all records from Employees tables and only matching records
from Projects table. This will also show those employees who aren't allocated to any projects.

I can modify the above query by RIGHT JOIN (ing) the Employees table with Projects table to get the same result.

Then why do we have two different join types when I can use either a LEFT JOIN or a RIGHT JOIN to suffice all situations.

Can somebody clarify on this??

TIA,
Debsoft
__________________
cheers,
debsoft
 
Old May 2nd, 2007, 07:57 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It has to do with the types of records that are returned.

For example, an Inner Join returns data ONLY when rows exist in both tables, so in your above example if I did an INNER Join on emp.EmployeeID = proj.EmployeeID an ID must exist in both the source table and the table we are joining on.

A LEFT JOIN will return data even if there is no matching data in the second table. So in your example, if the Employees table contained the ID 1 and the Projects table did not have an employee_fk of 1 all of the values you are returning from the Projects table will return NULL.

A RIGHT JOIN is the exact opposite. If there is data in the second table (the table you are joining) that does not exist in your first table (the table in your FROM clause) the values from your first table will return NULL.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old May 10th, 2007, 08:23 AM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

USE THIS ...

Select emp.EmployeeName, proj.ProjectName, emp.ManagerName, proj.StartDate, proj.EndDate
FROM Employees emp
        INNER JOIN Projects proj ON emp.EmployeeID = proj.EmployeeID



With Regards,
Muralidharan.D





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL - Help with Joins oldmuttonhead Classic ASP Databases 0 January 21st, 2006 05:02 PM
SQL Joins in XPath arcuza XSLT 0 March 16th, 2005 01:05 PM
Union joins in SQL sellis Access 3 April 7th, 2004 12:25 AM
SQL Table Joins Dredd Classic ASP Databases 2 June 3rd, 2003 06:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.