|
||||||
| Database & Database Programming MySQL, Oracle, SQL, PL/SQL, ABAP, Smart Forms, and other databases and languages. A database is an organized body of related information used in many websites (including CC). |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Can someone explain to me what joins are, how to use them and why you would use them? I have heard of an inner join but have no idea what it is. Is this the only one?
__________________
I Need Help |
| Sponsored Links |
|
|
|
|||
|
Joins basically combine 2 tables. The three most common are:
INNER JOIN: intersection between 2 sets LEFT OUTER JOIN: all rows from left side, matching or NULL rows from right RIGHT OUTER JOIN: same as left, just in reverse The others are less common, and may not be available with all platforms: FULL OUTER JOIN: all rows from left side, matching or NULL rows from right; plus all rows from right side, matching or NULL rows from left (basically, a UNION ALL between LEFT and RIGHT joins...in fact, if your DB doesn't support FULL joins, you can do that) CROSS JOIN: all possible matches (the Cartesian product) between left and right side. This one is rarely useful, IME. For an example, let's start with 2 simple tables: Code:
Employee [Id] [Name] [SalaryId] 1 Mark 2 2 Jim 1 3 Bob 6 4 Steve NULL Salary [Id] [Amount] 1 $100000 2 $80000 3 $70000 Code:
SELECT Employee.Name, Salary.Amount FROM Employee INNER JOIN Salary ON Employee.SalaryId = Salary.Id [Name] [Amount] Mark $80000 Jim $100000 If we wanted to see Bob or Steve in the result set, we'd need an OUTER JOIN: Code:
SELECT Employee.Name, Salary.Amount FROM Employee LEFT OUTER JOIN Salary ON Employee.SalaryId = Salary.Id [Name] [Amount] Mark $80000 Jim $100000 Bob NULL Steve NULL Code:
SELECT Employee.Name, Salary.Amount FROM Employee RIGHT OUTER JOIN Salary ON Employee.SalaryId = Salary.Id [Name] [Amount] Mark $80000 Jim $100000 NULL $70000 Code:
SELECT Employee.Name, Salary.Amount FROM Employee FULL OUTER JOIN Salary ON Employee.SalaryId = Salary.Id [Name] [Amount] Mark $80000 Jim $100000 Bob NULL Steve NULL NULL $70000 Code:
SELECT Employee.Name, Salary.Amount FROM Employee CROSS JOIN Salary [Name] [Amount] Mark $80000 Mark $100000 Mark $70000 Jim $80000 Jim $100000 Jim $70000 ... To be honest, I think I have used a CROSS JOIN in one or two instances, but it's pretty rare and I can't think of why I did it right now. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |
Goal: 100,000 Posts
Complete: 98%