0% found this document useful (0 votes)
69 views12 pages

DP080 Lecture 4

This document provides an overview of using joins and subqueries in SQL. It includes examples of left joins on multiple tables, self joins to compare rows within a single table, and subqueries used within the WHERE, HAVING, and FROM clauses. Common table expressions (CTEs) are also introduced as a way to name and reuse the results of a simple SELECT statement. Exercises are provided to join product and category tables, find products with discounts over 20%, and use subqueries to filter on sales amount.

Uploaded by

Ngân Hồ Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views12 pages

DP080 Lecture 4

This document provides an overview of using joins and subqueries in SQL. It includes examples of left joins on multiple tables, self joins to compare rows within a single table, and subqueries used within the WHERE, HAVING, and FROM clauses. Common table expressions (CTEs) are also introduced as a way to name and reuse the results of a simple SELECT statement. Exercises are provided to join product and category tables, find products with discounts over 20%, and use subqueries to filter on sales amount.

Uploaded by

Ngân Hồ Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

You might also like