Download presentation
Presentation is loading. Please wait.
Published byAmos Bailey Modified over 9 years ago
1
CSED421 Database Systems Lab Join
2
Human Resources (HR) schema
3
Cross Join (Cartesian Product) 107×27=2889 rows selected. employees departments 27 rows selected. 107 rows selected. SELECT e.last_name, d.department_name FROM employees e, departments d; …
4
Inner Join EMPLOYEES DEPARTMENTS 106 rows selected. 27 rows selected. 107 rows selected. SELECT e.last_name, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id; …
5
Use a join to query data from more than one table. The join condition in the WHERE clause. mysql> SELECT table1.column1, table2.column2 –> FROM table1, table2 –> WHERE table1.column3 = table2.column3 -> AND table1.column4 = table2.column4; JOIN ~ ON ~ mysql> SELECT table1.column1, table2.column2 –> FROM table1 JOIN table2 –> ON table1.column3 = table2.column3 -> AND table1.column4 = table2.column4; JOIN ~ USING ~ mysql> SELECT table1.column1, table2.column2 –> FROM table1 JOIN table2 –> USING (column3, column4); Inner Join
6
Equijoin 107 rows selected. EMPLOYEESDEPARTMENTS 27 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;
7
Non-Equijoin 107 rows selected. EMPLOYEESJOB_GRADES 6 rows selected. 107 rows selected. SELECT last_name, salary, grade_level FROM employees, job_grades WHERE salary BETWEEN lowest_sal AND highest_sal; (WHERE salary >= lowest_sal AND salary <= highest_sal) …
8
Joining More than Two Tables 107 rows selected. EMPLOYEES DEPARTMENTS 27 rows selected. 23 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;
9
Outer Join 107 rows selected. EMPLOYEES DEPARTMENTS 27 rows selected.
10
Left join All rows from the left table, even if there are no matches in the right table. mysql> SELECT table1.column1, table2.column2 –> FROM table1 LEFT JOIN table2 –> ON table1.column3 = table2.column3; Right join All rows from the right table, even if there are no matches in the left table. mysql> SELECT table1.column1, table2.column2 –> FROM table1 RIGHT JOIN table2 –> ON table1.column3 = table2.column3; Outer Join
11
SELECT employee_id, department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id; EMPLOYEES DEPARTMENTS 107 rows selected. EMP ⋈ DEPT 27 rows selected.
12
Outer Join SELECT employee_id, department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; EMPLOYEES DEPARTMENTS 107 rows selected.106 rows selected. EMP ⋈ DEPT 27 rows selected.
13
Self Join 8 rows selected. 107 rows selected. EMPLOYEES (WORKER) EMPLOYEES (MANAGER) SELECT e1.last_name, e2.last_name FROM employees e1, employees e2 WHERE e1.employee_id = e2.manager_id; 106 rows selected.
14
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) ]; Joining Tables Using SQL: 1999 Syntax
15
Source hr.sql http://ids.postech.ac.kr/dblab/2014/hr.sql Human Resources (HR) schema
16
lab3 DB schema
17
1. 특정 부서에 소속되어 있는 직원들의 이름 (last_name) 과 각 직원의 소속부서의 이름 (department_name) 을 출력하기. Practice 106 rows selected. …
18
2. 부서 이름 (department_name) 과 해당 부서가 위치한 국가 이름 (country_name), 지역 이름 (region_name) 을 출력하기. Practice 27 rows selected.
19
3. ‘Seattle’ 도시 (city) 에서 근무하는 직원들의 부서명 (department_name) 과 이름 (last_name) 을 출력하기. Practice
20
4. 모든 부서이름 (department_name) 과 각 부서에 소속된 직원의 수 출력하기. Practice 27 rows selected.
21
5. 자신의 상사 (manager) 보다 오래 일한 직원들의 이름 (last_name) 을 출력하기. Practice
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.