SQL Retrieving Data from Multiple Tables

Similar documents
Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Lecture 6 - More SQL

SQL Functions (Single-Row, Aggregate)

Procedure & Function

Chapter 3: Introduction to SQL

Triggers- View-Sequence

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

QQ Group

Database Lab Lab 6 DML part 3

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Data Manipulation Language (DML)

Retrieving Data from Multiple Tables

CS 582 Database Management Systems II

Procedural Language Structured Query Language (PL/SQL)

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

Querying Data with Transact-SQL

Explain in words what this relational algebra expression returns:

Lab # 6. Data Manipulation Language (DML)

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CS3DB3/SE4DB3/SE6M03 TUTORIAL

CSC Web Programming. Introduction to SQL

Querying Data with Transact SQL

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Querying Data with Transact-SQL

Unit 2: SQL AND PL/SQL

Using Stored Queries Instead of Nested Queries in the FROM Clause

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Relational model and basic SQL

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Relational Algebra. Procedural language Six basic operators

CS127 Homework #3. Due: October 11th, :59 P.M. Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E)

Chapter 4: Intermediate SQL

Chapter 7 Advanced SQL. Objectives

Intermediate SQL ( )

T-SQL Training: T-SQL for SQL Server for Developers

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Writing Analytical Queries for Business Intelligence

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 7 Equijoins. Review. Why do we store data for articles and writers in two separate tables? How can we link an article to its writer?

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Querying Data with Transact-SQL

Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL (20761)

Querying Data with Transact-SQL

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 6: Formal Relational Query Languages

Querying Data with Transact-SQL

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

Unit Assessment Guide

CS 327E Class 3. February 11, 2019

Chapter 4: Intermediate SQL

Chapter 6 - Part II The Relational Algebra and Calculus

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune

Principles of Data Management

Duration Level Technology Delivery Method Training Credits. Classroom ILT 5 Days Intermediate SQL Server

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Relational Algebra and SQL

Microsoft Querying Microsoft SQL Server 2014

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Chapter 4: Intermediate SQL

20761B: QUERYING DATA WITH TRANSACT-SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

CPS221 Lecture: Relational Database Querying and Updating

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Computer Programming: C++

CPS221 Lecture: Relational Database Querying and Updating

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Querying Microsoft SQL Server

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Querying Data with Transact-SQL

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Arrays. Eng. Mohammed Abdualal

Querying Microsoft SQL Server 2012/2014

20761 Querying Data with Transact SQL

Score. 1 (10) 2 (10) 3 (8) 4 (13) 5 (9) Total (50)

Structure Query Language (SQL)

Querying Data with Transact-SQL

20461: Querying Microsoft SQL Server 2014 Databases

20461: Querying Microsoft SQL Server

Relational Database Management Systems for Epidemiologists: SQL Part II

Querying Data with Transact-SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Querying Microsoft SQL Server (MOC 20461C)

Oracle Database 10g: Introduction to SQL

Database Programming - Section 18. Instructor Guide

Chapter 6 Formal Relational Query Languages

Lesson 2. Data Manipulation Language

Selection Statement ( if )

Querying Microsoft SQL Server 2008/2012

Querying Microsoft SQL Server

COURSE OUTLINE: Querying Microsoft SQL Server

MIS NETWORK ADMINISTRATOR PROGRAM

Querying Microsoft SQL Server

Transcription:

The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Database Lab (ECOM 4113) Lab 5 SQL Retrieving Data from Multiple Tables Eng. Ibraheem Lubbad

An SQL JOIN clause is used to combine rows from two or more tables, views, or materialized views, based on a common field between them. The following list type of the SQL JOINs you can use: 1) Inner join Return all rows that match the condition in both the tables 2) Outer join LEFT (OUTER) JOIN: Return all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values. RIGHT (OUTER) JOIN: Return all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values. FULL (OUTER) JOIN: Combines LEFT OUTER JOIN and RIGHT OUTER JOIN. It returns row from either table when the conditions are met and returns NULL value when there is no match. 3) Natural join It is based on the two conditions: The JOIN is made on all the columns with the same name for quality Removes duplicate columns from the result. 4) Cross join It is the Cartesian product of the two tables involved. 5) Self It is a JOIN (INNER, OUTER, etc) of a table to itself.

