Introduction
In this article, we will explore two of the most powerful and widely used features in SQL: JOINs and Window Functions. We will begin by understanding what they are and how they work, and then walk through practical examples to see when, where, and why they are used in real-world scenarios.
Let’s start with joins:
What are Joins?
In Structured Query Language (SQL), a join is a clause used to combine rows from two or more tables based on a related column between them. The purpose of a joins is to retrieve data that is spread across multiple tables into a single table, providing a unified view.
Example: In database with an orders and customers table, a join can be used to answer questions such as which customer placed an order.
Type of Joins
1. INNER JOIN
It combines two or more tables based on a specified common column with matching values and only returns the set of records that have a match in all the involved tables. The rows that don’t have a match in the other table(s) are excluded from the result set.
Example:

select first_name, last_name
from article.customers
inner join article.orders on customers.customer_id = orders.customer_id;

2. LEFT JOIN
It retrieves all rows from the left (first) table and matching rows from the right (second) table. If a row in the left table has no corresponding match in the right table based on the join condition, the result will contain NULL values for the columns of the right table.
Example:
Using our previous tables
--LEFT JOIN
select first_name, last_name, order_date
from article.customers
left join article.orders on customers.customer_id = orders.customer_id;

The left join is also known as the left outer join.
3. RIGHT JOIN
It retrieves all rows from the right (second) table and matching rows from the left (first) table. If a row in the right table has no corresponding match in the left table based on the join condition, the result will contain NULL values for the columns of the left table.
--RIGHT JOIN
select first_name, last_name, order_date
from article.customers
right join article.orders on customers.customer_id = orders.customer_id;

The right join is also known as the right outer join.
4. FULL OUTER JOIN
It returns all rows from both the left and right tables, combining matching records and using NULL values for columns where there is no match
Example:
-- FULL OUTER JOIN
select first_name, last_name, email, phone_number,order_id, order_date, book_id
from article.customers
full outer join article.orders on customers.customer_id = orders.order_id;

Now, let’s look at window functions:
What are Window Functions?
In SQL, Window functions perform calculations across a set of table rows related to the selected row without merging these rows into a single output/value. Unlike traditional output functions such as SUM() and COUNT() which reduce multiple rows to one, window functions return a value for each row in the original result set.
They operate over a window (specific set of rows) defined by the OVER() clause.
Key Components of SQL Window Functions
Select: This defines the columns you want to select from the table_name(The columns you select, create your window).
Function: This is the window function you want to use.
Over Clause: This defines the partitioning and ordering of rows and can be applied with functions to compute aggregated values.
Partition by: This divides rows into partitions based on specified expressions. It is suitable for large datasets because it makes them simpler to manage.
Order by: This is a specified order expression to define the order in which rows will be processed within each partition.
Output column: This is the name you give to your output column.
Types of window functions in SQL
1. Aggregate window functions: They calculate aggregates over a window of rows while retaining individual rows.
2. Ranking window functions: They provide rankings of rows within a partition based on a specific criteria.
3. Value window functions: They are used to assign to rows, values from other rows. It is usually possible to replicate the values of these functions using two nested queries, hence they are not that common compared to aggregate and ranking window functions.
Example:

Let’s use a ranking window function RANK() (which provides a unique rank to each row while skipping duplicates), an OVER() to define the partitioning and ordering of rows using PARTITION BY and ORDER BY then define an output column that will contain out results.



To learn more on the other window functions, click here Window functions
Conclusion
From this article, we have gone through and understood what are joins and window functions are. Through clear explanations and practical, real-world examples, you’ve learned what they are, the different types available, how they work, and exactly how to write clean, efficient SQL queries to achieve your desired results. Mastering these powerful tools will dramatically improve your ability to analyze, transform and retrieve data with precision. Start applying them in your own projects today, the more you practice, the more natural they will feel.
Happy querying!

