Joinning in SQL Server

This article explains how to join tables in SQL Server.

Joining in SQL

There are various type of SQL joinning. A few of them that are commonly used are described below.

1: Inner join
2: Right Outer Join
3: Left Outer join

First Step

First create a SQL table named bio.


The following is the second table named fee.
 
2nd table

Now insert values into the bio table.

 insert values in bio table

Now insert values into the fee table.
 
 insert values in fee table

Second Step
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

 

public partial class SQLJoins : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    SqlConnection con = new SqlConnection(@"Data Source=IS9;Initial Catalog=practice;User ID=sa;Password=isource");

    protected void innerJoin_Click(object sender, EventArgs e)

    {

        con.Open();

        SqlCommand cmd=new SqlCommand ();

        cmd.Connection = con;

        cmd.CommandText = "Select * From bio Inner Join fee on bio.id=fee.id";

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();

        con.Close();

    }

Inner Join

    protected void RightOuterJoin_Click(object sender, EventArgs e)

    {

        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandText = "Select * From bio Right  Outer Join fee on bio.id=fee.id";

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();

        con.Close();   

    }

 
Outer Join 
 

    protected void LeftOuterJoin_Click(object sender, EventArgs e)

    {

        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandText = "Select * From bio Left Outer Join fee on bio.id=fee.id";

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();

        con.Close();   

    }   

}

 
Left Outer Join 


Similar Articles