Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama



Similar documents
Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Displaying Data from Multiple Tables

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Where? Originating Table Employees Departments

CHAPTER 12. SQL Joins. Exam Objectives

Database CIS 340. lab#6. I.Arwa Najdi

GET DATA FROM MULTIPLE TABLES QUESTIONS

Unit 3. Retrieving Data from Multiple Tables

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Displaying Data from Multiple Tables. Chapter 4

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database 10g: Introduction to SQL

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

SQL Programming. Student Workbook

Performing Queries Using PROC SQL (1)

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Oracle Database 12c: Introduction to SQL Ed 1.1

SQL SELECT Query: Intermediate

Oracle SQL. Course Summary. Duration. Objectives

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Querying Microsoft SQL Server

The join operation allows you to combine related rows of data found in two tables into a single result set.

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

Querying Microsoft SQL Server 2012

A basic create statement for a simple student table would look like the following.

Course ID#: W 35 Hrs. Course Content

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Querying Microsoft SQL Server 20461C; 5 days

Programming with SQL

SECTION 3 LESSON 1. Destinations: What s in My Future?

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

1 Structured Query Language: Again. 2 Joining Tables

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

MOC QUERYING MICROSOFT SQL SERVER

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

SQL. Short introduction

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Query 1: SQL Basics

Displaying Data from Multiple Tables

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Advanced Query for Query Developers

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Database Administration with MySQL

SQL Server. 1. What is RDBMS?

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Oracle Database: SQL and PL/SQL Fundamentals

How to Create Dashboards. Published

Darshan Institute of Engineering & Technology PL_SQL

Appendix A Practices and Solutions

Oracle 10g PL/SQL Training

Information Systems SQL. Nikolaj Popov

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Querying Microsoft SQL Server 2012

Using AND in a Query: Step 1: Open Query Design

Oracle Database 10g Express

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

ICAB4136B Use structured query language to create database structures and manipulate data

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Supporting Data Set Joins in BIRT

Introduction to Querying & Reporting with SQL Server

Relational Database: Additional Operations on Relations; SQL

D B M G Data Base and Data Mining Group of Politecnico di Torino

Advanced SQL Queries for the EMF

WRITING EFFICIENT SQL. By Selene Bainum

Inquiry Formulas. student guide

Paper TU_09. Proc SQL Tips and Techniques - How to get the most out of your queries

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

CS 2316 Data Manipulation for Engineers

Talking to Databases: SQL for Designers

RECURSIVE COMMON TABLE EXPRESSIONS DATABASE IN ORACLE. Iggy Fernandez, Database Specialists INTRODUCTION

MySQL for Beginners Ed 3

Structured Query Language (SQL)

Alternatives to Merging SAS Data Sets But Be Careful

Database: SQL, MySQL

Lecture 4: SQL Joins. Morgan C. Wang Department of Statistics University of Central Florida

a presentation by Kirk Paul Lafler SAS Consultant, Author, and Trainer

Introducción a las bases de datos SQL Libro de referencia

How To Create A Table In Sql (Ahem)

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Performance Tuning for the JDBC TM API

Microsoft Access Lesson 5: Structured Query Language (SQL)

!"#"$%&'(()!!!"#$%&'())*"&+%

Calculations that Span Dimensions

50439B: Basics of Transact SQL with SQL Server 2008 R2

Transcription:

The Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4113: Database Lab Lab # 5 Retreiving Data from Multiple Tables Eng. Alaa O Shama November, 2015

Objectives: Write SELECT statements to access data from more than one table using joins. Join a table to itself by using a self-join. View data that generally does not meet a join condition by using OUTER joins. Join Types: A join is used to view information from multiple tables. Therefore, you can join tables together to view information from more than one tables, based on a common field between them. The following table lists the types the different SQL JOINs you can use: CROSS JOIN: produces the cross-product of two tables. INNER JOIN: Specifies a join between two tables with an explicit join clause.

NATURAL JOIN: creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name and data type in both tables. LEFT OUTER JOIN: Specifies a join between two tables with an explicit join clause, preserving unmatched rows from the first table. RIGHT OUTER JOIN: Specifies a join between two tables with an explicit join clause, preserving unmatched rows from the second table. FULL OUTER JOIN: Return all rows when there is a match in ONE of the tables. In general, the syntax of an SQL JOIN clause is: JOIN Syntax SELECT TABLE1.COLUMN, TABLE2.COLUMN FROM TABLE1 [NATURAL JOIN TABLE2] [JOIN TABLE2 USING (COLUMN_NAME)] [JOIN TABLE2 ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME)] [LEFT RIGHT FULL OUTER JOIN TABLE2 ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME)] [CROSS JOIN TABLE2]; In the syntax: - table1.column denotes the table and the column from which data is retrieved. - NATURAL JOIN joins two tables based on the same column name - JOIN table2 USING column_name performs a join based on the column name. - JOIN table2 ON table1.column_name = table2.column_name performs a join based on the condition in the ON clause. - LEFT/RIGHT/FULL OUTER is used to perform OUTER joins. - CROSS JOIN returns a Cartesian product from the two tables.

