Mysql Joins

You might also like

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

Your Brief

Guide to
MySQL JOINs
with examples and best practices
What is a JOIN?

JOIN is a clause used to retrieve data from two or more tables


in a single query. These tables have to be related to each other
with a common key value. You can apply JOINs of different
types and complexity in your SELECT, UPDATE, and DELETE
statements.

However complex your JOINs might be, they can be easily


managed with dbForge Studio for MySQL, a multifunctional
IDE whose functionality goes far beyond them and covers
nearly any database-related task.
Why should you use JOINs?

JOINs in MySQL are convenient, versatile, and offer a number of practical


advantages that you will most definitely benefit from in daily use.

You can combine the required data from JOINs deliver better performance than subqueries
multiple tables into a single result set and are flexible enough to meet your criteria

JOINs can be easily used with other clauses,


You need only one query to do that
aggregate functions, subqueries, etc.
Types of Each JOIN type defines the way two or more tables
MySQL JOINs are related in a query.

INNER JOIN CROSS JOIN SELF JOIN


creates a result table by combining rows in multiple creates a result table containing combinations of each joins a table to itself and compares rows within it;
tables that have matching values row of the first table with each row of the second table available indirectly via INNER JOIN or LEFT OUTER JOIN

LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN


creates a result table that includes all the rows from creates a result table that includes all the rows from the MySQL does not support FULL OUTER JOIN directly, but
the left table along with the rows from the right right table along with the rows from the left table that the same result—a table that includes both matching and
table that meet the join condition; the non-matching meet the join condition; the non-matching rows have non-matching rows from the joined tables—can be
rows have NULL values NULL values achieved by combining LEFT and RIGHT OUTER JOINs
INNER JOIN INNER JOIN allows retrieving only those rows from Table A and Table B
that match (i.e., meet the join condition). This is the most widely used
type of JOIN.

SYNTAX

SELECT columns
FROM tableA
INNER JOIN tableB
ON tableA.column = tableB.column;
LEFT As opposed to INNER JOIN, the three types of OUTER JOINs return
not only matching rows, but non-matching ones as well.
OUTER JOIN For instance, LEFT OUTER JOIN allows retrieving all rows from Table A along
with those rows from Table B that meet the join condition. For the rows from
Table A that do not match the condition, NULL values are displayed.

SYNTAX

SELECT columns
FROM tableA
LEFT [OUTER] JOIN tableB
ON tableA.column = tableB.column;
RIGHT Similarly to the previous type, RIGHT OUTER JOIN allows retrieving all
rows from Table B along with those rows from Table A that meet the join
OUTER JOIN condition. For the rows from Table B that do not match the condition,
NULL values are displayed.

SYNTAX

SELECT columns
FROM tableA
RIGHT [OUTER] JOIN tableB
ON tableA.column = tableB.column;
CROSS JOIN CROSS JOIN, also known as cartesian join, retrieves all combinations of
rows from each table. The result set comprises paired combinations of
each row of Table A with each row of Table B.

SYNTAX

SELECT columns
FROM tableA
CROSS JOIN tableB;
FULL Unlike SQL Server, MySQL does not support FULL OUTER JOIN. However,
to get the same result, you can simply use both LEFT and RIGHT OUTER
OUTER JOIN JOINs in a single query, as shown in the syntax below.

SYNTAX

SELECT * FROM tableA


LEFT JOIN tableB ON tableA.id = tableB.id
UNION
SELECT * FROM tableA
RIGHT JOIN tableB ON tableA.id = tableB.id
SELF JOIN Although MySQL does not support SELF JOIN directly, you have two
ways to join a MySQL table to itself.

The INNER JOIN clause can be used to join a table to


itself and retrieve the required results.

SYNTAX

SELECT columns
FROM tableA
INNER JOIN tableA
ON tableA.columnA = tableA.columnB;

Alternatively, you can apply the LEFT OUTER JOIN


clause for similar purposes.

SYNTAX

SELECT columns
FROM tableA
LEFT [OUTER] JOIN tableA
ON tableA.columnA = tableA.columnB;
Best practices Now as you know the difference between JOIN types,
learn the following tips to make your work with JOINs most
effective.

Use explicit JOINs (i.e., JOIN and ON keywords)


Choose the appropriate JOIN type carefully
Apply table aliases when joining multiple tables or tables with
long names
Use the [table alias].[column] name format for columns in queries
Apply column aliases (the names assigned to the columns in the
result dataset)
If necessary, combine several JOINs for maximum flexibility
dbForge Studio for MySQL:
your comprehensive IDE for MySQL databases

dbForge Studio is a multifunctional IDE for SQL editor


The built-in SQL editor was designed to help you effectively manage your SQL
MySQL, which helps you handle a variety of code with smart completion, syntax highlighting, formatting, refactoring, and a
slew of other productivity enhancements that let you get a sharper focus on
tasks, including operations with JOINs. your work.

With its help, you can instantly get at least Context-sensitive code completion
30% more effective and reduce the time With dbForge Studio, you can easily speed up your routine SQL coding by at
least 90% with context-sensitive keyword and object suggestions, which include
auto-generation of JOIN clauses. Auxiliary features include code snippets,
you typically spend on your routine work column picker, wildcards, highlighting, and parameter information.

with databases by about 50%.


Instant syntax check
The syntax checker is one of the most valuable features integrated into the SQL
editor. Whenever the checker detects an error in the code that you are typing, it
instantly highlights the problematic place so you can fix it immediately.

SQL formatting
It is easy to improve the readability, consistency, and standardization of your
DOWNLOAD FREE 30-DAY TRIAL code with the rich SQL formatting options offered by the Studio. Depending on
your needs, you can apply automatic, manual, or wizard-aided formatting.
dbForge Studio for MySQL:
visual query building

In dbForge Studio, you can create queries of any


complexity on visual diagrams, which don't require any
SQL coding. You are free to build, group, and arrange
your tables, construct and visualize JOINs between
them, easily navigate across your database objects, as
well as add and update conditions — all using an
intuitive drag-and-drop interface.

This feature typically doubles the query building


productivity of its user. Furthermore, the performance of
your queries can be optimized with the built-in Query
I WANT TO TRY!
Profiler.
Helpful resources

Here are a few bonus insights into MySQL JOINs


and dbForge Studio for MySQL.

MySQL JOINs: video MySQL JOINs tutorial Beginner's guide Helpful resources
tutorial for beginners with examples to dbForge Studio to boost your skills

Get started with dbForge Studio for free today! DOWNLOAD 30-DAY TRIAL

You might also like