Tuesday, October 23, 2007

What kind of join am I? (example SQL code)

Create Table Fred
( TheID INTEGER
, TheName VARCHAR(20)
)

Create Table Jobs
( TheID INTEGER
, TheJob VARCHAR(20)
)

INSERT INTO Fred VALUES (1, 'Fred')
INSERT INTO Fred VALUES (2, 'Freddy')
INSERT INTO Fred VALUES (3, 'Fredrick')

INSERT INTO Jobs VALUES (1, 'Boss')
INSERT INTO Jobs VALUES (4, 'Slave')

SELECT F.TheID, F.TheName, J.TheID, J.TheJob
FROM Fred F
INNER JOIN Jobs J On J.TheID = F.TheID

SELECT F.TheID, F.TheName, J.TheID, J.TheJob
FROM Fred F
RIGHT JOIN Jobs J On J.TheID = F.TheID

SELECT F.TheID, F.TheName, J.TheID, J.TheJob
FROM Fred F
LEFT JOIN Jobs J On J.TheID = F.TheID

No comments:

Post a Comment