0% found this document useful (0 votes)
30 views

SQL-3-Querying Multiple Tables

This document discusses querying multiple tables in SQL, including understanding different types of joins such as inner joins, outer joins, cross joins, and self-joins. It covers join terminology like Cartesian products, provides an overview of different join types including inner, left/right/full outer joins, and discusses SQL syntax options for performing joins in T-SQL. Examples are provided for querying with different join types and self-joins.

Uploaded by

柴柴
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

SQL-3-Querying Multiple Tables

This document discusses querying multiple tables in SQL, including understanding different types of joins such as inner joins, outer joins, cross joins, and self-joins. It covers join terminology like Cartesian products, provides an overview of different join types including inner, left/right/full outer joins, and discusses SQL syntax options for performing joins in T-SQL. Examples are provided for querying with different join types and self-joins.

Uploaded by

柴柴
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Querying Multiple Tables

 Understanding Joins 合併查詢

 Querying with Inner Joins

 Querying with Outer Joins

 Querying with Cross Joins

 Self-Joins 自我關聯 ( 主管 - 員工 )

1
Understanding Joins

 Join Terminology: Cartesian Product( 笛卡爾乘積 )

 Overview of Join Types

 T-SQL Syntax Choices

2
Join Terminology:
Cartesian Product( 笛卡爾乘積 )
Name Product
Davis Alice Mutton
Davis Crab Meat
Name Product
Davis Ipoh Coffee
Davis Alice Mutton Funk Alice Mutton
Funk Crab Meat Funk Crab Meat
King Ipoh Coffee Funk Ipoh Coffee
King Alice Mutton
King Crab Meat
King Ipoh Coffee

3
Overview of Join Types

Join Type Description

Cross 笛卡爾乘積
不需要給 ON

Inner Starts with Cartesian product;


applies filter to match rows between tables based on predicate.

LEFT|RIGHT|FULL Starts with Cartesian product;


Outer all rows from designated table preserved, matching rows from other
table retrieved.
Additional NULLs inserted as placeholders.

4
5
T-SQL Syntax Choices

 ANSI SQL-1992
 Tables joined by JOIN operator in FROM Clause
 Preferred syntax
SELECT ...
FROM Table1 INNER JOIN Table2
ON <on_predicate>

 ANSI SQL-1989
 Tables joined by commas in FROM Clause
 Not recommended: accidental Cartesian products!
SELECT ...
FROM Table1, Table2
WHERE <where_predicate>
6
Querying with Inner Joins

 Join based on single matching attribute


SELECT ...
FROM Production.Categories AS C
JOIN Production.Products AS P
ON C.categoryid = P.categoryid;

 Join based on multiple matching attributes


(composite join)
-- List cities and countries where both
-- customers and employees live
SELECT DISTINCT e.city, e.country
FROM Sales.Customers AS c
JOIN HR.Employees AS e
ON c.city = e.city AND
7
c.country = e.country;
Querying with Outer Join

 All customers with order details ( 不管有沒有訂單 ):


SELECT c.custid, c.contactname,
o.orderid, o.orderdate
FROM Sales.Customers AS C
LEFT OUTER JOIN Sales.Orders AS O
ON c.custid = o.custid;

 找出那些客戶沒有訂單 (outer 可省略 ):


SELECT c.custid, c.contactname,
o.orderid, o.orderdate
FROM Sales.Customers AS C LEFT OUTER
JOIN Sales.Orders AS O
ON c.custid = o.custid
WHERE o.orderid IS NULL;
8
使用 Join 要注意的事

 把 Table 間要 matching 的欄位寫在 ON

 要篩選資料的條件寫在 Where

 Inner Join 的話,篩選條件在 ON 跟在 Where 是一樣的

 Outer Join 的話,就有差別了哦 !


-- 因為 Orders 沒資料,所以 Orders 的值是 -- 找出沒有下訂單的客戶
NULL SELECT c.custid, c.contactname, o.orderid,
SELECT c.custid, c.contactname, o.orderid, o.orderdate
o.orderdate FROM Sales.Customers AS c LEFT JOIN
FROM Sales.Customers AS c LEFT JOIN Sales.Orders AS o
Sales.Orders AS o ON c.custid = o.custid
ON c.custid = o.custid AND o.orderid IS NULL; WHERE o.orderid IS NULL;
9
Querying with Self-Joins

 Self-Join Syntax

 Self-Join Examples

10
Self-Join Syntax

 Can use same basic structure as inner join, outer join, and cross join

SELECT ...
FROM T1 AS t1 JOIN T1 AS t2
ON t1.Col1 = t2.Col2;

 Use inequality in ON clause to return unique combinations


 Self pairs (1 with 1), mirror pairs (1 with 2, 2 with 1) omitted
SELECT ...
FROM T1 AS t1 JOIN T1 AS t2
ON t1.Col1 < t2.Col1;

11
Self-Join Examples

 列出員工及它的主管編號

SELECT e. empid, e.lastname,


e.title, m.mgrid
FROM HR.Employees AS e
LEFT OUTER JOIN HR.Employees AS m
ON e.mgrid=m.empid;

12
範例  4.1. - Demonstration A.sql

13

You might also like