Qualifying Ambiguous Column Names When joining two or more tables, you need to qualify the names of the columns with the table name to avoid ambiguity. So we use table prefix to execute a query. If there are no common column names between the two tables, there is no need to qualify the columns. However, using the table prefix improves performance, because you tell the Oracle server exactly where to find the columns. Instead of full table name prefixes, use table aliases. Keeps SQL code smaller, uses less memory. In the following query, we have REGION_ID col in REGION and COUNTRY tables, so we need to use table prefix to identify to which table col belongs. 1. CROSS JOIN CROSS JOIN Syntax Table1 CROSS JOIN Table2 OR Table1, Table2 It produces the cross-product of two tables. The joined table will contain a row for every possible combination of rows from Table1 to Table2 consisting of all columns in Table1 followed by all columns intable2.

If the tables have N and M rows respectively, the joined table will have N* M rows. 2. Natural JOIN A NATURAL JOIN is a JOIN operation that creates an implicit join based on all columns in the two tables that have the same name. It selects rows from the two tables that have equal values in all matched columns. If the columns having the same names have defferent data types, an error is returned. Natural JOIN Example :

3. INNER JOIN An INNER JOIN is a JOIN operation that allows you to specify an explicit join clause. INNER JOIN Syntax TABLE1 [INNER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); INNER JOIN Syntax 1. USING ( COLUMN NAME ) Use the USING clause to match only one column when more than one column matches. Note: Don't qualify a column that is used in the USING clause. 2. ON (boolean_expression) Use the ON clause to specify a join condition. With this, you can specify join conditions separate from any search or filter conditions in the WHERE clause.

You can apply additional conditions to a join using AND clause or on WHERE clause. Ex: select employee ID, employee last name, department ID and department name of all employees who are hired before 2004. - USING

- ON or Creating Three-Way Joins with the ON Clause A three-way join is a join of three tables. Joins are performed from left to right. Ex: retrieve first name, last name, job, department id, and department name for all employees who work in Toronto City.

Ex: Retrieve country name for each department. Self-Join: Joining a Table to Itself Sometimes you need to join a table to itself. To find the name of each employee's manager, you need to join the EMPLOYEE table to itself. In this process, you look in the table twice. The first time you look in the table to find LAST_NAME column and MANAGER_ID. The second time you look in the EMPLOYEE_ID column to find value of the MANAGER_ID you found then the LAST_NAME column to find manager's name.

Note: you must alias the table in self-join. Ex: retrieve employee's last names, department numbers, and all the employees who work in the same department as a given employee. Ex: write a query to retrieve the names and hire dates of all the employees who are hired before their managers, along with their managers names and hire dates.

4. OUTER JOIN By default, joining tables with the NATURAL JOIN, USING, or ON clauses results in an inner join. Any unmatched rows are not displayed in the output. To return the unmatched rows, you can use an outer join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other table satisfy the join condition. There are three types of outer joins: LEFT OUTER RIGHT OUTER FULL OUTER OUTER JOIN Syntax TABLE1 {LEFT RIGHT FULL} [OUTER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); LEFT OUTER JOIN Example: To return the employees that do not have an assigned department.

RIGHT OUTER JOIN Example: To return the department that do not have any employees. FULL OUTER JOIN Example: This query retrieves all rows in the EMPLOYEES table, even if there is no match in the DEPARTMENT table. It also retrieves all rows in the DEPARTMENT table, even if there is no match in the EMPLOYEES table.

NOTE: for easy solutions For easy solution especially with more than two tables joins, you can use a cross join instead of other join types and put the join condition in WHERE clause and you will have the same result. Example: Write a query to retrieve first name, last name, department name and city, for all employees whose salary is greater than 9000. And Will have the same result

Exercise: ( Must be in your report ) 1. Display department name, manager name, and salary of the manager for all managers whose experience is more than 5 years. 2. Display job title and average salary of employees. 3. Display employee name if the employee joined before his manager. GOOD LUCK