Lesson02 Advanced SQL - Join
Lesson02 Advanced SQL - Join
Advanced Database
Topic & Structure of Lesson
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
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.
Example:
SELECT (firstname + ' ' + lastname) AS name,
city, postalcode
FROM employees
UNION