Sunday 31 January 2016

SQL Inner Join

No doubt that one of the most common programming questions of all time is What are SQL joins?  and what so ever. 
In this article i will try my very best to explain what a SQL inner join is. Basically an inner SQL join is used to combine all rows of  two tables where the condition is met.



The common syntax of an inner SQL join is as below

SELECT column_name(s)
FROM table1INNER JOIN table2ON table1.column_name=table2.column_name;


So guys the very first thing is that lets suppose we have two tables. Table 1 is ORDERS and Table 2 is CUSTOMERS  Both of the tables are given below.


A section from CUSTOMERS table is

 A section from ORDERS table is


Now if we observe both tables closely we can see that there is a connection between the two the "CustomerID" in the CUSTOMERS(Primary key)  table is acting as a Foreign Key in the ORDERS table. Now to begin with query of joins on these tables we should write as

SELECT Customers.CustomerName, Orders.OrderID
FROM CustomersINNER JOIN OrdersON Customers.CustomerID=Orders.CustomerID;


The result would be all the rows meeting the condition as shown below:



Remember a join always return the data "rows" and not the "columns" this is the mistake most guys make and expect the same number of columns.

I hope you like the article and its of use to you dont forget to share your views as i will highly appreciate them Thanks!.


No comments:

Post a Comment