Click here to Skip to main content
15,886,362 members
Articles / Database Development / SQL Server / SQL Server 2008

Visual Representation of SQL Joins

Rate me:
Please Sign up or sign in to vote.
4.96/5 (457 votes)
3 Feb 2009CPOL3 min read 3.5M   14K   570  
This article describes SQL Joins in a visual manner, and also the most efficient way to write the visualized Joins.
This is just a simple article visually explaining SQL JOINs. In this article I am going to discuss seven different ways you can return data from two relational tables. The seven Joins I will discuss are: Inner JOIN, Left JOIN, Right JOIN, Outer JOIN, Left Excluding JOIN, Right Excluding JOIN, Outer Excluding JOIN, while providing examples of each.
CREATE TABLE Table_A
(
  PK INT NOT NULL,
  [Value] VARCHAR(10)
)
GO

CREATE TABLE Table_B
(
  PK INT NOT NULL,
  [Value] VARCHAR(10)
)
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (1, 'FOX')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (2, 'COP')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (3, 'TAXI')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (4, 'LINCOLN')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (5, 'ARIZONA')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (6, 'WASHINGTON')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (7, 'DELL')
GO

INSERT INTO Table_A
(PK, [Value]) VALUES (10, 'LUCENT')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (1, 'TROT')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (2, 'CAR')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (3, 'CAB')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (6, 'MONUMENT')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (7, 'PC')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (8, 'MICROSOFT')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (9, 'APPLE')
GO

INSERT INTO Table_B
(PK, [Value]) VALUES (11, 'SCOTCH')
GO

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Green Sand Software
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions