Click here to Skip to main content
15,878,959 members
Articles / Database Development / SQL Server / SQL Server CE
Tip/Trick

Creating SQL Joins in Simple Steps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
20 Sep 2012CPOL2 min read 19.7K   5   1
Simple steps to create SQL joins by using SQL Server 2008 Management Studio

Introduction

This tip explains how to create SQL joins in a very simple way. With this, developers need not put much effort to frame SQL Joins. This is very simple and easy to do.

Note: Please feel free to send comments... thanks in advance.

Background

The idea behind posting this tip is to provide users with a simple way of creating join queries. Most people might already know it. But still it might help newbies.

Using the Code

For this explanation, I have created two tables as follows:

Table 1. T_EMPLOYEE (T stands for table, this is the method that I use to create tables)

Below is my Employee table design:

Image 1

Table 2. T_DEPARTMENTS

Below is my Department table design:

Image 2

And please make sure that you have some rows(data) in both the tables.

Note: Relationship between fields has already been created between EMP_DEPARTMENT and DEPT_ID.

Now moving into the actual stuff about creating Joins, please follow the steps given below:

  1. Open SQL Server 2008 Management Studio
  2. Expand your database, locate "Views" and right click on that
  3. Click on "New View"
  4. The following dialog will be displayed:

    Image 3

  5. Select both the tables and click on Add button... and Close the window.. you will find the following screen with the relationship between both the tables which we made.

    Tables with relations

  6. Observe that it has pre-populated basic INNER JOIN query based on Primary and Foreign Key relation.
  7. Now for our convenience and requirement, we don't require all the fields. So Select the required fields from both the tables.
  8. I have selected the following fields. And for ease of understanding, I have given alias names too.

    Image 5

  9. That's it. Now you have your SQL query which joins two tables. Execute it, then you will find the following table as an output table.

    Image 6

  10. So, you can use this for:
    • Joining more than two tables
    • Union of tables
    • and lot more

Points of Interest

This is so simple and pretty straight forward.

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
santosh pabba20-Sep-12 7:02
santosh pabba20-Sep-12 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.