You are on page 1of 6

create table emp(id int,name varchar(20),dept_id int,salary int);

create table dept(dept_id int,dname varchar(20),location varchar(20));

insert into emp values(101,'senthil',20,20000);


insert into emp values(102,'ramu',10,40000);
insert into emp values(103,'sarathi',10,50000);
insert into emp values(104,'saji',30,27000);
insert into emp values(105,'sindhu',60,33000);
insert into emp values(106,'sathish',50,16000);

insert into dept values(10,'IT','bangalore');


insert into dept values(20,'sales','Mangalore');
insert into dept values(30,'finance','delhi');
insert into dept values(40,'accounts','kerala');
insert into dept values(60,'marketing','mysore');
mysql> select * from emp;
+------+---------+---------+--------+
| id | name | dept_id | salary |
+------+---------+---------+--------+
| 101 | senthil | 20 | 20000 |
| 102 | ramu | 10 | 40000 |
| 103 | sarathi | 10 | 50000 |
| 104 | saji | 30 | 27000 |
| 105 | sindhu | 60 | 33000 |
| 106 | sathish | 50 | 16000 |
+------+---------+---------+--------+
6 rows in set (0.00 sec)

mysql> select * from dept;


+---------+-----------+-----------+
| dept_id | dname | location |
+---------+-----------+-----------+
| 10 | IT | bangalore |
| 20 | sales | Mangalore |
| 30 | finance | delhi |
| 40 | accounts | kerala |
| 60 | marketing | mysore |
+---------+-----------+-----------+
5 rows in set (0.00 sec)
natural join:
*******************
mysql> select * from emp natural join dept;
+---------+------+---------+--------+-----------+-----------+
| dept_id | id | name | salary | dname | location |
+---------+------+---------+--------+-----------+-----------+
| 20 | 101 | senthil | 20000 | sales | Mangalore |
| 10 | 102 | ramu | 40000 | IT | bangalore |
| 10 | 103 | sarathi | 50000 | IT | bangalore |
| 30 | 104 | saji | 27000 | finance | delhi |
| 60 | 105 | sindhu | 33000 | marketing | mysore |
+---------+------+---------+--------+-----------+-----------+
5 rows in set (0.02 sec)

cross join:
****************
mysql> select * from emp cross join dept;
+------+---------+---------+--------+---------+-----------+-----------+
| id | name | dept_id | salary | dept_id | dname | location |
+------+---------+---------+--------+---------+-----------+-----------+
| 101 | senthil | 20 | 20000 | 10 | IT | bangalore |
| 101 | senthil | 20 | 20000 | 20 | sales | Mangalore |
| 101 | senthil | 20 | 20000 | 30 | finance | delhi |
| 101 | senthil | 20 | 20000 | 40 | accounts | kerala |
| 101 | senthil | 20 | 20000 | 60 | marketing | mysore |
| 102 | ramu | 10 | 40000 | 10 | IT | bangalore |
| 102 | ramu | 10 | 40000 | 20 | sales | Mangalore |
| 102 | ramu | 10 | 40000 | 30 | finance | delhi |
| 102 | ramu | 10 | 40000 | 40 | accounts | kerala |
| 102 | ramu | 10 | 40000 | 60 | marketing | mysore |
| 103 | sarathi | 10 | 50000 | 10 | IT | bangalore |
| 103 | sarathi | 10 | 50000 | 20 | sales | Mangalore |
| 103 | sarathi | 10 | 50000 | 30 | finance | delhi |
| 103 | sarathi | 10 | 50000 | 40 | accounts | kerala |
| 103 | sarathi | 10 | 50000 | 60 | marketing | mysore |
| 104 | saji | 30 | 27000 | 10 | IT | bangalore |
| 104 | saji | 30 | 27000 | 20 | sales | Mangalore |
| 104 | saji | 30 | 27000 | 30 | finance | delhi |
| 104 | saji | 30 | 27000 | 40 | accounts | kerala |
| 104 | saji | 30 | 27000 | 60 | marketing | mysore |
| 105 | sindhu | 60 | 33000 | 10 | IT | bangalore |
| 105 | sindhu | 60 | 33000 | 20 | sales | Mangalore |
| 105 | sindhu | 60 | 33000 | 30 | finance | delhi |
| 105 | sindhu | 60 | 33000 | 40 | accounts | kerala |
| 105 | sindhu | 60 | 33000 | 60 | marketing | mysore |
| 106 | sathish | 50 | 16000 | 10 | IT | bangalore |
| 106 | sathish | 50 | 16000 | 20 | sales | Mangalore |
| 106 | sathish | 50 | 16000 | 30 | finance | delhi |
| 106 | sathish | 50 | 16000 | 40 | accounts | kerala |
| 106 | sathish | 50 | 16000 | 60 | marketing | mysore |
+------+---------+---------+--------+---------+-----------+-----------+
30 rows in set (0.00 sec)
Equi Join or Inner join:
**********************
mysql> select name from emp join dept on emp.dept_id=dept.dept_id;
+---------+
| name |
+---------+
| senthil |
| ramu |
| sarathi |
| saji |
| sindhu |
+---------+
5 rows in set (0.00 sec)

