Presentation is loading. Please wait.

Presentation is loading. Please wait.

Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2.

Similar presentations


Presentation on theme: "Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2."— Presentation transcript:

1 Join CSED421 Database Systems Lab

2 Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1. column1 = table2.column2; The join condition in the WHERE clause.

3 Cartesian Product 20×8=160 rows selected. EMPLOYEES DEPARTMENTS 8 rows selected. 20 rows selected. SELECT e.last_name, d.department_name FROM employees e, departments d; …

4 Equijoin 20 rows selected. EMPLOYEESDEPARTMENTS 8 rows selected. Foreign key Primary key SELECT e.employee_id, d.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id;

5 Joining More than Two Tables 20 rows selected. EMPLOYEES DEPARTMENTS 8 rows selected. 5 rows selected. LOCATIONS SELECT e.employee_id, d.department_id, l.city FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id;

6 Non-Equijoin 20 rows selected. EMPLOYEESJOB_GRADES 6 rows selected. 20 rows selected. SELECT last_name, salary, grade_level FROM employees, job_grades WHERE salary BETWEEN lowest_sal AND highest_sal; …

7 Outer Join You use an outer join to also see rows that do not meet the join condition. Left outer join SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column (+); Right outer join SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column (+)= table2.column;

8 Outer Join (left outer join) EMPLOYEESDEPARTMENTS employee_iddepartment_id E1D1 E2Null department_iddepartment_name D1Marketing D2IT Left outer join SELECT e.employee_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id (+) employee_iddepartment_name E1Marketing E2Null

9 Outer Join (right outer join) EMPLOYEESDEPARTMENTS employee_iddepartment_id E1D1 E2Null department_iddepartment_name D1Marketing D2IT Right outer join SELECT e.employee_id, d.department_name FROM employees e, departments d WHERE e.department_id (+) = d.department_id employee_iddepartment_name E1Marketing NullIT

10 Self Join 8 rows selected. 20 rows selected. EMPLOYEES (WORKER) EMPLOYEES (MANAGER) …

11 Joining Tables Using SQL: 1999 Syntax SELECTtable1.column, table2.column FROMtable1 [ CROSS JOIN table2 ] | [ NATURAL JOIN table2 ] | [ JOIN table2 USING (column_name)] | [ JOIN table2 ON (table1.column1 = table2.column2) ] | [ LEFT | RIGHT | FULL OUTER JOIN table2 ON (table1.column1 = table2.column2) ]; You can obtain the same result as were shown in the prior pages.

12 SELECTlast_name, department_name FROMemployees CROSS JOIN departments; SELECTlast_name, department_name FROMemployees, departments; SELECTdepartment_name, location_id, city FROMdepartments NATURAL JOIN locations; SELECTd.department_name, l.location_id, l.city FROMdepartments d, locations l WHEREd.location_id = l.location_id;

13 SELECTlast_name, department_name FROMemployees JOIN departments USING(department_id); SELECTlast_name, department_name FROMemployees JOIN departments ON(e.department_id = d.department_id); SELECTlast_name, department_name FROMemployees e, departments d WHEREe.department_id = d.department_id;

14 SELECTlast_name, department_name FROMemployees e LEFT OUTER JOIN departments d ON(e.department_id = d.department_id); SELECTlast_name, department_name FROMemployees e, departments d WHEREe.department_id = d.department_id (+); SELECTlast_name, department_name FROMemployees e FULL OUTER JOIN departments d ON(e.department_id = d.department_id);

15 Practice Sample schema (lab4_create.sql) –Departments (department_id, department_name, manager_id, location_id ) –Locations (location_id, street_address, postal_code, city, state_province, country_id ) –Countries (country_id, country_name, region_id ) –Regions (region_id, region_name ) –Employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id ) –Job_grades (grade_level, lowest_sal, highest_sal ) –Jobs (job_id, job_title, min_salary, max_salary ) –Job_history (employee_id, start_date, end_date, job_id, department_id )

16 Practice 1. 모든 직원들의 이름 (last_name) 과 각 직원의 소속부서의 이름 (department_name) 을 출력하기. 19 rows selected. …

17 Practice 2. 모든 부서들의 이름 (department_name) 과 해당 부서가 위치한 국가 이름 (country_name), 지역 이름 (region_name) 을 출력하기. 8 rows selected.

18 Practice 3.‘Seattle’ 도시 (city) 에서 근무하는 모든 직원의 부서명 (department_name) 과 이름 (last_name) 을 출력하기. 4. 모든 부서이름 (department_name) 과 각 부서에 소속된 직원의 수 출 력하기. 6 rows selected. 8 rows selected. 7 rows selected.

19 Practice 5. 자신의 상사보다 오래 일한 직원들의 이름 (last_name) 을 출력하기. 9 rows selected.

20 Practice 6. 월급 (salary) 이 가장 많은 직원의 이름 (last_name) 과 월급 (salary) 을 출력하기. 단, max 와 같은 aggregate function 을 사용하지 말것. ( 힌트 ) self outer join 을 이용


Download ppt "Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2."

Similar presentations


Ads by Google