SQL Join Summary

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

SQL Joins Summary

LEFT JOIN INNER JOIN NON EQUIJOIN RIGHT JOIN


Select all matching rows Any join that does not involve
Select all rows in the left table, with or equality Select all rows in the right table, with
without a match in the right table or without a match in the left table

A B A B
A B A B
SELECT <list> SELECT <list>
SELECT <list> FROM A INNER JOIN B FROM A INNER JOIN B SELECT <list>
FROM A LEFT JOIN B ON A.Key = B.Key; ON A.Key < B.Value1 AND FROM A RIGHT JOIN B
ON A.Key = B.Key; A.Key > B.Value2; ON A.Key = B.Key;
NONMATCHES NONMATCHES
FULL JOIN
Select all rows in the left table without a Select all matches and nonmatches Select all nonmatches Select all rows in the right table without a
match in the right table match in the left table

A B A B
A B A B
SELECT <list> SELECT <list>
SELECT <list> FROM A FULL JOIN B FROM A FULL JOIN B SELECT <list>
FROM A LEFT JOIN B ON A.Key = B.Key; ON A.Key = B.Key FROM A RIGHT JOIN B
ON A.Key = B.Key WHERE A.Key IS NULL OR ON A.Key = B.Key
WHERE B.Key IS NULL; B.Key IS NULL; WHERE A.Key IS NULL;

To prevent missing values from joining, add AND table.key is NOT NULL to the ON clause.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other
brand and product names are trademarks of their respective companies. Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.

You might also like