SQL tutorial | Interview questions | Oracle
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How To Join Two Tables in a Single Query?

Go down

How To Join Two Tables in a Single Query? Empty How To Join Two Tables in a Single Query?

Post by Micheal Sun Jan 17, 2010 3:09 am

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.

Micheal
Admin

Posts : 243
Join date : 2010-01-10

http://sql-tutorial.co.cc

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum