How To Join Two Tables in a Single Query?
Page 1 of 1
How To Join Two Tables in a Single Query?
Two tables can be joined together in a query in 4 ways:
Inner Join: Returns only rows from both tables that satisfy the join condition.
Left Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of rows
from the first (left) table.
Right Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of
rows from the second (right) table.
Full Outer Join: Returns rows from both tables that satisfy the join condition, the rest of rows from
the first (left) table, and the rest of rows from the second (right) table.
How To Write a Query with an Inner Join?
If you want to query from two tables with an inner join, you can use the INNER JOIN ... ON clause
in the FROM clause. The following query returns output with an inner join from two tables:
employees and departments. The join condition is that the department ID in the employees table
equals to the department ID in the departments table:
SQL> SELECT employees.first_name, employees.last_name,
departments.department_name
FROM employees INNER JOIN departments
ON employees.department_id=departments.department_id;
FIRST_NAME LAST_NAME DEPARTMENT_NAME
-------------------- ------------------------- ------------------
Steven King Executive
Neena Kochhar Executive
Lex De Haan Executive
Alexander Hunold IT
Bruce Ernst IT
David Austin IT
Valli Pataballa IT
......
Note that when multiple tables are used in a query, column names need to be prefixed with table
names in case the same column name is used in both tables.
Inner Join: Returns only rows from both tables that satisfy the join condition.
Left Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of rows
from the first (left) table.
Right Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of
rows from the second (right) table.
Full Outer Join: Returns rows from both tables that satisfy the join condition, the rest of rows from
the first (left) table, and the rest of rows from the second (right) table.
How To Write a Query with an Inner Join?
If you want to query from two tables with an inner join, you can use the INNER JOIN ... ON clause
in the FROM clause. The following query returns output with an inner join from two tables:
employees and departments. The join condition is that the department ID in the employees table
equals to the department ID in the departments table:
SQL> SELECT employees.first_name, employees.last_name,
departments.department_name
FROM employees INNER JOIN departments
ON employees.department_id=departments.department_id;
FIRST_NAME LAST_NAME DEPARTMENT_NAME
-------------------- ------------------------- ------------------
Steven King Executive
Neena Kochhar Executive
Lex De Haan Executive
Alexander Hunold IT
Bruce Ernst IT
David Austin IT
Valli Pataballa IT
......
Note that when multiple tables are used in a query, column names need to be prefixed with table
names in case the same column name is used in both tables.
Similar topics
» What Privilege Is Needed for a User to Query Tables in Another Schema?
» How To Write a Query with a Right Outer Join?
» How To Write a Query with a Left Outer Join?
» How To Write a Query with a Full Outer Join?
» What Is a SELECT Query Statement?
» How To Write a Query with a Right Outer Join?
» How To Write a Query with a Left Outer Join?
» How To Write a Query with a Full Outer Join?
» What Is a SELECT Query Statement?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Sun Jan 31, 2010 12:35 am by Micheal
» Accessing tables of other users in SQL
Sun Jan 31, 2010 12:34 am by Micheal
» Grouping commands in SQL
Sun Jan 31, 2010 12:32 am by Micheal
» Operations on Result Sets in SQL
Sun Jan 31, 2010 12:31 am by Micheal
» Subqueries in SQL
Sun Jan 31, 2010 12:30 am by Micheal
» Joining Relations in SQL
Sun Jan 31, 2010 12:28 am by Micheal
» Delete comand in SQL
Sun Jan 31, 2010 12:27 am by Micheal
» Update command in SQL
Sun Jan 31, 2010 12:26 am by Micheal
» Insert command in SQL
Sun Jan 31, 2010 12:23 am by Micheal