Thursday, October 24, 2013

INNER JOIN


-------------------------------------------------------------------------------
//Make database and table

-- Database: `inventory`

-- Table Name -  `orders`
--

CREATE TABLE IF NOT EXISTS `orders` (
  `order_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `order_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `orders`
--

INSERT INTO `orders` (`order_id`, `customer_id`, `order_date`) VALUES
(1, 1, '2013-10-25'),
(2, 2, '2013-10-26'),
(3, 3, '2013-10-27'),
(4, 4, '2013-10-28'),
(5, 5, '2013-10-29'),
(6, 6, '2013-10-30'),
(7, 7, '2013-10-31');

--------------------------------------------------------------------------------

//Make database and table

-- Database  Name  -  : `inventory`
-- Table Name  -   `customers`
--

CREATE TABLE IF NOT EXISTS `customers` (
  `customer_id` int(11) NOT NULL,
  `customer_name` varchar(222) NOT NULL,
  `contact_name` varchar(222) NOT NULL,
  `country` varchar(222) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `customers`
--

INSERT INTO `customers` (`customer_id`, `customer_name`, `contact_name`, `country`) VALUES
(1, 'smile Mittal', 'smile', 'india'),
(2, 'honey jindal', 'honey', 'india'),
(7, 'neha', 'n', 'n'),
(8, 'vipin', 'v', 'v'),
(9, 'l', 'l', 'l'),
(10, 'p', 'p', 'p');

---------------------------------------------------------------------------------------------

Querry of mysql for INNER JOIN

SELECT orders.order_id, customers.customer_name, orders.order_date
FROM orders
INNER JOIN customers
ON orders.customer_id=customers.customer_id;

--------------------------------------------------------------------------------------------
Querry of mysql for LEFT JOIN

SELECT customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id

ORDER BY customers.customer_name;

--------------------------------------------------------------------------------------------
Querry of mysql for RIGHT JOIN
SELECT customers.customer_name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id
ORDER BY customers.customer_name;

2 comments:

Unknown said...

I will learn how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.Thanks sir...

Unknown said...

you have learnt how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.Thanks sir....