www.bullraider.com

You are here: Home Database SQL Tutorial

SQL Joins

AddThis Social Bookmark Button

Joins In Sql

SQL joins are used to fetch data from two or more tables, based on  conditions between  tables.
There are various type of joins. Like
    =>Equi-Joins
   => Self-Join
   => Outer-Join
   => Semi-Join
    =>Anti-Join
   => Cross-Join
Equi-join
It is a sql join  where we use the equal sign as the comparison operator i.e "="  between two tables.By default this join  is inner-equi-join.

Examples:
Example1: Suppose we want to fetch records from two tables  emp, dept where deptno of employee is equal to dept no of dept.
The query is:

SELECT * FROM EMP E,DEPT D WHERE E.DEPTNO=D.DEPTNO;


Another way to write it,like

 

	SELECT * FROM EMP E INNER JOIN DEPT  ON D WHERE E.DEPTNO=D.DEPTNO;


Example2: Suppose there are two tables category and product.Both table contains category_id. We want to fetch the records where both tables contain same category_id .

	SELECT * FROM CATEGORIES C ,PRODUCTS P where P.CATEGORYID = C.CATEGORYID;

Another way to write it,like

	SELECT * FROM CATEGORIES C INNER JOIN PRODUCTS P ON P.CATEGORYID = C.CATEGORYID

Example3: Suppose there are two table Employee and Bonus. We want to fetch records where both tables contain same empno.

	SELECT * FROM EMPLOYEE E,BONUS B WHERE E.EMPNO=B.EMPNO;

Another way to write it,like

	SELECT * FROM EMPLOYEE INNER JOIN EBONUS B ON E.EMPNO=B.EMPNO;
Powered by Bullraider.com