csed421 database systems lab join. human resources (hr) schema
TRANSCRIPT
CSED421Database Systems Lab
Join
Human Resources (HR) schema
Cross Join (Cartesian Prod-uct)
107×27=2889 rows selected.
employees departments
27 rows selected.107 rows selected.
SELECT e.last_name, d.department_nameFROM employees e, departments d;
…
Inner JoinEMPLOYEES DEPARTMENTS
106 rows selected.
27 rows selected.107 rows selected.
SELECT e.last_name, d.department_nameFROM employees e, departments dWHERE e.department_id = d.department_id;
…
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
Equijoin
107 rows selected.
EMPLOYEES DEPARTMENTS
27 rows selected.
(Foreign key)
(Primary key)
SELECT e.employee_id, d.department_id, d.department_nameFROM employees e, departments dWHERE e.department_id = d.department_id;
Non-Equijoin
107 rows selected.
EMPLOYEES JOB_GRADES
6 rows selected.
107 rows selected.
SELECT last_name, salary, grade_levelFROM employees, job_gradesWHERE salary BETWEEN lowest_sal AND highest_sal;
(WHERE salary >= lowest_sal AND salary <= highest_sal)
…
Joining More than Two Ta-bles
107 rows selected.
EMPLOYEES DEPARTMENTS
27 rows selected.
23 rows selected.
LOCATIONS
SELECT e.employee_id, d.department_id, l.cityFROM employees e, departments d, locations lWHERE e.department_id = d.department_id AND d.location_id = l.location_id;
Outer Join
107 rows selected.
EMPLOYEES DEPARTMENTS
27 rows selected.
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
Outer Join
SELECT employee_id, department_nameFROM employees e LEFT JOIN departments dON e.department_id = d.department_id;
EMPLOYEESDEPARTMENTS
107 rows selected. 107 rows selected.
EMP ⋈ DEPT
27 rows selected.
Outer Join
SELECT employee_id, department_nameFROM employees e JOIN departments dON e.department_id = d.department_id;
EMPLOYEESDEPARTMENTS
107 rows selected. 106 rows selected.
EMP ⋈ DEPT
27 rows selected.
Self Join
8 rows selected.
107 rows selected.
EMPLOYEES (WORKER) EMPLOYEES (MANAGER)
SELECT e1.last_name, e2.last_nameFROM employees e1, employees e2WHERE e1.employee_id = e2.manager_id;
106 rows selected.
SELECT table1.column, table2.columnFROMtable1[ 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
Source hr.sql
http://ids.postech.ac.kr/dblab/2014/hr.sql
Human Resources (HR) schema
lab3 DB schema
1. 특정 부서에 소속되어 있는 직원들의 이름 (last_name) 과 각 직원의 소속부서의 이름 (department_name) 을 출력하기 .
Practice
106 rows selected.
…
2. 부서 이름 (department_name) 과 해당 부서가 위치한 국가 이름(country_name), 지역 이름 (region_name) 을 출력하기 .
Practice
27 rows selected.
3. ‘Seattle’ 도시 (city) 에서 근무하는 직원들의 부서명(department_name) 과 이름 (last_name) 을 출력하기 .
Practice
4. 모든 부서이름 (department_name) 과 각 부서에 소속된 직원의 수 출력하기 .
Practice
27 rows selected.
5. 자신의 상사 (manager) 보다 오래 일한 직원들의 이름(last_name) 을 출력하기 .
Practice