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

Python and Sql

This document is a cheat sheet for SQL JOIN statements, detailing various types such as CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, and SELF JOIN, along with their syntax and examples. It also mentions the use of the UNION operator to combine result sets from multiple SELECT statements. The document includes authorship and changelog information.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python and Sql

This document is a cheat sheet for SQL JOIN statements, detailing various types such as CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, and SELF JOIN, along with their syntax and examples. It also mentions the use of the UNION operator to combine result sets from multiple SELECT statements. The document includes authorship and changelog information.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6/22/23, 4:21 PM about:blank

SQL Cheat Sheet: JOIN statements

Joins

Topic Syntax Description Example


The CROSS JOIN is used to
generate a paired
Cross SELECT column_name(s) FROM
combination of each row SELECT DEPT_ID_DEP, LOCT_ID FROM
Join table1 CROSS JOIN table2; DEPARTMENTS CROSS JOIN LOCATIONS;
of the first table with each
row of the second table.
SELECT column_name(s) FROM You can use an inner join select E.F_NAME,E.L_NAME,
table1 INNER JOIN table2 in a SELECT statement to JH.START_DATE from EMPLOYEES as E
Inner ON table1.column_name = retrieve only the rows that INNER JOIN JOB_HISTORY as JH on
Join table2.column_name; WHERE satisfy the join conditions E.EMP_ID=JH.EMPL_ID where E.DEP_ID
condition;
on every specified table. ='5';
SELECT column_name(s) FROM The LEFT OUTER JOIN will select
Left table1 LEFT OUTER JOIN return all records from the E.EMP_ID,E.L_NAME,E.DEP_ID,D.DEP_NAME
table2 ON
Outer table1.column_name =
left side table and the from EMPLOYEES AS E LEFT OUTER JOIN
Join table2.column_name WHERE matching records from the DEPARTMENTS AS D ON
E.DEP_ID=D.DEPT_ID_DEP;
condition; right table.
SELECT column_name(s) FROM The RIGHT OUTER JOIN select
Right table1 RIGHT OUTER JOIN returns all records from E.EMP_ID,E.L_NAME,E.DEP_ID,D.DEP_NAME
table2 ON
Outer table1.column_name =
the right table, and the from EMPLOYEES AS E RIGHT OUTER JOIN
Join table2.column_name WHERE matching records from the DEPARTMENTS AS D ON
E.DEP_ID=D.DEPT_ID_DEP;
condition; left table.
The FULL OUTER JOIN
SELECT column_name(s) FROM clause results in the
Full table1 FULL OUTER JOIN inclusion of rows from select E.F_NAME,E.L_NAME,D.DEP_NAME
table2 ON from EMPLOYEES AS E FULL OUTER JOIN
Outer table1.column_name =
two tables. If a value is DEPARTMENTS AS D ON
Join table2.column_name WHERE missing when rows are E.DEP_ID=D.DEPT_ID_DEP;
condition; joined, that value is null in
the result table.
SELECT column_name(s) FROM A self join is regular join SELECT B.* FROM EMPLOYEES A JOIN
but it can be used to joined EMPLOYEES
B ON A.MANAGER_ID =
Self Join table1 T1, table1 T2 WHERE
B.MANAGER_ID WHERE A.EMP_ID =
condition; with itself. 'E1001';

Joins in MySQL using phpMyAdmin

about:blank 1/2
6/22/23, 4:21 PM about:blank

SELECT column_name(s) FROM


table1 LEFT OUTER JOIN select
table2 ON table1.column_name E.F_NAME,E.L_NAME,D.DEP_NAME
= table2.column_name WHERE from EMPLOYEES AS E LEFT
condition OUTER JOIN DEPARTMENTS AS D
ON E.DEP_ID=D.DEPT_ID_DEP
UNION The UNION operator is used to
Full Outer UNION
combine the result-set of two or
Join SELECT column_name(s) more SELECT statements. select
FROM table1
RIGHT OUTER JOIN table2 E.F_NAME,E.L_NAME,D.DEP_NAME
ON table1.column_name = from EMPLOYEES AS E
table2.column_name RIGHT OUTER JOIN DEPARTMENTS
WHERE condition AS D ON
E.DEP_ID=D.DEPT_ID_DEP

Author(s)
D.M Naidu

Changelog
Date Version Changed by Change Description
2023-05-04 1.1 Benny Li Formatting changes
2022-10-04 1.0 D.M.Naidu Initial Version

about:blank 2/2

You might also like