Professional Documents
Culture Documents
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)
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;