SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL

SQL INNER join query for MySQL

INNER join SQL command is mostly used to join one table to it self. The biggest advantage of doing this is to get linking information from the same table. We will try to understand this with an example. The best example of INNER join will be employee table where we will keep the employee and its manager as a single record. This way by linking to the table it self we will generate a report displaying it as two linked tables. Each record will have one additional field storing the data of the manager by keeping the employee ID and we will use M_ID ( manager ID ) to link with main employee ID. This way we will link two virtual tables generated from one main table. Here is the table. You can download /copy the sql dump file to create your own MySQL table for testing.
Main tableManagersEmployee
id name m_id
1 John 2
2 Greek Tor 1
3 Alex John 0
4 Mike tour 1
5 Brain J 3
6 Ronald 3
7 Kin 4
8 Herod 3
9 Alen 2
10 Ronne 1
id emp_name
2 Greek Tor
3 Alex John
1 John
3 Alex John
3 Alex John
4 Mike tour
3 Alex John
2 Greek Tor
1 John
id emp_name
1 John
2 Greek Tor
3 Alex John
4 Mike tour
5 Brain J
6 Ronald
7 Kin
8 Herod
9 Alen
10 Ronne
Main Table (emp ): Table with id , name and m_id. Each employ has one unique id and one m_id ( manager id which is part of id field )

Note that we have only one table main table and other two Managers and Employee reports are generated out of the main table only.

In the table you can see every record has one manager id field known as m_id. We have used the unique id of the employee in the m_id field to mark who is the manager for the employee.
To generate the manager table we have used this SQL

SELECT t1.id,t1.name as emp_name from emp as t1 INNER JOIN emp as t2 on t1.id=t2.m_id

Employee report

To generate employee report we will use the matching id of employee. Note the id =3 , here since we kept him without boss but still as an employee we can display.

To generate the employee report we have used this SQL

SELECT t1.id,t1.name as emp_name from emp as t1 INNER JOIN emp as t2 on t2.id=t1.id

Employee Manager report

Now let us use inner join to create one report to display who is the manager of which employee. Check this SQL

SELECT t1.id, t1.name as emp_name, t2.name as manager FROM emp as t1 INNER JOIN emp as t2 on t2.id = t1.m_id

Note : Here record with id=3 is missing as Alex John does not have any Manager.
id emp_name manager
1 John Greek Tor
2 Greek Tor Alex John
4 Mike tour John
5 Brain J Alex John
6 Ronald Alex John
7 Kin Mike tour
8 Herod Alex John
9 Alen Greek Tor
10 Ronne John

CREATE TABLE IF NOT EXISTS `emp` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL DEFAULT '',
  `m_id` int(4) DEFAULT '0',
  UNIQUE KEY `id` (`id`)
) ;

--
-- Dumping data for table `emp`
--

INSERT INTO `emp` (`id`, `name`, `m_id`) VALUES
(1, 'John', 2),
(2, 'Greek Tor', 3),
(3, 'Alex John', 0),
(4, 'Mike tour', 1),
(5, 'Brain J', 3),
(6, 'Ronald', 3),
(7, 'Kin', 4),
(8, 'Herod', 3),
(9, 'Alen', 2),
(10, 'Ronne', 1);

Number of User Comments : 23


Google+

sajid29-03-2009
this is very easy sum
Lane11-06-2009
thank you, this was very helpful.
Niyas21-08-2009
Your Website is Very Helpful for programmers like us, The way of giving examples was superb.
lavanya27-08-2009
gud website for beginners
Ptarmigan11-10-2009
a fave website: love anti-style style: craiglist for webbies
Vinod09-11-2009
Thanks a lot, i got the complete idea of Inner join.
Karu24-11-2009
How to create Main Table or Inner joint. Please give sql in table level
vimal03-02-2010
Thanks i got the complete idea of Inner join.
Zameer16-02-2010
Plus2net is one of the best source to learn :)
fei02-03-2010
this all tutorial in your web in very helpfull for me. and for beginner programers, thanks
ann11-03-2010
thanks..!!gud website for beginners...
Narasimha Varman12-04-2010
thank you plus2net. Its very helpful. Examples shown here are live one.
Garrettraj16-04-2010
Thanks.....i got the complete idea of Limits.Examples shown here are live one.
jacob25-07-2010
thanks a lot its wonderful explanation of inner join .................
ashoks09-10-2010
Thanks i got the complete idea of Inner join.
PJ4YOU31-10-2010
This is a brilliant way to explain,But I have a question.What is the difference between INNER and SELF Join?
priyalogasamy18-03-2011
i need to select account numbers from 1 table by checking conditions in other 2 tables
surendra23-09-2011
Thank you ,i got the full idea about inner join
Prashant Sahu24-11-2011
Can you tell me differnce between self join, equi join and inner join?
Vikas Yadav15-05-2012
The representation method is superb..Every biggner can understand easily...Very very thanks ...Vikas
Jyodentist03-08-2012
Thank you,Good explanation with example.
Subhash Patel16-08-2012
its learnt by me easily from here.......nicee....
Mainuddin Bhuiyan31-01-2014
Go ahead...........Don't think ...site is useless

I am use this site 3to 5 times in every week.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer