5. Multiple tables: Step 12

Demo

Related data in more than two tables.

To get a set of related data spread among more than two tables we can simply list all the tables required in the FROM clause and write a WHERE clause which selects the required combined rows.

E.g. Show the employee name, the managers name and the employee's department:

SQL

SELECT e.ename, m.ename, d.dname FROM emp e, emp m , dept d WHERE e.mgr = m.empno AND e.deptno = d.deptno;

Output

ename ename dname
CLARK KING Accounting
MILLER CLARK Accounting
SMITH FORD Research
JONES KING Research
SCOTT JONES Research
ADAMS SCOTT Research
FORD JONES Research
ALLEN BLAKE Sales
WARD BLAKE Sales
MARTIN BLAKE Sales
BLAKE KING Sales
TURNER BLAKE Sales
JAMES BLAKE Sales

Progress

Proceed to Next step