...

/

Mix and Match Data

Mix and Match Data

Learn how data analysts use SQL joins to combine relational data for deeper insights.

In the real-world, data doesn’t come wrapped in a perfect little bow. It’s all over the place; there are users in one table, orders in another, and maybe products in a third. On their own, these tables don’t narrate the full story. But together? They can show how customers behave, what’s selling, and what’s going wrong. As data analysts, our job is to bring that story to life by connecting the dots.

But hold on, we don’t just join tables like mixing all the leftovers in the fridge. First, we’ve got to understand how tables are built and how they relate to each other. That means getting friendly with primary keys, foreign keys, and SQL joins, which constitute our go-to toolkit for turning scattered pieces into a clean, connected dataset ready for analytics.

Creating tables in SQL

In SQL, a table is like a dataset: rows represent records, and columns represent fields (attributes). As data analysts, we often query these tables to explore data, clean it, or prepare it for analysis.

Here’s a simple syntax to define a table:

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Define a table

For example, a table of customers:

CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);
Example table

In this example, CustomerID is marked as a PRIMARY KEY, meaning it will uniquely identify each customer; this is essential when linking this table to others. The Name and Email columns use the VARCHAR(100) data type, which stands for variable-length character data. The number 100 sets the maximum length of the text to 100 characters.

Now let’s take a closer look at what primary keys are and why they matter.

Understanding primary keys

A primary key uniquely identifies each row in a table. For example, no two customers should share the same CustomerID. The following are key characteristics:

  • Unique: No duplicates allowed.

  • ...