| Join type | Oracle syntax | ANSI syntax |
|---|---|---|
| Cartesian |
SELECT * SELECT * |
SELECT * SELECT * |
| Inner |
SELECT * SELECT * |
Natural join
SELECT *If the tables students and grades have common field SID, then the following natural join will join these tables based on the value of this field SELECT *Note: if there are several columns with the same names in both tables, then the natural join will join the rows with all values in these columns equal. |
|
join using
SELECT *Again, we assume that both tables departments and locations have column loc_id SELECT *Note: even if these two tables have other columns with the same name, then the join will be performed only against this particular column. |
||
|
join on
SELECT *where conditions is usual condition clause just like the one we can have in the where clause. This syntax does not require the tables have columns with the same names. SELECT * |
||
| Outer | The inner join returns only the matched rows from both tables. Sometimes, we might need to see even rows that don't have a match in the other table. For example, we would like to see the list of all courses offered the next semester and the names of the students registered for them even if there are no students registered form them yet. Thus, we need to see rows from the table courses even if there is no match for them in the table registration. | |
SELECT columns SELECT course, student |
Left outer join
The left outer join returns all (matched and unmatched) rows from the table on the left of the JOIN clause
and only the matched rows from the other table.
SELECT columns SELECT course, student SELECT columns SELECT course, student SELECT columns SELECT course, student |
|
|
Right outer join
A right outer join is a join between two tables that returns rows based on matching condition, as
well as unmatched rows from the table on right of the JOIN clause.
SELECT columns SELECT course, student SELECT columns SELECT course, student SELECT columns SELECT course, student |
||
Oracle non-ANSI syntax doesn't allow us to do that. In other words we cannot use two (+)
signs. Although, we can use the UNION clause together with both left and right joins:
SELECT course, student |
Full outer join
A full outer join returns rows based on matching condition, as well as unmatched rows from both
left and right tables.
SELECT columns SELECT course, student SELECT columns SELECT course, student SELECT columns SELECT course, student |
|
| Operator | Description | Example |
| UNION | Returns all unique rows selected by either query. |
SELECT first_name, last_name, grade |
| UNION ALL | Returns all rows including duplicates selected by either query. |
SELECT first_name, last_name, grade |
| INTERSECT | Returns rows selected by both queries. |
SELECT first_name, last_name, grade |
| MINUS | Returns unique rows selected by the first query, but not the rows selected from the second query. | SELECT first_name, last_name, grade |
SELECT * FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);
SELECT first_name, last_name, dep_id
FROM employees
WHERE dep_id IN (SELECT dep_id
FROM employees
WHERE first_name = 'John');
Other multiple row operators are
| Operator | Description | Example |
|---|---|---|
| EXISTS | The EXISTS operator is always followed by a subquery in parenthesis. This operator returns true if the subquery returns at least one row. |
The following query returns all employees currently working on at least one project
SELECT * |
| ANY | The ANY or SOME operators are used to compare a value to each value in a list or subquery. These operators always must be preceded by the comparison operators (=, !=, <, >, <=, or >=). |
The query returns all information about employees working for the departments with dep_id
less or equal to 12, 23, or 43.
SELECT * |
| ALL | The ALL operator is used to compare a value to every value in a list or subquery. The ALL operator always must be preceded by the comparison operators (=, !=, <, >, <=, or >=). |
The query returns all information about the employees working for the departments with dep_id
strictly greater than the dep_id of all departments where any John is working.
SELECT * |
| SOME | Same as ANY. | See ANY. |
SELECT first_name, last_name, salary
FROM employees e
WHERE salary = (SELECT MAX(salary)
FROM employee m
WHERE m.emp_id = e.emp_id)
ORDER BY last_name, first_name;
Examples:
SELECT city, country_id,
(CASE WHEN country_id IN (SELECT country_id FROM countries WHERE country_name='India')
THEN 'Indian'
ELSE 'Non-Indian'
END) "India?"
FROM locations
WHERE city LIKE 'B%';
SELECT dep_name, dep_id
(SELECT MAX(salary) FROM employees e WHERE e.dep_id=d.dep_id) MAXSAL
FROM departments d;
SELECT dep_name, manager_id,
(SELECT last_name FROM employees WHERE emp_id = d.manager_id) "Manager"
FROM departments d
WHERE manager_id IS NOT NULL
AND
( (SELECT country_id
FROM locations l
WHERE d.location_id = l.location_id
)
IN
(SELECT country_id
FROM countries
WHERE country_name = 'United States of America'
OR
country_name = 'Canada'
)
);
SELECT country_id, city_name, state_province FROM locations l ORDER BY (SELECT country_name FROM countries c WHERE c.country_id = l.country_id);
UPDATE employees e SET salary = ( SELECT MAX(salary) FROM employees m WHERE m.dep_id = e.dep_id );
DELETE FROM employees e WHERE salary < ( SELECT MAX(salary) FROM employees m WHERE m.dep_id = e.dep_id );
INSERT INTO employees_archive SELECT * FROM employees
INSERT INTO departments (dep_id, dep_name)
VALUES ( SELECT MAX(dep_id)+10 FROM departments, 'EDP');
We can also use a subquery in the INSERT, UPDATE, and DELETE statements in place of the table name:
DELETE FROM ( SELECT * FROM departments WHERE dep_id < 20 ) WHERE dep_id = 10;The subquery can also have an optional WITH clause: