SQL Joins & Window Functions

Published: (March 2, 2026 at 11:19 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

SQL joins are used to combine data from multiple tables based on a related column.

Window functions perform calculations across a set of table rows related to the current row, enabling row‑wise aggregations without collapsing the data.

In this article you will learn in depth how SQL joins and window functions work, when to use them, and how they solve real‑world business problems. Let’s get started.

Types of SQL joins

There are four main types of SQL joins.

Inner join

Returns only the records that have matching values in both tables.

Inner Join Example

Business Question

Which customers have placed orders?

Assume we have two tables:

  • Customers
  • Orders

Customers & Orders Tables

Sample Data

Query

SELECT c.customer_id,
       c.customer_name,
       o.order_id
FROM   customers c
INNER JOIN orders o
       ON c.customer_id = o.customer_id;

Result

Inner Join Result

Tim Adagala is not in the output because he has no order.

Left join

Returns all records from the left table and only matching rows from the right table.

Left Join Diagram

Business Question

Show all customers, even those who have yet to order.

Query

SELECT c.customer_id,
       c.customer_name,
       o.order_id
FROM   customers c
LEFT  JOIN orders o
       ON c.customer_id = o.customer_id;

Result

Left Join Result

The NULL in order_id indicates that Tim Adagala exists but has not placed any order.

Right join

Returns all records from the right table and only matching rows from the left table (the reverse of a LEFT JOIN).

Right Join Diagram

Business Question

Show all orders and their customers (if available).
Keep all orders; attach customer info if it exists.

Query

SELECT c.customer_id,
       c.customer_name,
       o.order_id
FROM   customers c
RIGHT  JOIN orders o
       ON c.customer_id = o.customer_id;

Result

Right Join Result

Full join

Returns all records from both tables, regardless of whether there is a match.

Full Join Diagram

Business Question

Show all customers and all orders, including unmatched records on both sides.

Query

SELECT c.customer_id,
       c.customer_name,
       o.order_id
FROM   customers c
FULL   OUTER JOIN orders o
       ON c.customer_id = o.customer_id;

Output

Result Image

Window Functions

A window function performs calculations across a set of rows related to the current row, without collapsing the result into a single row like a GROUP BY clause.

Syntax of a Window Function

function_name(column)
OVER (
    PARTITION BY column   -- optional
    ORDER BY column       -- sometimes required
)
  • OVER() – Defines the window of rows to operate on. This clause is mandatory.
  • PARTITION BY – (Optional) Divides data into logical groups.
  • ORDER BY – (Sometimes required) Defines the order of rows inside each partition. Required for:
    • Ranking functions
    • Navigation functions
    • Running totals
0 views
Back to Blog

Related posts

Read more »

SQL JOINS AND WINDOW FUNCTIONS

SQL Joins and Window Functions !tonny otienohttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uplo...

PostgreSQL Joins and Window Function

Understanding JOINS in PostgreSQL Joins let you merge data from multiple tables or views by linking them through related columns. The choice of join type depen...