Join Conditions: Natural: Creates an implicit join base on columns that have the same name in both tables, also common column must have the same datatype, otherwise return error. Using ( column_name) : Creates an explicit join base on columns that have the same name in both tables, usually use key attribute to join two table. On: creates an explicit join using join condition to bind to two table, use it when key attribute in each column has different name, also we can add conditional expression as where. Note: All INNER and OUTER keywords are optional Join Conditions (natural, using, on) can be combined with any join type In general, the syntax of an SQL JOIN clause is: Syntax of SQL Select Statement 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];

INNER JOIN (INNER) JOIN: Return records that have matching values in both tables. Syntax of SQL Select Statement TABLE1 [INNER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1. COLUMN_NAME = TABLE2. COLUMN_NAME) ; Example: Find a list of all students, displaying their ID, and name, dept_name, and tot_cred, along with the courses that they have taken Use NATURAL clause SELECT ID,NAME,DEPT_NAME,TOT_CRED,COURSE_ID FROM STUDENT NATURAL JOIN TAKES ; Use USING clause SELECT ID, NAME,DEPT_NAME,TOT_CRED,COURSE_ID FROM STUDENT JOIN TAKES USING (ID) Use on clause SELECT STUDENT.ID,NAME,DEPT_NAME,TOT_CRED,COURSE_ID FROM STUDENT JOIN TAKES ON STUDENT.ID=TAKES.ID ;

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. If the SELECT statement in which the JOIN operations appears has an asterisk (*) in the select list, the asterisk will be expanded to the following list of columns (in this order): All the common columns. Every column in the first (left) table that is not a common column. Every column in the second (right) table that is not a common column There are three types of outer joins: 1. LEFT OUTER 2. RIGHT OUTER 3. FULL OUTER OUTER JOIN Syntax TABLE1 { LEFT RIGHT FULL} [OUTER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1. COLUMN_NAME = TABLE2. COLUMN_NAME) ;

1) Left OUTER JOIN: Example: Find INSTRUCTORs that do not work in any department SELECT * FROM INSTRUCTOR LEFT JOIN DEPARTMENT USING(DEPT_NAME); Note first column condition attribute Instructor table columns Department table columns Instructor ibraheem do not work in any department so all department attribute returned as null value. Also we can use natural condition to join between two table base common column tables. SELECT * FROM INSTRUCTOR NATURAL LEFT JOIN DEPARTMENT

2) Right OUTER JOIN: SELECT * FROM INSTRUCTOR RIGHT JOIN DEPARTMENT USING(DEPT_NAME); There isn t any instructor in the Department of Mathematics. 3) Full OUTER JOIN: Combines LEFT OUTER JOIN and RIGHT OUTER JOIN SELECT * FROM INSTRUCTOR FULL JOIN DEPARTMENT USING(DEPT_NAME); Left table Right table

Self-join: Joining a Table to Itself Example: Find the names of all instructors whose salary is greater than at least one instructor in the Biology department. SELECT DISTINCT T.NAME,T.SALARY,S.NAME, S.SALARY FROM INSTRUCTOR T JOIN INSTRUCTOR S ON T.ID <> S.ID WHERE T.SALARY > S.SALARY AND S.DEPT_NAME = 'Biology'; Join multiple tables: To execute a join of three or more tables, first joins two of the tables based on the join conditions comparing their columns and then joins the result to another table based on join conditions containing columns of the joined tables and the new table. Example: Find all instructors names and all courses name they taught SELECT NAME AS INSTRUCTOR_NAME, C.TITLE AS COURSE_NAME FROM INSTRUCTOR I JOIN TEACHES T ON I.ID=T.ID JOIN COURSE C ON T.COURSE_ID=C.COURSE_ID;

Join with Grouping: Apply aggregation function on table that containing columns of the joined tables Example: For each course section offered in 2009, find the average total credits ( tot cred) of all students enrolled in the section, which had at least 2 students SELECT COURSE_ID,SEC_ID,SEMESTER,YEAR,AVG(TOT_CRED) FROM TAKES NATURAL JOIN STUDENT WHERE YEAR=2009 GROUP BY COURSE_ID,SEC_ID,SEMESTER,YEAR HAVING COUNT (ID) >= 2; Example: Find the number of instructors in each department who teach a course in the Spring 2010 semester SELECT DEPT_NAME, COUNT (DISTINCT ID) AS INSTR_COUNT FROM INSTRUCTOR NATURAL JOIN TEACHES WHERE SEMESTER = 'Spring' AND YEAR = 2010 GROUP BY DEPT_NAME; END