Queries can access multiple tables at once or access the same table in such a way that multiple rows of the table are processed at the same time. A query that accesses multiple rows of the same or different tables at one time is called a join query.
Executing a join query
For example, suppose you want to list all the employee records together with the name and location of the associated department. To do that, you need to compare the deptno column of each row of the emp table with the deptno column of all rows in the dept table. Then select the pairs of rows where these values match. You can accomplish this using the following query:
Observe two things about the result set:
There's no result row for department 40. That's because there's no matching entry in the emp table for department 40, so the join ignores the unmatched rows in the dept table. The code that follows shows how to fix this.
It's more desirable to list the output columns qualified by table name rather than using * or leaving out the qualification as follows:
Since all the columns had different names (except for deptno, which therefore must be qualified), the parser found the table they belong to. However, it's best practice to fully qualify column names in join queries.
You can also write this kind of join queries in this alternative form:
This syntax is not as commonly used, but we show it here to help you understand the following topics.
Executing an outer join
In all the above results for joins, no employees were returned that belonged to department 40. As a consequence, the record for department 40 never appears. Next, resolve how to get the department 40 record in the results despite the fact that there are no matching employees. The query must scan the dept table and, for each row, find the matching emp row. If no matching row is found, we want to substitute some “empty” values for the emp table’s columns. This kind of query is called an outer join. (The joins you have seen so far are inner joins.) The command looks like this:
This query is called a left outer join. The table mentioned on the left of the join operator has each of its rows in the output at least once. The table on the right has only those rows output that match some row of the left table. When a left-table row is selected for which there is no right-table match, empty (NULL) values are substituted for the right-table columns.
An alternative syntax for an outer join is to use the outer join operator, “(+)”, in the join condition in the WHERE clause. The outer join operator is placed after the column name of the table for which you substitute null values for unmatched rows. So for all the rows in the dept table that have no matching rows in the emp table, EDB Postgres Advanced Server returns null for any select list expressions containing columns of emp. Hence you can rewrite the earlier example as:
Executing a self join
You can also join a table against itself, which is called a self join. As an example, suppose you want to find the name of each employee and the name of that employee’s manager. You need to compare the mgr column of each emp row to the empno column of all other emp rows.
Here, the emp table was relabeled as e1 to represent the employee row in the select list and in the join condition. It was also relabeled as e2 to represent the matching employee row acting as manager in the select list and in the join condition. You can use these kinds of aliases in other queries to save some typing. For example: