You are here
Home > Sql Server > SQL JOIN (INNER JOIN)

SQL JOIN (INNER JOIN)

SQL JOIN

Inner Join or Equi Join:

  • Inner join selects data that have matching values in tables.
  • Inner join is same as join, so we can use either inner join or join keyword.
  • Syntax:
  • select column_name(s)

    from table_name1

    inner join table_name2

    on  table_name1.column_name=  table_name2.column_name

     

    OR

     

    select column_name(s)

    from table_name1

    join table_name2

    on  table_name1.column_name=  table_name2.column_name

  • Database Table: Customers

Cust_id

Name Age City
1 Prince 22

Anand

2

Komal 44 Pune
3 Khusi 30

Delhi

4

Anil 45 Mumbai
5 Abhi 27

Ahmedabad

6

Juhi 35

Baroda

  • Here, Cust_id is primary key.
  • Database Table: Orders

Order_id

Order_no Price Cust_id
1 1111 1100

2

2

2244 227 2
3 7777 677

4

4

4466 449 4
5 8888 1700

6

  • Here, Cust_id is foreign key and Order_id is primary key.
  • Example:
  • select C.Name, C.Age, C.City,

    O.Order_id, O.Order_no, O.Price

    from Customers as C

    inner join Orders as O

    on C.Cust_id=O.Cust_id;

  • Output:

Name

Age City Order_id Order_no Price
Komal 44 Pune 1 1111

1100

Komal

44 Pune 2 2244 227
Anil 45 Mumbai 3 7777

677

Anil

45 Mumbai 4 4466 449
Juhi 35 Baroda 5 8888

1700

  • Join three tables:
  • Database Table: Customers

Cust_id

Name Age City
1 Prince 22

Anand

2

Komal 44 Pune
3 Khusi 30

Delhi

4

Anil 45 Mumbai
5 Abhi 27

Ahmedabad

6

Juhi 35

Baroda

  • Here, Cust_id is primary key.
  • Database Table: Orders

Order_id

Order_no Price Cust_id
1 1111 1100

2

2

2244 227 2
3 7777 677

4

4

4466 449 4
5 8888 1700

6

  • Here, Cust_id is foreign key and Order_id is primary key.
  • Database Table: Details

Details_id

Details_no Order_id
1 101

2

2

102 2
3 103

4

4

104

4

  • Here, Order_id is foreign key and Details_id is primary key.
  • Example:
  • select C.Name, C.City, O.Order_no, O.Price, D.Details_no

    from Customers as C

    inner join Orders as O

    on C.Cust_id=O.Cust_id

    inner join Details as D

    on O.Order_id =D.Order_id;

  • Output:

Name

City Order_no Price Details_no
Komal Pune 2244 227

101

Komal

Pune 2244 227 102
Anil Mumbai 4466 449

103

Anil

Mumbai 4466 449

104

Keep visiting my site ask2tech.com for upcoming top technical article

Learn more about DBA at TechNet Consultancy

One thought on “SQL JOIN (INNER JOIN)

Leave a Reply

Top