笛卡爾集
笛卡爾集會在下面條件下產生:spa
爲了不笛卡爾集, 能夠在 WHERE 加入有效的鏈接條件。3d
Oracle 鏈接
使用鏈接在多個表中查詢數據。code
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2;
等值鏈接
示例blog
SELECT employees.employee_id, employees.last_name, employees.department_id, departments.department_id, departments.location_id FROM employees, departments WHERE employees.department_id = departments.department_id;
區分重複的列名
ci
表的別名it
SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e, departments d WHERE e.department_id = d.department_id;
鏈接多個表
io
非等值鏈接
EMPLOYEES表中的列工資應在JOB_GRADES表中的最高工資與最低工資之間
示例table
SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;
內鏈接和外鏈接
ast
外鏈接語法class
右外鏈接 SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column(+) = table2.column; 左外鏈接 SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column = table2.column(+);
示例
SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id (+) = d.department_id;
自鏈接
示例
SELECT worker.last_name || ' works for ' || manager.last_name FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id;
1999 語法鏈接
SELECT table1.column, table2.column FROM table1 [CROSS JOIN table2] | [NATURAL JOIN table2] | [JOIN table2 USING (column_name)] | [JOIN table2 ON(table1.column_name = table2.column_name)] | [LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)];
使用ON 子句建立鏈接
示例
SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON ( e.department_id = d.department_id );
使用 ON 子句建立多表鏈接
示例
SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d.department_id = e.department_id JOIN locations l ON d.location_id = l.location_id;
左外鏈接
示例
SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON ( e.department_id = d.department_id );
右外鏈接
示例
SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON ( e.department_id = d.department_id );
滿外鏈接
SELECT e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON ( e.department_id = d.department_id );