Module 4:
Using Joins and
Subqueries
© Copyright Microsoft Corporation. All rights reserved.
Using Joins
Module
Agenda Using Subqueries
© Copyright Microsoft Corporation. All rights reserved.
Lesson 1: Using Joins
© Copyright Microsoft Corporation. All rights reserved.
JOIN with multiple table
Syntax
SELECT column1, colum2, column3
FROM table1 as t1
Left join table2 as t2 on t1.key1 = t2.key1
Left join table3 as t3 on t1.key2 = t3.key2
WHERE conditions
Exercise 1:
From dbo.DimProduct and dbo. DimProductSubcategory, dbo. DimProductCategrory
Write a query displaying the Product key, EnglishProductName, EnglishProductSubCategoryName ,
EnglishProductCategroyName
and Color columns of product which has EnglishProductCategoryName is 'Clothing’
Exercise 2:
From dbo.DimProduct, dbo.DimPromotion, dbo.FactInternetSales,
Write a query display EnglishProductName which has discount percentage >= 20%*/
Self Joins Employee
EmployeeID FirstName ManagerID
1 Dan NULL
2 Aisha 1
• Compare rows in a table to other rows in 3 Rosie 1
same table 4 Naomi 3
• Create two instances of same table in
FROM clause
• At least one alias required
Result
Employee Manager
Dan NULL
Aisha Dan
Rosie Dan
Naomi Rosie
Self Joins
Example:
SELECT child.OrganizationKey as child_key
,child.OrganizationName as child_name
,parent.OrganizationKey as parent_key
,parent.OrganizationName as parent_name
FROM dbo.DimOrganization as child
LEFT JOIN dbo.DimOrganization as parent
on parent.OrganizationKey = child.ParentOrganizationKey
Excersice:
From dbo.DimDepartmentGroup, write a query display DepartmentGroupName and their parent
DepartmentGroupName
Lesson 2: Using Subqueries
© Copyright Microsoft Corporation. All rights reserved.
Introduction to Subqueries
Subqueries are nested queries: queries within queries
Results of inner query passed to outer query
• Inner query acts like an expression from perspective of the outer query
SELECT * FROM…
SELECT * FROM…
Subqueries
In SQL a Subquery can be simply defined as a query within another query
Subqueries can be used with:
• WHERE clause, HAVING clause, FROM clause.
• SELECT, UPDATE, INSERT, DELETE statements,...
Example:
SELECT calendar_year
, calendar_month
FROM
(
SELECT DISTINCT
YEAR(FullDateAlternateKey) as calendar_year
, MONTH(FullDateAlternateKey) as calendar_month
FROM DimDate
) as calendar
Subqueries
Example:
Write a query displaying the Product key, EnglishProductNameName, and Color columns from rows in the
dbo.DimProduct table. Display only those rows in which the SalesAmount exceeded $1,000 and orderdate
during 2010
SELECT Productkey
, EnglishProductNameName
, Color
FROM dbo.DimProduct
where Productkey in
( select Productkey
From FactInternetSales
Where SalesAmount > 1000 )
CTEs - common_table_expression
A common table expression, or CTE, is a temporary named result set created from a simple
SELECT statement that can be used in a subsequent SELECT statement.
Example:
WITH calendar as
(SELECT DISTINCT
YEAR(FullDateAlternateKey) as calendar_year
, MONTH(FullDateAlternateKey) as calendar_month
FROM DimDate)
SELECT calendar_year
, calendar_month
FROM calendar
© Copyright Microsoft Corporation. All rights reserved.