DEV Community

Cover image for Mastering Joins and Window Functions in SQL
Elizabeth Njihia
Elizabeth Njihia

Posted on

Mastering Joins and Window Functions in SQL

Introduction.

If you’re working with data — especially in tools like Power BI, Excel, or databases like MySQL or PostgreSQL — understanding Joins and Window Functions is a game changer. These two concepts help you combine data and analyze it in powerful ways without losing detail. Here is an article to guide you through joins and window functions.

🔗What Are Joins?

A Join combines rows from two or more tables based on a related column.

Importance of joins.
Most databases are structured into multiple tables to avoid duplication. To analyze the data meaningfully, you need to bring those tables together.

Types of Joins
1️⃣ INNER JOIN
Returns only the matching records in both tables.

Example;

SELECT customers.name, orders.order_date
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;`
Enter fullscreen mode Exit fullscreen mode

Meaning: Only customers who have placed orders will appear.

2️⃣ LEFT JOIN (LEFT OUTER JOIN)
Returns all records from the left table and matching records from the right table.
Example;

SELECT customers.name, orders.order_date
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
Enter fullscreen mode Exit fullscreen mode

Meaning: All customers appear — even those who never placed orders (their order columns will be NULL).

3️⃣ RIGHT JOIN
Returns all records from the right table and matching records from the left.
Example;

SELECT customers.name, orders.order_date
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id;
Enter fullscreen mode Exit fullscreen mode

4️⃣ FULL OUTER JOIN
Returns all records when there is a match in either table.
Example;

SELECT customers.name, orders.order_date
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer_id;
Enter fullscreen mode Exit fullscreen mode

🧠 Summary
INNER JOINOnly matching rows

LEFT JOINEverything on the left + matches

RIGHT JOINEverything on the right + matches

FULL JOINEverything from both

🪟 What Are Window Functions?

Window functions perform calculations across a set of rows related to the current row — without collapsing the result into a single row like GROUP BY does.

Basic Structure of a Window Function

SELECT 
    column,
    window_function() OVER (
        PARTITION BY column
        ORDER BY column
    )
Enter fullscreen mode Exit fullscreen mode

FROM table;
Key Parts:

OVER()defines the window

PARTITION BYdivides data into groups

ORDER BYdefines order within the group

Common Window Functions
1️⃣ ROW_NUMBER()
Assigns a unique number to each row.

SELECT 
    name,
    salary,
    ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank_position
FROM employees;
Enter fullscreen mode Exit fullscreen mode

Use case: Ranking employees by salary.

2️⃣ RANK()
Similar to ROW_NUMBER but allows ties.

SELECT 
    name,
    salary,
    RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees;
Enter fullscreen mode Exit fullscreen mode

3️⃣ DENSE_RANK()
Like RANK but doesn’t skip numbers after ties.

SELECT 
    name,
    salary,
    DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank
FROM employees;
Enter fullscreen mode Exit fullscreen mode

4️⃣ SUM() OVER()
Calculates running totals or grouped totals without grouping the table.

SELECT 
    department,
    salary,
    SUM(salary) OVER (PARTITION BY department) AS dept_total_salary
FROM employees;
Enter fullscreen mode Exit fullscreen mode

This keeps every row but adds department totals.

5️⃣ Running Total

SELECT 
    order_date,
    amount,
    SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders;
Enter fullscreen mode Exit fullscreen mode

🧠 summary

  • They calculate across related rows.
  • They do not reduce rows.
  • They add analytical power without losing detail.
  • Perfect for ranking, running totals, comparisons.

Window functions increase insight, not width or height.

🗣️ Insights
Think of Joins as connecting puzzle pieces. Each table is a piece of information. When you join them, you build the full picture.
Think of Window Functions as giving each row awareness of its neighbors. Instead of summarizing the table, you let each row “see” the other rows around it.

Joins = relationship builders

Window functions = insight enhancers

Together, they form the backbone of advanced SQL analytics.

Visualization;

Top comments (0)