SQL Joins – INNER JOIN

SQL JOINS

SQL – Joins are used to fetch data collectivly from two or more than two tables. It Selects column values from the two tables & results it as combined data.

SQL Joins (INNER JOIN)

Sql INNER JOIN Selects the data from Both the tables only the matching records from both table.

Create An Order Table Which We will Use for joins.

Order Table Used for JOINS

CREATE TABLE IF NOT EXISTS `Orders` (
`OrderId` int(11) NOT NULL,
`UserId` int(11) NOT NULL,
`OrderItem` varchar(100) NOT NULL,
`Sysdate` datetime NOT NULL
) ;

ALTER TABLE `orders`
ADD PRIMARY KEY (`OrderId`);

INSERT INTO `customersdb`.`orders` (`orderId`, `userId`, `orderItem`, `Sysdate`) VALUES (‘1’, ‘2’, ‘Tees’, ‘2015-03-19 00:00:00’);
INSERT INTO `customersdb`.`orders` (`orderId`, `userId`, `orderItem`, `Sysdate`) VALUES ( ‘2’,’3′, ‘Shoes’, ‘2015-03-20 00:00:00’);
INSERT INTO `customersdb`.`orders` (`orderId`, `userId`, `orderItem`, `Sysdate`) VALUES (‘3′,’5’, ‘Shirt’, ‘2015-03-21 00:00:00’);
INSERT INTO `customersdb`.`orders` (`orderId`, `userId`, `orderItem`, `Sysdate`) VALUES (‘4’, ‘6’, ‘Watch’, ‘2015-03-21 00:00:00’);
INSERT INTO `customersdb`.`orders` (`orderId`, `userId`, `orderItem`, `Sysdate`) VALUES (‘5′,’8’, ‘Ipad’, ‘2015-03-22 00:00:00’);

Now We Have The Following Tables :

Orders Table :

OrderTable
Note : “UserId” In “Orders” Table is the id of the users which is assigned to users in “Users” Table in “ID” column. The Values stored in “UserId” Column are reffered from “Users” Table.

And

Users Table :

tableFull

Simple Join Example Can Be Explained Using The Above Two Tables :

Example

SELECT users.Name,Users.Email,Orders.OrderItem FROM Users JOIN Orders ON (Users.ID= Orders.UserId);

Note : Simple JOIN keyword also refers to the INNER JOIN.

ON – : Condition is key for the joining two tables which related the tables using common columns reffered from the tables..

After Running The Query the following Result will be produced :

SQL Joins results


User Ans/Comments

📖 Read More


Advertisements