mysql> select name from emp ,dept where emp.dept_id=dept.dept_id;


+---------+
| name |
+---------+
| senthil |
| ramu |
| sarathi |
| saji |
| sindhu |
+---------+
5 rows in set (0.00 sec)

mysql> select name from emp join dept using (dept_id);


+---------+
| name |
+---------+
| senthil |
| ramu |
| sarathi |
| saji |
| sindhu |
+---------+
5 rows in set (0.00 sec)

mysql> select dept_id from emp ,dept where emp.dept_id=dept.dept_id;


ERROR 1052 (23000): Column 'dept_id' in field list is ambiguous

mysql> select emp.dept_id from emp ,dept where emp.dept_id=dept.dept_id;


+---------+
| dept_id |
+---------+
| 20 |
| 10 |
| 10 |
| 30 |
| 60 |
+---------+
5 rows in set (0.00 sec)

using alias name :


***********
mysql> select name from emp e join dept d on e.dept_id=d.dept_id;
+---------+
| name |
+---------+
| senthil |
| ramu |
| sarathi |
| saji |
| sindhu |
+---------+
5 rows in set (0.00 sec)
mysql> select e.* from emp e join dept d on e.dept_id=d.dept_id;
+------+---------+---------+--------+
| id | name | dept_id | salary |
+------+---------+---------+--------+
| 101 | senthil | 20 | 20000 |
| 102 | ramu | 10 | 40000 |
| 103 | sarathi | 10 | 50000 |
| 104 | saji | 30 | 27000 |
| 105 | sindhu | 60 | 33000 |
+------+---------+---------+--------+
5 rows in set (0.00 sec)

mysql> select e.*,d.* from emp e join dept d on e.dept_id=d.dept_id;


+------+---------+---------+--------+---------+-----------+-----------+
| id | name | dept_id | salary | dept_id | dname | location |
+------+---------+---------+--------+---------+-----------+-----------+
| 101 | senthil | 20 | 20000 | 20 | sales | Mangalore |
| 102 | ramu | 10 | 40000 | 10 | IT | bangalore |
| 103 | sarathi | 10 | 50000 | 10 | IT | bangalore |
| 104 | saji | 30 | 27000 | 30 | finance | delhi |
| 105 | sindhu | 60 | 33000 | 60 | marketing | mysore |
+------+---------+---------+--------+---------+-----------+-----------+
5 rows in set (0.00 sec)
mysql> select e.salary,d.dname from emp e join dept d on e.dept_id=d.dept_id;
+--------+-----------+
| salary | dname |
+--------+-----------+
| 20000 | sales |
| 40000 | IT |
| 50000 | IT |
| 27000 | finance |
| 33000 | marketing |
+--------+-----------+
5 rows in set (0.00 sec)
NON-EQUI JOIN:
****************
mysql> select e.salary,d.dname from emp e join dept d on e.dept_id<>d.dept_id;
+--------+-----------+
| salary | dname |
+--------+-----------+
| 20000 | IT |
| 20000 | finance |
| 20000 | accounts |
| 20000 | marketing |
| 40000 | sales |
| 40000 | finance |
| 40000 | accounts |
| 40000 | marketing |
| 50000 | sales |
| 50000 | finance |
| 50000 | accounts |
| 50000 | marketing |
| 27000 | IT |
| 27000 | sales |
| 27000 | accounts |
| 27000 | marketing |
| 33000 | IT |
| 33000 | sales |
| 33000 | finance |
| 33000 | accounts |
| 16000 | IT |
| 16000 | sales |
| 16000 | finance |
| 16000 | accounts |
| 16000 | marketing |
+--------+-----------+
25 rows in set (0.00 sec)

