How to use JOINs effectively?

How to use JOINs effectively?

A JOIN combines tables by using a primary or foreign key to align the information coming from both tables in the combination process. JOINs use these keys to identify relationships and corresponding values across tables.?

The general JOIN syntax

As you can see from the syntax, the JOIN statement is part of the FROM clause of the query. JOIN in SQL indicates that you are going to combine data from two tables. ON in SQL identifies how the tables are to be matched for the correct information to be combined from both.?

Type of JOINs

There are four general ways in which to conduct JOINs in SQL queries: INNER, LEFT, RIGHT, and FULL OUTER.

Here is what these different JOIN queries do.

INNER JOIN

INNER is optional in this SQL query because it is the default as well as the most commonly used JOIN operation. You may see this as JOIN only. INNER JOIN returns records if the data lives in both tables. For example, if you use INNER JOIN for the 'customers' and 'orders' tables and match the data using the customer_id key, you would combine the data for each customer_id that exists in both tables. If a customer_id exists in the customers table but not the orders table, data for that customer_id isn’t joined or returned by the query.

The results from the query might look like the following, where customer_name is from the customers table and product_id and ship_date are from the orders table:

The data from both tables was joined together by matching the customer_id common to both tables. Notice that customer_id doesn’t show up in the query results. It is simply used to establish the relationship between the data in the two tables so the data can be joined and returned.?

LEFT JOIN

You may see this as LEFT OUTER JOIN, but most users prefer LEFT JOIN. Both are correct syntax. LEFT JOIN returns all the records from the left table and only the matching records from the right table. Use LEFT JOIN whenever you need the data from the entire first table and values from the second table, if they exist. For example, in the query below, LEFT JOIN will return customer_name with the corresponding sales_rep, if it is available. If there is a customer who did not interact with a sales representative, that customer would still show up in the query results but with a NULL value for sales_rep.

The results from the query might look like the following where customer_name is from the customers table and sales_rep is from the sales table. Again, the data from both tables was joined together by matching the customer_id common to both tables even though customer_id wasn't returned in the query results.

RIGHT JOIN

You may see this as RIGHT OUTER JOIN or RIGHT JOIN. RIGHT JOIN returns all records from the right table and the corresponding records from the left table. Practically speaking, RIGHT JOIN is rarely used. Most people simply switch the tables and stick with LEFT JOIN. But using the previous example for LEFT JOIN, the query using RIGHT JOIN would look like the following:

The query results are the same as the previous LEFT JOIN example.

FULL OUTER JOIN

You may sometimes see this as FULL JOIN. FULL OUTER JOIN returns all records from the specified tables. You can combine tables this way, but remember that this can potentially be a large data pull as a result. FULL OUTER JOIN returns all records from both tables even if data isn’t populated in one of the tables. For example, in the query below, you will get all customers and their products’ shipping dates. Because you are using a FULL OUTER JOIN, you may get customers returned without corresponding shipping dates or shipping dates without corresponding customers. A NULL value is returned if corresponding data doesn’t exist in either table.

The results from the query might look like the following.

For more information

JOINs are going to be useful for working with relational databases and SQL—and you will have plenty of opportunities to practice them on your own. Here are a few other resources that can give you more information about JOINs and how to use them:

  • SQL JOINs: This is a good basic explanation of JOINs with examples. If you need a quick reminder of what the different JOINs do, this is a great resource to bookmark and come back to later. ?
  • Database JOINs - Introduction to JOIN Types and Concepts: This is a really thorough introduction to JOINs. Not only does this article explain what JOINs are and how to use them, but it also explains the various scenarios in more detail of when and why you would use the different JOINs. This is a great resource if you are interested in learning more about the logic behind JOINing.
  • SQL JOIN Types Explained in Visuals: This resource has a visual representation of the different JOINs. This is a really useful way to think about JOINs if you are a visual learner, and it can be a really useful way to remember the different JOINs.?
  • SQL JOINs: Bringing Data Together One Join at a Time: Not only does this resource have a detailed explanation of JOINs with examples, but it also provides example data that you can use to follow along with their step-by-step guide. This is a useful way to practice JOINs with some real data.?
  • SQL JOIN: This is another resource that provides a clear explanation of JOINs and uses examples to demonstrate how they work. The examples also combine JOINs with aliasing.

要查看或添加评论,请登录

Sheik Shahul M的更多文章

  • Before you accept, negotiating the contract

    Before you accept, negotiating the contract

    Picture this: you have made it through the end of the interview process and great news- the hiring manager wants to…

  • Top tips for interview success

    Top tips for interview success

    Tip 1: Find connections between the job listing and your resume First, re-read your resume and the job description to…

  • The interview process

    The interview process

    For data analyst positions, you can think of the job interview process as having four stages: introduction, skill test,…

  • How to create your online portfolio?

    How to create your online portfolio?

    This reading provides a checklist about what to include in your portfolio, where you can set up accounts to host your…

  • Resources to explore case studies

    Resources to explore case studies

    Inspiration is everywhere. You can get ideas for a case study to include in your portfolio from your hobbies, travels…

  • Your portfolio and case study checklist

    Your portfolio and case study checklist

    It is important to understand the key components of both a portfolio and case study, as both are essential to success…

  • Common problems when visualizing in R

    Common problems when visualizing in R

    Coding errors are an inevitable part of writing code—especially when you are first beginning to learn a new programming…

  • Working with biased data

    Working with biased data

    Every data analyst will encounter an element of bias at some point in the data analysis process. That’s why it’s so…

  • File-naming conventions

    File-naming conventions

    An important part of cleaning data is making sure that all of your files are accurately named. Although individual…

  • When to use RStudio?

    When to use RStudio?

    As a data analyst, you will have plenty of tools to work with in each phase of your analysis. Sometimes, you will be…

社区洞察

其他会员也浏览了