SQL Joins Cheat Sheet
1. Quick Reference | Join Type | Description | | --- | --- | | INNER JOIN | Returns records with matching values in both tables | | LEFT JOIN | Returns all records from the left table and matching rec...
TechSilo
AI-assisted and human-curated
1. **Quick Reference**
| Join Type | Description |
| --- | --- |
| INNER JOIN | Returns records with matching values in both tables |
| LEFT JOIN | Returns all records from the left table and matching records from the right table |
| RIGHT JOIN | Returns all records from the right table and matching records from the left table |
| FULL OUTER JOIN | Returns all records from both tables with null values where no match exists |
2. **Syntax**
The basic syntax for a SQL join is:
SELECT *
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;* table1 and table2 are the names of the tables to be joined
* column_name is the name of the column to join on
* JOIN can be replaced with the specific type of join (e.g. INNER JOIN, LEFT JOIN, etc.)
3. **Common Patterns**
* Inner Join with Multiple Conditions:
SELECT *
FROM table1
INNER JOIN table2
ON table1.column1 = table2.column1 AND table1.column2 = table2.column2;* Left Join with Null Check:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name
WHERE table2.column_name IS NULL;* Full Outer Join with Order By:
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
ORDER BY table1.column_name;* Joining Multiple Tables:
SELECT *
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
INNER JOIN table3
ON table2.column_name = table3.column_name;4. **Gotchas**
* No Matching Records: If there are no matching records in the joined tables, the result set will be empty.
* Null Values: If a join is performed on a column that contains null values, the result set may not be what is expected.
* Table Order: The order of the tables in a join can affect the result set, especially when using LEFT JOIN or RIGHT JOIN.
* Joining on Non-Unique Columns: Joining on non-unique columns can result in a Cartesian product, which can be very large and slow.
5. **Related Commands**
* UNION and UNION ALL for combining the results of multiple SELECT statements
* GROUP BY and HAVING for grouping and filtering the results of a join
* SUBQUERY for using the results of one query as input to another query
* EXISTS and IN for checking if a record exists in a table or if a value is in a list of values.
Want to put this into practice?
Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.
Related blog posts
CSS Flexbox Cheat Sheet: Alignment, Wrapping, and Responsive Rows
A practical CSS Flexbox reference covering display:flex, flex-direction, gap, justify-content, align-items, flex-wrap, and flex shorthand.
Read postCSS Grid Cheat Sheet: Columns, Rows, Areas, and Responsive Layouts
A practical CSS Grid reference with repeat(), minmax(), auto-fit, alignment, named areas, and responsive layout examples.
Read postnpm Scripts Cheat Sheet: Commands, Arguments, and Automation
A developer reference for package.json scripts, npm run, pre/post hooks, environment variables, and safe task automation.
Read postTypeScript Utility Types Cheat Sheet: Pick, Omit, Partial and More
Practical examples of TypeScript Partial, Required, Pick, Omit, Record, ReturnType, Awaited, Extract, and Exclude.
Read postGit Commands Cheat Sheet
1. Quick Reference | Command | Description | | --- | --- | | git init | Initialize a new Git repository | | git clone [url] | Clone a Git repository from a URL | | git add [file] | Stage a file ...
Read post