mysql> select e.salary,d.dname from emp e join dept d on e.dept_id>=d.dept_id;


+--------+-----------+
| salary | dname |
+--------+-----------+
| 20000 | IT |
| 20000 | sales |
| 40000 | IT |
| 50000 | IT |
| 27000 | IT |
| 27000 | sales |
| 27000 | finance |
| 33000 | IT |
| 33000 | sales |
| 33000 | finance |
| 33000 | accounts |
| 33000 | marketing |
| 16000 | IT |
| 16000 | sales |
| 16000 | finance |
| 16000 | accounts |
+--------+-----------+
16 rows in set (0.01 sec)

mysql> select e.salary,d.dname from emp e join dept d on e.dept_id>d.dept_id;


+--------+----------+
| salary | dname |
+--------+----------+
| 20000 | IT |
| 27000 | IT |
| 27000 | sales |
| 33000 | IT |
| 33000 | sales |
| 33000 | finance |
| 33000 | accounts |
| 16000 | IT |
| 16000 | sales |
| 16000 | finance |
| 16000 | accounts |
+--------+----------+
11 rows in set (0.00 sec)

mysql> select e.salary,d.dname from emp e join dept d on e.dept_id<d.dept_id;


+--------+-----------+
| salary | dname |
+--------+-----------+
| 20000 | finance |
| 20000 | accounts |
| 20000 | marketing |
| 40000 | sales |
| 40000 | finance |
| 40000 | accounts |
| 40000 | marketing |
| 50000 | sales |
| 50000 | finance |
| 50000 | accounts |
| 50000 | marketing |
| 27000 | accounts |
| 27000 | marketing |
| 16000 | marketing |
+--------+-----------+
14 rows in set (0.00 sec)

mysql> select e.salary,d.dname from emp e join dept d on e.dept_id<=d.dept_id;


+--------+-----------+
| salary | dname |
+--------+-----------+
| 20000 | sales |
| 20000 | finance |
| 20000 | accounts |
| 20000 | marketing |
| 40000 | IT |
| 40000 | sales |
| 40000 | finance |
| 40000 | accounts |
| 40000 | marketing |
| 50000 | IT |
| 50000 | sales |
| 50000 | finance |
| 50000 | accounts |
| 50000 | marketing |
| 27000 | finance |
| 27000 | accounts |
| 27000 | marketing |
| 33000 | marketing |
| 16000 | marketing |
+--------+-----------+
19 rows in set (0.00 sec)

RIGHT JOIN:
****************
mysql> select e.*,d.* from emp e right join dept d on e.dept_id=d.dept_id;
+------+---------+---------+--------+---------+-----------+-----------+
| id | name | dept_id | salary | dept_id | dname | location |
+------+---------+---------+--------+---------+-----------+-----------+
| 102 | ramu | 10 | 40000 | 10 | IT | bangalore |
| 103 | sarathi | 10 | 50000 | 10 | IT | bangalore |
| 101 | senthil | 20 | 20000 | 20 | sales | Mangalore |
| 104 | saji | 30 | 27000 | 30 | finance | delhi |
| NULL | NULL | NULL | NULL | 40 | accounts | kerala |
| 105 | sindhu | 60 | 33000 | 60 | marketing | mysore |
+------+---------+---------+--------+---------+-----------+-----------+
6 rows in set (0.00 sec)
LEFT JOIN:
****************
mysql> select e.*,d.* from emp e left join dept d on e.dept_id=d.dept_id;
+------+---------+---------+--------+---------+-----------+-----------+
| id | name | dept_id | salary | dept_id | dname | location |
+------+---------+---------+--------+---------+-----------+-----------+
| 101 | senthil | 20 | 20000 | 20 | sales | Mangalore |
| 102 | ramu | 10 | 40000 | 10 | IT | bangalore |
| 103 | sarathi | 10 | 50000 | 10 | IT | bangalore |
| 104 | saji | 30 | 27000 | 30 | finance | delhi |
| 105 | sindhu | 60 | 33000 | 60 | marketing | mysore |
| 106 | sathish | 50 | 16000 | NULL | NULL | NULL |
+------+---------+---------+--------+---------+-----------+-----------+

SELF JOIN:
******************
select e.eid from emp e join e1 where e1.emp=e.mgr;

You might also like