Use a join to query data from more than one table .Rows in one table can be joined to rows in another table according common values existing in corresponding columns. That is usually primary and foreign key columns.
select table1.column,table2.column
from table1 , table 2
where table1.column1 = table2.column2;
1)Equijoin
equijoins are aslo called simple joins or inner joins.
for example there are two tables
- employees with columns employee_id,first_name,last_name,department_id
- departments with columns department_id,department_name,location_id
retrieving records with Equijoin
select e.employee_id,e.last_name,e.department_id,d.department_id,d.location_id
from employees e,departmnets d
where e.employee_id=d.department_id
- The select clause specifies the columns names to retirive
- employee last name,employee id and department id wich are columns in the Employees table
- department id,department name and location id which are columns in departmnets table
2. the from clause specifies the two tables that the database must access
- employees table
- department table
the where clause specifies how the tables are to be joined
e.department_id=d.department_id
becouse the department_id column is common to both tbales it must be prefixed by the table name to avoid ambuguity
No comments:
Post a Comment