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

Slides:



Advertisements
Similar presentations
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
Advertisements

1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Managing Schema Objects
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Database I. Practice SQL.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
1 Copyright © 2009, Oracle. All rights reserved. B Table Descriptions.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Sections 3 – Joins & Hierarchical Queries
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
JDeveloper 10g and Oracle ADF Business Components Getting the Most Out of Your Data Avrom Roy-Faderman Senior Programmer November, 2005.
Nikolay Kostov Telerik Corporation
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
Manipulating Large Data Sets. Lesson Agenda ◦ Manipulating data using subqueries ◦ Specifying explicit default values in the INSERT and UPDATE statements.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
6 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
CSED421 Database Systems Lab Join. Human Resources (HR) schema.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
4 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Using Data Model Editor to Create Data Models Based on a SQL Query Data Set.
I Copyright © 2009, Oracle. All rights reserved. Introduction.
1 Copyright © 2006, Oracle. All rights reserved. Introduction.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Introduction.
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables Using Joins
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Oracle Join Syntax.
Introduction.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Join CSED421 Database Systems Lab

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.

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; …

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;

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;

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; …

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;

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

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

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

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.

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;

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;

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);

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, , 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 )

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

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

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

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

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