Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Explanation required for join with Example.

Author  Topic 

anandchinnappan
Starting Member

7 Posts

Posted - 2010-03-18 : 16:18:02
Dear Experts,

I'm a lot confused with joins. Can you please explain joins in SQL Server 2005 in detail with example? or the websites which explains join in detail from the ground up.

Thanks in adv.

Regards
Anand

Shakazahn
Starting Member

2 Posts

Posted - 2010-03-18 : 16:42:31
I can share the little I know:

On a SELECT statement, you first say from WHERE you want to get the data from, so let's say... table_Users.

SELECT * FROM table_Users u

When you INNER JOIN with another table, you're getting only the intersection values from the two tables.

INNER JOIN table_Food f ON f.UserID = u.UserID

This will display the results of all users and all foods, ONLY the users who HAVE food. (And also the food who have users assigned to them).

LEFT JOIN table_Food f ON f.UserID = u.UserID

This will display the results of all users, regardless if they've food or not. So you may have some rows with Users with NULL on their food columns.

RIGHT JOIN table_Food f ON f.UserID = u.UserID

This will display all foods, regardless if they're assigned to users or not. Since you're selecting everything, you will have rows with NULL users but with food columns filled.

There's also some other type of joins like CROSS (a RIGHT & LEFT simultaneously) and UNION (just add everything) but usually INNER, LEFT and RIGHT are suficient for most of the common stuff you may need (non big, nasty reports).

Hope it helps!

Edit: Keep in mind the table you select mainly from affects how LEFT and RIGHT display, the first table is said to be on the LEFT (that's why LEFT checks all values from that table). So even if you want you could select mainly from table_Food and RIGHT JOIN with users and you would get the same results.
Go to Top of Page

anandchinnappan
Starting Member

7 Posts

Posted - 2010-03-18 : 16:49:06
Hai Shakazahn,

Well explained. I, now understood the core of Joins.
Thanks a lot.
Take care.
Bye

Anand
Go to Top of Page
   

- Advertisement -