Adventures in Machine Learning

Simplify Complex Database Queries with SQL WITH Clause

Structured Query Language (SQL) is a programming language designed for accessing, manipulating, and managing relational databases. It has become a core aspect of several applications that require data management.

SQL queries form the core of any data retrieval and manipulation process, and they can vary from simple to complex forms. One of the techniques used to simplify complex queries is the “Divide and Conquer” technique, which involves breaking down complex queries into smaller subqueries.

The SQL WITH clause is an essential tool used in the “Divide and Conquer” technique to simplify and organize complex queries. In this article, we’ll cover the basics of SQL WITH clause, its definitions and usages, and provide an example of how to use it in a simple query.

Simple vs. Complex Queries

Database queries can be simple or complex, depending on the nature of the data retrieval process.

Simple queries require minimal filtering and manipulate basic data types, while complex queries involve advanced filtering processes that may require subqueries. In complex queries, it is vital to break down the query into smaller subqueries.

The “Divide and Conquer” Technique

The “Divide and Conquer” technique is an ancient strategy used in several areas of life, including programming. It involves breaking down complex tasks into smaller and more manageable sub-tasks that can be solved individually and combined for the overall target.

In programming, the “Divide and Conquer” technique is employed to solve complex programming tasks by dividing them into smaller sub-tasks that are easier to manage, and the SQL WITH clause is a dependable tool utilized as a virtual table to simplify complex queries.

Definition and Usage of SQL WITH Clause

The SQL WITH clause, also known as Common Table Expressions (CTEs), is a temporary, named result set that is defined within a query. A CTE is a subquery that can be referenced multiple times within the same query or in subsequent queries.

The WITH clause can be used to simplify complex queries and improve readability. It is also useful in recursive queries, where a query is repeatedly executed using repeatedly-changing input.

Basic Usage of SQL WITH Clause

To better understand how to use SQL WITH clause, we’ll demonstrate a simple SQL query example using a CTE. Example Query: Obtaining List of Countries with Export More than $1M in 2021

Consider the following table of country exports in millions, spanning from the year 2019 to 2021.

| Country | 2019 Export | 2020 Export | 2021 Export |


| USA | 318 | 275 | 321 |

| Canada | 52 | 60 | 49 |

| Mexico | 461 | 381 | 372 |

| Japan | 684 | 699 | 727 |

| Germany | 1466 | 1214 | 1554 |

| China | 2499 | 1919 | 2487 |

Using this table, let’s imagine that we need to obtain a list of countries whose export in the year 2021 was more than $1M. We can use the following SQL query:

WITH export_2021 AS (SELECT Country, “2021 Export” as TotalExport FROM exports)

SELECT Country FROM export_2021 WHERE TotalExport > 1000;

This query uses the WITH clause to define a virtual table named “export_2021,” which selects the two relevant columns from the “exports” table.

In the following line, we use the “SELECT” statement to retrieve all countries from the “export_2021” table whose export “TotalExport” in the year 2021 was more than $1M.

Referencing CTEs Like Regular Database Tables

It is essential to note that CTEs are referenced like regular database tables, and they can be utilized in subsequent queries. This feature makes it possible to simplify very complex queries by breaking them into manageable subqueries, which can be repeatedly referenced throughout the entire query.


In summary, SQL WITH clauses are powerful tools that simplify the process of breaking down complex queries into smaller subqueries that are more manageable. Using the WITH clause enhances the readability and simplicity of SQL queries, and its usage is prevalent in complex queries that require subqueries.

It is essential to note that CTEs are referenced like regular database tables, and they can be referred to in subsequent queries. With this knowledge and understanding of SQL WITH clause, database programmers can build faster, safer, simpler, and more efficient databases.

3) Using Multiple CTEs in One Query

As we’ve seen from the previous section, SQL WITH clauses are a powerful tool for breaking down complex queries into smaller, more manageable subqueries. However, sometimes a query may require multiple subqueries.

