Mastering SQL: Joining Tables and Retrieving Data

Sumit Kumar
4 min readJul 9, 2023

--

SQL (Structured Query Language) is a powerful tool for managing and manipulating data in relational databases. Joining tables and retrieving data are essential skills in SQL that allow you to combine information from multiple tables and extract the desired results. This blog post will explore the fundamentals of joining tables and retrieving data, including exercises and examples to solidify your understanding. We will cover different types of joins, and their syntax, and provide practice exercises to enhance your skills in retrieving data with increasing difficulty.

Joining Tables

Joining tables is a fundamental concept in SQL, enabling you to combine related data from multiple tables into a single result set. Here are exercises and examples that demonstrate joining tables:

Inner Join

Let’s consider two tables: “Customers” and “Orders.” The “Customers” table contains information about customers, while the “Orders” table contains order details. We can join these tables using the common column “customer_id” to retrieve information about customers and their orders.

SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.order_date
FROM Customers
INNER JOIN Orders ON Customers.customer_id = Orders.customer_id;

This query retrieves the customer ID, customer name, order ID, and order date for customers who have placed orders.

Left Join

Continuing from the previous example, let’s say we want to retrieve all customers, including those who have not placed any orders. We can use a left join to achieve this.

SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.order_date
FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id;

In this query, all customer records from the “Customers” table will be returned, even if there is no corresponding order record in the “Orders” table. If there is no order, the order ID and order date will be NULL.

Right Join

Now, let’s consider the reverse scenario. Suppose we want to retrieve all orders, including those with no corresponding customer records. We can use a right join.

SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.order_date
FROM Customers
RIGHT JOIN Orders ON Customers.customer_id = Orders.customer_id;

This query will return all order records, even if there is no matching customer record. If there is no customer, the customer ID and customer name will be NULL.

Full Join

In some cases, we may want to retrieve all records from both tables, regardless of whether there is a match. A full join, also known as a full outer join, can be used to accomplish this.

SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.order_date
FROM Customers
FULL JOIN Orders ON Customers.customer_id = Orders.customer_id;

This query will return all customer and order records, including those without matches. If there is no match, the respective columns will contain NULL values.

Retrieving Data

Retrieving data is the process of extracting specific information from a database. SQL offers various tools and techniques to refine our queries and obtain the desired results. Let’s explore how we can further enhance our query by using sorting, filtering, and limiting the results.

IMP: In SQL, the sequence for execution follows a specific order known as the logical query processing order. Although the actual physical execution may vary depending on the database system, the logical processing order remains consistent across most SQL implementations.

To illustrate these concepts, let’s consider our previous example of joining the Customers, Orders, and Product tables. Suppose we want to retrieve data for customers from the United States, sort the results by the order date in descending order, and limit the result set to the top 10 orders. We can modify our query as follows:

SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.order_date, Products.product_name
FROM Customers
JOIN Orders ON Customers.customer_id = Orders.customer_id
JOIN OrderDetails ON Orders.order_id = OrderDetails.order_id
JOIN Products ON OrderDetails.product_id = Products.product_id
WHERE Customers.country = 'United States'
ORDER BY Orders.order_date DESC
LIMIT 10;

In this above query, we incorporate the WHERE clause to filter the results based on the customer's country. By adding WHERE Customers.country = 'United States', we ensure that only customers from the United States are included in the result set.

Furthermore, we introduce the ORDER BY clause to sort the results based on the order date in descending order. This allows us to retrieve the most recent orders at the top of the result set.

Lastly, we utilize the LIMIT clause to restrict the number of rows returned to 10. By adding LIMIT 10 at the end of the query, we ensure that only the first 10 rows are included in the result set.

Practice Source:

Vertabelo Academy: https://academy.vertabelo.com/course/ms-sql-join-practice

SQLZoo: https://sqlzoo.net/

HackerRank: https://www.hackerrank.com/domains/tutorials/10-days-of-sql

In this blog post, we explored the fundamentals of joining tables in SQL and learned about different types of joins, including inner join, left join, right join, and full join. We also delved into the process of retrieving data, incorporating sorting, filtering, and limiting techniques to obtain the desired results. By mastering these skills, you will be equipped to harness the full power of SQL and effectively manage and extract insights from complex relational databases.

--

--