Join and Subquery in SQL

In SQL querying, we sometimes need to fetch data from more than one table, for which we need to join the tables together or apply subqueries. Subqueries and the Join operation help us retrieve related data by linking the tables or running nested queries within a larger query.

In this chapter, we will have an elaborate discussion on joins and subqueries with the help of clear examples and their resulting tables.

Sample Tables and Data

For the examples in this chapter, we will use the following three tables: Customers, Orders, and Products. We will use these three tables to demonstrate joins and subqueries.

Here is the Customers table −

customer_idfirst_namelast_nameemailphone
1AmritaJoshi[email protected]123-456-7890
2BimalSaha[email protected]234-567-8901
3ChandanTudu[email protected]345-678-9012

This is the Products table −

product_idproduct_namecategorypricestock_quantity
101LaptopElectronics1000.0010
102SmartphoneElectronics800.0020
103Office ChairFurniture150.005

And, the Orders table is as follows −

order_idcustomer_idproduct_idquantityorder_date
1110112023-11-01 10:00:00
2210322023-11-02 12:30:00
3310212023-11-03 15:45:00
4110312023-11-04 14:00:00

Joins in SQL

Joins combine rows from two or more tables based on a related column. In SQL we have option for several types of joins. We will see the most commonly used techniques.

The Inner Join

The Inner Join retrieves rows that have matching values in both tables. If some matching is not there the entire row will be neglected.

Example: Fetch Customer Orders

Use the following query to fetch all the customer orders −

SELECT c.first_name, c.last_name, p.product_name, o.quantity, o.order_date
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
JOIN Products p ON o.product_id = p.product_id;

This query combines the Orders, Customers, and Products tables to display complete order details −

first_namelast_nameproduct_namequantityorder_date
AmritaJoshiLaptop12023-11-01 10:00:00
BimalSahaOffice Chair22023-11-02 12:30:00
ChandanTuduSmartphone12023-11-03 15:45:00
AmritaJoshiOffice Chair12023-11-04 14:00:00

The Left Join

The Left Join retrieves all the rows from the left table and the matching rows from the right table. If no match exists, NULL values are returned for the right table’s columns.

Example: Customers with or without Orders

Use the following query to fetch the customer details with or without orders −

SELECT c.first_name, c.last_name, o.order_id
FROM Customers c
LEFTJOIN Orders o ON c.customer_id = o.customer_id;

It fetches all customers, including those with no orders (if there were any).

first_namelast_nameorder_id
AmritaJoshi1
AmritaJoshi4
BimalSaha2
ChandanTudu3

The Right Join

The Right Join retrieves all the rows from the right table and the matching rows from the left table.

Example: Products with or without Orders

Use the following query to fetch the products with or without orders −

SELECT p.product_name, o.order_id
FROM Products p
RIGHTJOIN Orders o ON p.product_id = o.product_id;

The query fetches all the rows from the Orders table. When it is a product that does not match, it will be NULL. All products are matched in this case.

product_nameorder_id
Laptop1
Office Chair2
Smartphone3
Office Chair4

The Full Outer Join

Combining both the Left and Right join makes the new join a Full Outer Join. It will fetch all the rows from both the tables.

Subqueries in SQL

Subqueries are nested queries inside another query. They can return single values or entire tables. Subqueries are useful for solving complex problems step-by-step.

Subqueries in WHERE Clause

Subqueries are mostly used in the WHERE clause to filter data based on the result of another query.

Example: Find Customers Who Ordered the Most Expensive Product

Here, we have three queries all together. The innermost query finds the product_id having the maximum number of products. Then, this product_id is matched with the Orders table in the second inner query, from where it gets the customer_id. Based on the customer_id, it fetches the name of the customer.

SELECT first_name, last_name
FROM Customers
WHERE customer_id IN(SELECT customer_id
   FROM Orders
   WHERE product_id =(SELECT product_id
  FROM Products
  WHERE price =(SELECTMAX(price)FROM Products)));</pre>

This query identifies the customers who ordered the most expensive product (Laptop).

first_namelast_name
AmritaJoshi

Subqueries in SELECT Clause

Subqueries can also be used in the SELECT clause to include calculated data in the output.

Example: Total Quantity Ordered by Each Product

Use the following query to get the details of the total quantity ordered by each product −

SELECT product_name,(SELECTSUM(quantity)FROM Orders
   WHERE Orders.product_id = Products.product_id)AS total_quantity_ordered
FROM Products;

This query calculates the total quantity ordered for each product using a subquery. It is an advanced version of GROUP BY querying.

product_nametotal_quantity_ordered
Laptop1
Smartphone1
Office Chair3

Subqueries in FROM Clause

Subqueries in the FROM clause are called derived tables and are used to create temporary tables.

Example: Find Revenue per Product

Use the following query to get the revenue details per product −

SELECT product_name, revenue
FROM(SELECT p.product_name,SUM(o.quantity * p.price)AS revenue
   FROM Orders o
   JOIN Products p ON o.product_id = p.product_id
   GROUPBY p.product_name
)AS ProductRevenue;

The subquery calculates revenue per product, and the main query fetches it for display.

product_namerevenu
Laptop1000.00
Smartphone800.00
Office Chair450.00

When to Use Joins vs. Subqueries

Both the approaches, joins and subqueries, are equally useful and often interchangeable. However, the joining operation is avoided sometimes due to its resulting large intermediate table size. In such cases, using an inner query is much more efficient. But, choosing the right option depends on the situation.

  • Use Joins when you need to combine rows from multiple tables directly.
  • Use Subqueries when you need to break down a problem into steps or filter the data based on calculated values.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *