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

SQL Cheat Sheet - JOIN Statements

This document is a SQL cheat sheet focused on various JOIN statements, including CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, and SELF JOIN. Each type of join is described with its syntax, purpose, and an example query. Additionally, it mentions the UNION operator for combining result sets from multiple SELECT statements.

Uploaded by

kaif mohammad
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)
3 views

SQL Cheat Sheet - JOIN Statements

This document is a SQL cheat sheet focused on various JOIN statements, including CROSS JOIN, INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, and SELF JOIN. Each type of join is described with its syntax, purpose, and an example query. Additionally, it mentions the UNION operator for combining result sets from multiple SELECT statements.

Uploaded by

kaif mohammad
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/ 2

11/28/24, 2:10 AM 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) combination of each SELECT DEPT_ID_DEP, LOCT_ID FROM
FROM table1 CROSS JOIN
Join table2; row of the first table DEPARTMENTS CROSS JOIN LOCATIONS;
with each row of the
second table.

You can use an inner


SELECT column_name(s) join in a SELECT select E.F_NAME,E.L_NAME,
FROM table1 INNER JOIN statement to retrieve
Inner JH.START_DATE from EMPLOYEES as E
table2 ON
only the rows that INNER JOIN JOB_HISTORY as JH on
Join table1.column_name =
E.EMP_ID=JH.EMPL_ID where E.DEP_ID
table2.column_name; satisfy the join
conditions on every ='5';
WHERE condition;
specified table.

SELECT column_name(s)
The LEFT OUTER JOIN
will return all records select
Left FROM table1 LEFT OUTER
from the left side table E.EMP_ID,E.L_NAME,E.DEP_ID,D.DEP_NAME
JOIN table2 ON
Outer from EMPLOYEES AS E LEFT OUTER JOIN
table1.column_name = and the matching DEPARTMENTS AS D ON
Join table2.column_name WHERE records from the right E.DEP_ID=D.DEPT_ID_DEP;
condition;
table.

SELECT column_name(s) The RIGHT OUTER JOIN select


Right FROM table1 RIGHT OUTER returns all records from E.EMP_ID,E.L_NAME,E.DEP_ID,D.DEP_NAME
JOIN table2 ON
Outer table1.column_name =
the right table, and the from EMPLOYEES AS E RIGHT OUTER JOIN
Join matching records from DEPARTMENTS AS D ON
table2.column_name WHERE
the left table. E.DEP_ID=D.DEPT_ID_DEP;
condition;

The FULL OUTER JOIN


SELECT column_name(s) clause results in the
Full FROM table1 FULL OUTER inclusion of rows from select E.F_NAME,E.L_NAME,D.DEP_NAME
JOIN 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.

A self join is regular SELECT B.* FROM EMPLOYEES A JOIN


Self SELECT column_name(s)
EMPLOYEES B ON A.MANAGER_ID =
FROM table1 T1, table1 join but it can be used
Join T2 WHERE condition;
B.MANAGER_ID WHERE A.EMP_ID =
to joined with itself. 'E1001';

Joins in MySQL using phpMyAdmin

about:blank 1/2
11/28/24, 2:10 AM about:blank

SELECT column_name(s) FROM


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

Author(s)
D.M Naidu

about:blank 2/2

You might also like