|
Home >> FAQs/Tutorials >> Oracle DBA FAQ >> Index
Oracle DBA FAQ - Understanding SQL SELECT Query Statements
By: FYIcenter.com
Part:
1
2
3
4
5
6
7
8
(Continued from previous part...)
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,
2 departments.department_name
3 FROM employees INNER JOIN departments
4 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.
How To Define and Use Table Alias Names?
When column names need to be prefixed with table names, you can define
table alias name and use them to prefix column names as shown in the
following select statement:
SQL> SELECT e.first_name, e.last_name, d.department_name
FROM employees e INNER JOIN departments d
ON e.department_id=d.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
......
How To Write a Query with a Left Outer Join?
If you want to query from two tables with a left outer join, you can use the
LEFT OUTER JOIN ... ON clause in the FROM clause. The following query returns output
with a left outer join from two tables: departments and employees. The join condition
is that the manager ID in the departments table equals to the employee ID in the employees table:
SQL> set NULL 'NULL'
SQL> SELECT d.department_name, e.first_name, e.last_name
2 FROM departments d LEFT OUTER JOIN employees e
3 ON d.manager_id = e.employee_id;
DEPARTMENT_NAME FIRST_NAME LAST_NAME
-------------------- -------------------- --------------
Administration Jennifer Whalen
Marketing Michael Hartstein
Purchasing Den Raphaely
Human Resources Susan Mavris
Shipping Adam Fripp
IT Alexander Hunold
......
Treasury NULL NULL
Corporate Tax NULL NULL
Control And Credit NULL NULL
Shareholder Services NULL NULL
Benefits NULL NULL
Manufacturing NULL NULL
Construction NULL NULL
......
Note that a left outer join may return extra rows from the first (left) table
that do not satisfy the join condition. In those extra rows, columns from the
second (right) table will be given null values.
The extra rows returned from the left outer join in this example represents departments
that have no manager IDs.
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
|