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

Lesson02 Advanced SQL - Join

The document provides an overview of advanced SQL topics including using aliases for table names, different types of joins to combine data from multiple tables, and combining multiple result sets. It defines SQL aliases as a way to temporarily rename tables or columns to make them more readable. It describes inner, left, right, full outer, and cross joins as well as examples of joining more than two tables and joining a table to itself. It also summarizes that the SQL UNION operator is used to combine the results of two or more SELECT statements and lists requirements for column names and data types.

Uploaded by

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

Lesson02 Advanced SQL - Join

The document provides an overview of advanced SQL topics including using aliases for table names, different types of joins to combine data from multiple tables, and combining multiple result sets. It defines SQL aliases as a way to temporarily rename tables or columns to make them more readable. It describes inner, left, right, full outer, and cross joins as well as examples of joining more than two tables and joining a table to itself. It also summarizes that the SQL UNION operator is used to combine the results of two or more SELECT statements and lists requirements for column names and data types.

Uploaded by

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

Advanced SQL

Advanced Database
Topic & Structure of Lesson

 Using Aliases for Table Names


 Using Joins to Combining Data from Multiple Tables
 Combining Multiple Result Sets

Slide 2 (of 30)


SQL Aliases
 SQL aliases are used to temporarily rename a table or
a column heading.
 Basically aliases are created to make column names
more readable.
 SQL Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name
 SQL Alias Syntax for Tables
SELECT column_name(s)
FROM table_name AS alias_name
Using Aliases for Table Names
 Example 1 (without an alias name)

USE joindb
SELECT buyer_name, sales.buyer_id, qty
FROM buyers INNER JOIN sales
 Example 2 (with an
ON buyers.buyer_id alias name)
= sales.buyer_id
GO

USE joindb
SELECT buyer_name, s.buyer_id, qty
FROM buyers AS b INNER JOIN sales AS s
ON b.buyer_id = s.buyer_id
GO
SQL JOINs
 An SQL JOIN clause is used to combine rows from two or
more tables, based on a common field between them.
 Types of SQL JOINs:
 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL JOIN
 CROSS JOIN
Introduction to Joins

 Selects Specific Columns from Multiple Tables


 JOIN keyword specifies that tables are joined and how
to join them
 ON keyword specifies join condition
 Queries Two or More Tables to Produce a Result Set
 Use primary and foreign keys as join conditions
 Use columns common to specified tables to join tables
SQL INNER JOIN Keyword
 The INNER JOIN keyword selects all rows from both
tables as long as there is a match between the columns in
both tables.

 SQL INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name
Using Inner Joins
USE joindb
SELECT buyer_name, sales.buyer_id, qty
FROM buyers INNER JOIN sales Example 1
ON buyers.buyer_id = sales.buyer_id
GO
buyers sales
buyer_name buyer_id buyer_id prod_id qty
Adam Barr 1 1 2 15
Sean Chai 2 1 3 5
Eva Corets 3 4 1 37
Erin O’Melia 4 3 5 11
4 2 1003
Result
buyer_name buyer_id qty
Adam Barr 1 15
Adam Barr 1 5
Erin O’Melia 4 37
Eva Corets 3 11
Erin O’Melia 4 1003
SQL LEFT JOIN Keyword
 The LEFT JOIN keyword returns all rows from the left
table (table1), with the matching rows in the right table
(table2). The result is NULL in the right side when there is
no match.

 SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name
Using Left Outer Joins
USE joindb
SELECT buyer_name, sales.buyer_id, qty
FROM buyers LEFT OUTER JOIN sales
ON buyers.buyer_id = sales.buyer_id Example 1
GO
buyers sales
buyer_name buyer_id buyer_id prod_id qty
Adam Barr 1 1 2 15
Sean Chai 2 1 3 5
Eva Corets 3 4 1 37
Erin O’Melia 4 3 5 11
Result 4 2 1003
buyer_name buyer_id qty
Adam Barr 1 15
Adam Barr 1 5
Erin O’Melia 4 37
Eva Corets 3 11
Erin O’Melia 4 1003
Sean Chai NULL NULL
SQL RIGHT JOIN Keyword
 The RIGHT JOIN keyword returns all rows from the right
table (table2), with the matching rows in the left table
(table1). The result is NULL in the left side when there is
no match.

 SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name
SQL FULL OUTER JOIN Keyword
 The FULL OUTER JOIN keyword returns all rows from
the left table (table1) and from the right table (table2).
 The FULL OUTER JOIN keyword combines the result of
both LEFT and RIGHT joins.

 SQL FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name
SQL CROSS JOIN Keyword
 The SQL CROSS JOIN produces a result set which is the
number of rows in the first table multiplied by the number
of rows in the second table,
 If no WHERE clause is used along with CROSS JOIN.
This kind of result is called as Cartesian Product.

 SQL FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
CROSS JOIN table2
Using Cross Joins
USE joindb
SELECT buyer_name, qty
FROM buyers Example 1
CROSS JOIN sales
GO
buyers sales Result
buyer_id buyer_name buyer_id prod_id qty buyer_name qty
1 Adam Barr 1 2 15 Adam Barr 15
2 Sean Chai 1 3 5 Adam Barr 5
3 Eva Corets 4 1 37 Adam Barr 37
4 Erin O’Melia 3 5 11 Adam Barr 11
4 2 1003 Adam Barr 1003
Sean Chai 15
Sean Chai 5
Sean Chai 37
Sean Chai 11
Sean Chai 1003
Eva Corets 15
... ...
Joining More Than Two Tables
SELECT buyer_name, prod_name, qty
FROM buyers
INNER JOIN sales
ON buyers.buyer_id = sales.buyer_id Example 1
INNER JOIN produce
ON sales.prod_id = produce.prod_id
GO
buyers sales produce
buyer_id buyer_name buyer_id prod_id qty prod_id prod_name
1 Adam Barr 1 2 15 1 Apples
2 Sean Chai 1 3 5 2 Pears
3 Eva Corets 3 1 37 3 Oranges
4 Erin O’Melia 4 5 11 4 Bananas
2 2 1003 5 Peaches
Result
buyer_name prod_name qty
Erin O’Melia Apples 37
Adam Barr Pears 15
Erin O’Melia Pears 1003
Adam Barr Oranges 5
Eva Corets Peaches 11
Joining a Table to Itself
SELECT a.buyer_id AS buyer1, a.prod_id
,b.buyer_id AS buyer2
FROM sales AS a
JOIN sales AS b Example 3
ON a.prod_id = b.prod_id
WHERE a.buyer_id > b.buyer_id
GO

sales a sales b
buyer_id prod_id qty buyer_id prod_id qty
1 2 15 1 2 15
1 3 5 1 3 5
4 1 37 4 1 37
3 5 11 3 5 11
4 2 1003 4 2 1003

Result
buyer1 prod_id buyer2
4 2 1
Combining Multiple Result Sets
 The SQL UNION operator combines the result of two or
more SELECT statements.

 SQL UNION Syntax


SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2
Combining Multiple Result Sets

 Each Query Must Have:


 Similar data types
 Same number of columns
 Same column order in select list

Example:
SELECT (firstname + ' ' + lastname) AS name,
city, postalcode
FROM employees

UNION

SELECT companyname, city, postalcode


FROM customers
JOINS: Some notes

 Inner is default and is usually omitted

 Outeris also often omitted but should be


included as good practice

Slide 13 (of 30)


Slide 20 of 15

You might also like