In such cases, it’s possible to use multiple CTEs in one query.

Breaking Long Queries into Simple Queries with CTEs

One of the benefits of using multiple CTEs in one query is the ability to break down long and complex queries into more manageable subqueries. This approach simplifies the query and improves readability while making it easier to maintain.

Consider a scenario where we need to find all the import-export companies operating in the local market and their current balance. The following query demonstrates how multiple CTEs can be used to obtain this data:

WITH import_companies AS (


FROM companies

WHERE type = ‘importer’


export_companies AS (


FROM companies

WHERE type = ‘exporter’


company_balances AS (

SELECT company_id, SUM(amount) AS balance

FROM transactions

GROUP BY company_id


SELECT AS company_name,

company_balances.balance AS balance, AS country

FROM import_companies

INNER JOIN export_companies ON =

INNER JOIN company_balances ON = company_balances.company_id;

This query utilizes three CTEs; “import_companies,” which selects all import companies, “export_companies,” which selects all export companies, and “company_balances,” which computes a company’s total balance from their transactions. We then join these three CTEs using the “JOIN” statement to produce the final report, which contains the name of the import-export company, their current balance, and their country of operation.

Example Query: Creating Report of Local Import-Export Companies and Their Balance

Let’s assume that we have a table “companies” that contains information on all import and export companies in the local market. We also have a table “transactions” that contains all the financial transactions made by these companies.

Each transaction contains the transaction amount, date, and company ID. Using this data, we can create a report that shows all the import-export companies in the local market and their current balance.



company_balances AS (

SELECT company_id, SUM(amount) AS balance

FROM transactions

GROUP BY company_id


SELECT AS company_name,

company_balances.balance AS balance, AS country

FROM companies

INNER JOIN company_balances ON = company_balances.company_id

WHERE companies.type IN (‘importer’, ‘exporter’);

This query uses one CTE called “company_balances,” which computes the total balance for each company. It then selects the company name, balance, and country from the “companies” table and joins it with the “company_balances” table using the “INNER JOIN” statement.

Finally, we filter the results to show only the import-export companies using the “WHERE” statement.

4) Chaining Multiple CTEs in One Query

Chaining multiple CTEs in one query involves using the result set of one CTE as the input for another CTE. This technique allows us to build more complex queries that require multiple subqueries.

Example Query: Obtaining All Export Operations for Local Companies with Increased Exports in 2021

Suppose we need to obtain all export operations for local companies with increased exports in 2021. To achieve this, we can use multiple CTEs and chain them together as follows:

WITH company_exports AS (

SELECT company_id, SUM(amount) AS total_exports

FROM transactions

WHERE type = ‘export’ AND year = 2021

GROUP BY company_id


increased_exports AS (

SELECT company_id

FROM company_exports

WHERE total_exports > 5000


company_operations AS (


FROM operations

WHERE type = ‘export’


local_companies AS (


FROM companies

WHERE country = ‘Local’


local_export_operations AS (

SELECT company_operations.*

FROM local_companies

INNER JOIN company_operations ON = company_operations.company_id


local_export_increased AS (

SELECT local_export_operations.*

FROM local_export_operations

INNER JOIN increased_exports ON local_export_operations.company_id = increased_exports.company_id



FROM local_export_increased;

This query uses five CTEs. The first CTE, “company_exports,” selects all export transactions in 2021 and computes the total exports for each company. The second CTE, “increased_exports,” selects all companies with exports greater than $5000.

The third CTE, “company_operations,” selects all export operations. The fourth CTE, “local_companies,” selects all companies that operate locally.

The fifth CTE, “local_export_operations,” selects all export operations by local companies. Finally, we chain the CTEs “local_export_operations” and “increased_exports” together to create the final result set.

The query returns all export operations by local companies with increased exports in 2021.


In conclusion, the SQL WITH clause is a powerful tool that can simplify complex queries by breaking them down into smaller subqueries. This article has discussed the usage of multiple CTEs in one query, as well as chaining CTEs. Using these techniques makes it possible to solve even more complex queries with SQL.

By employing these methods, a programmer can create faster, simpler, and more efficient databases while maintaining the readability of the query.

5) Using Recursive Queries with the SQL WITH Clause

In SQL, Recursive queries are used to retrieve data that references itself in a hierarchical or tree-like structure. Recursive queries are an essential tool used in several applications, such as organization charts, bill of materials, and when dealing with data that forms a tree-like structure.

In SQL, recursive queries can be achieved using the SQL WITH clause. Do it in SQL: Recursive SQL Tree Traversal

A common example of recursive queries is performing tree traversals.

Consider a scenario where we have a table “employees” that contains hierarchical data for an organization’s employees. The table has columns “id,” “name,” “manager_id,” and “designation.” Each employee’s manager_id corresponds to the employee_id who is that employees manager in the organizations hierarchy.

Using a recursive query, we can create an organization chart that shows the hierarchy of the organization’s employees. The code snippet below demonstrates how recursive queries work:

WITH RECURSIVE subordinates AS (

SELECT id, name, designation, manager_id

FROM employees

WHERE id = 1 — starting employee id


SELECT,, employees.designation, employees.manager_id

FROM employees

JOIN subordinates ON employees.manager_id =



FROM subordinates;

This query starts with the employee who has the id of 1. Then, it ‘JOINS’ the subquery and references the employees whose manager_id is equal to the subquery’s id.

The recursive query keeps running and appending the result set until the query returns an empty set or the maximum recursion depth limit has been reached. The final result set returns all the subordinates beneath the starting employee that we the Interactive Recursive Queries Course

One can learn more about recursive queries and other SQL concepts using an Interactive Recursive Queries course.

This course targeted for individuals who are interested in learning more about SQL and data management. The course provides an interactive platform where users can learn and practice SQL with real-world examples.

The course enables users to learn about advanced SQL topics, including recursive queries and the SQL WITH clause.


Conclusion and Advantages of SQL WITH Clause

Benefits of Using SQL WITH Clause for Complex Queries

SQL WITH clauses are vital to simplifying complex queries and reducing the amount of code required to retrieve data from databases. The primary advantages of SQL WITH clauses include:


Organization: SQL WITH clauses help organize complex queries by breaking them down into logical and more easily understood sections. 2.

Reusability: SQL WITH clauses can be used as subqueries in other queries, reducing repetition and making queries more efficient. 3.

Reliability: SQL WITH clauses help avoid common syntax errors in queries by standardizing the subqueries. 4.

Debugging: By breaking a complex query into smaller subqueries, identifying errors and debugging queries is made easier. 5.

Performance: SQL WITH clauses can lead to faster query performance since intermediate datasets are cached.

Reminder to Increase SQL Skills with Recursive Queries Course

In conclusion, SQL WITH clauses are an indispensable tool in simplifying complex queries and improving code maintainability. It is essential to continue to grow one’s skills in SQL and take the opportunity to learn about advanced SQL topics such as recursive queries, which can add significant value to database management.

The Interactive Recursive Queries course presents an excellent opportunity for those looking to expand their SQL skills to learn more about SQL WITH clauses and recursive queries. The SQL WITH clause is a powerful tool used to simplify complex database queries by breaking them down into smaller sub-queries.

This approach improves query readability, efficiency, and accuracy. By using multiple CTEs, developers can further simplify complex queries and reduce query maintenance time.

Recursive queries, when combined with the SQL WITH clause, enable the retrieval of tree-like or hierarchical data, making it useful for organization charts and bill of materials. Finally, courses such as the Interactive Recursive Queries course offer individuals an opportunity to enhance SQL skills and learn advanced SQL concepts such as recursive queries.

By investing time in mastering SQL and exploring the possibilities offered by SQL WITH clauses, developers can achieve new levels of database management proficiency.

Popular Posts