Urgent.News

What's breaking now, across thousands of outlets.

Tech

# Subqueries & CTEs: Two Ways to Query Inside a Query

Sometimes one query needs the result of another query to work. SQL gives you two main tools for that: subqueries and CTEs (Common Table Expressions). They can often solve the same problem, but they read differently, and knowing when to reach for each makes your SQL a lot easier to write and debug. What Are Subqueries? A subquery is a query nested inside another query, wrapped in parentheses. It…

Subqueries and Common Table Expressions (CTEs) are two methods in SQL to query within a query. They can both solve similar problems, but they have distinct differences in how they appear and how they work. Subqueries are nested inside another query, enclosed in parentheses. They can return a single value, a list of values, or even an entire table-like result.

A subquery can appear in various parts of a query, such as the SELECT list, the FROM clause, or the WHERE clause. In contrast, a CTE is defined at the beginning of a query using the WITH clause, given a name, and then referenced later in the query, just like a regular table. It exists only for the duration of that specific query and disappears once the query finishes.

One key difference between subqueries and CTEs is their readability and reusability. While subqueries can get complex when nested deeply and may become hard to read, CTEs remain clear even when chained together with multiple steps. Additionally, subqueries cannot reference themselves, but CTEs can be recursive, allowing them to reference themselves for hierarchical data.

In terms of performance, modern databases typically optimize both subqueries and CTEs similarly, so the choice between them often comes down to readability and reusability rather than speed. When deciding which to use, consider the simplicity of the logic and whether it needs to be reused. Use subqueries for small, single-use queries.

Opt for CTEs when you need to name a step, reuse it, chain several steps together, or walk a hierarchy recursively. The easiest way to remember the difference is that a subquery is a query hidden inside another query, while a CTE is a query given a name up front and then used like a table below it. As queries become more complex and start nesting deeper than three levels, it's usually a sign to extract them into a CTE.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

From Drawing Locks at 4AM to Coding My First Website: My Journey as a Cybersecurity Student in Ecuador

I'm Fernando, 29, from Calderón, Quito, Ecuador. Two years ago I was drawing locks with pencil at 4AM for my YouTube channel Relatos Reales. Today I'm building websites at 4AM with AI.

  • Fernando, 29, cybersecurity student from Calderón, Quito, Ecuador
  • Builds LUMI clinic's landing page using HTML, CSS, Tailwind, JavaScript, React
  • Secures internships with GMS Seguridad, Netlife, NTT Data via portfolio

In Next.js 16, the return value of require.resolve becomes an integer and the code crashes

Summary I added one tool, which AI agents call from outside, to a service I develop at my own company. When this tool is called, one page selected from a PDF document stored in the service is…

  • Next.js 16 returns integer from require.resolve, causing TypeError
  • Issue stems from using process working directory as base for resolving
  • Fix by using working directory as base or adding turbopackIgnore annotation

More from Tuesday 29 September →