CSED421 Database Systems Lab Join. Human Resources (HR) schema.

Slides:



Advertisements
Similar presentations
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advertisements

Chapter 4 Joining Multiple Tables
Displaying Data from Multiple Tables
Notice that No where clause In the syntax.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
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.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Sections 3 – Joins & Hierarchical Queries
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested 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.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
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.
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
SQL Intro (MS SQL Server and MySQL) Telerik Software Academy Databases.
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.
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
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.
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.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
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.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2.
 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.
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.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Copyright  Oracle Corporation, All rights reserved. 4 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 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL Introduction. Table of Contents 1. SQL and T-SQL Languages 2. The “SoftUni” Database Schema 3. Introducing the SELECT SQL Statement  SQL Operators.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Joins, Subqueries, CTE and Indices
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
多表查询 Schedule: Timing Topic 55 minutes Lecture 55 minutes Practice
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Oracle Join Syntax.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

CSED421 Database Systems Lab Join

Human Resources (HR) schema

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

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

 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. 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;

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

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;

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

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.

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.

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.

 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

 Source 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