When you want to combine two or more tables in SQL then commonly you use SQL JOIN or SQL INNER JOIN.
For example EMPLOYEE Table has
EMP_ID | EMP_NAME | HIRE_DATE | SALARY | DEPT_ID |
---|---|---|---|---|
1 | Roger Fed | 2001-05-01 | 5000 | 4 |
2 | Djoker Nova | 2002-07-15 | 6500 | 1 |
3 | Stef Graph | 2005-10-18 | 8000 | 5 |
4 | Mart Hingis | 2007-01-03 | 7200 | 3 |
5 | Andre Duplesi | 2008-06-24 | 5600 | null |
DEPARTMENT Table has
DEPT_ID | DEPT_NAME |
---|---|
1 | French Open |
2 | Wimbledon |
3 | US Open |
4 | Aus Open |
5 | Olympics Games |
Now lets try a SELECT JOIN statement
SELECT DEPT_ID, DEPT_NAME, EMP_NAME, SALARY
FROM EMPLOYEE e, DEPARTMENT d
WHERE e.DEPT_ID =d.DEPT_ID;
The Output will look like this:
DEPT_ID | DEPT_NAME | EMP_NAME | SALARY |
---|---|---|---|
4 | Aus Open | Roger Fed | 5000 |
1 | French Open | Djoker Nova | 6500 |
5 | Olympics Games | Stef Graph | 8000 |
3 | US Open | Mart Hingis | 7200 |
INNER JOIN select all records from both tables as long as there is a match between the columns in these 2 tables. If no match in DEPARTMENT table found w.r.t EMPLOYEE table then those records of DEPARTMENT table wont be shown.
Learn more : SQL Tutorial , SQL Interview Questions