13 Jun 2024 Gustavo du Mortier SQL CTEs: A Complete Overview of Common Table Expressions This article provides a detailed guide to everything you need to know about common table expressions (CTEs), from the basic definition to the most complex recursive queries. If the database you use supports CTEs, here’s everything you need to get the most out of it. Common table expressions – also called CTEs, WITH clauses, or recursive queries (although this last name is actually a specific application) – are a relatively new feature of SQL. Read more 23 May 2023 Nicole Darnley How to Write a Recursive CTE in SQL Server This guide will help you understand how to write and use recursive CTEs in SQL Server. Recursive CTEs are powerful tools for handling hierarchical data, and we'll break down their syntax and applications step by step. SQL Server offers a lot of powerful tools for working with data, including Common Table Expressions (CTEs). A CTE is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement, often referred to as a CTE query. Read more 19 Oct 2021 Tihomir Babic What Is a Recursive CTE in SQL? The article that’ll show you practical examples of using recursive CTEs in SQL. If you’ve heard about SQL’s recursive CTEs but never used them, this article is for you. It’s also for you if you never get tired of recursive CTE examples. Before we dig into recursion, I’ll remind you what CTEs are and what their syntax is. Then I’ll do the same for recursive CTEs. After that, I’ll show you how recursive CTEs work in three examples. Read more 22 Sep 2021 Tihomir Babic The 7 Best Articles about the SQL Recursive Query Do you want to learn about recursive queries but don’t know where to start? Here’s our selection of seven articles that should help you with diving into the recursive queries world. I won’t lie to you. Learning and understanding recursive queries can be difficult. But difficult things are usually worth trying. And by learning recursive queries, you’re making sure you know how to work with this very powerful tool. Read more 10 Jun 2021 Tihomir Babic How to Draw Pictures with Recursive SQL Queries Did you know that SQL can be used for fun? You can even ‘draw’ in SQL using recursive queries! Find out how in this article. As an SQL user, you already know this language is great for data analysis and data manipulation in general. This is something people get very serious about. But you can also have fun with it by drawing ASCII-art pictures using the SQL code. To do that, you have to know how recursive SQL queries work. Read more 20 Apr 2021 Tihomir Babic Hierarchical Data and How to Query It in SQL How do you recognize hierarchical data, and how do you query it? Two common SQL questions are about to be answered. Querying hierarchical data is a rather common situation if you’re working with SQL and relational databases. Not that hierarchical data is something mystical and rare; on the contrary, it’s everywhere. So why is hierarchical data so challenging when it comes to relational databases? Mainly, the difficulties occur in translating hierarchical data to relational database principles. Read more 25 Nov 2020 Tihomir Babic How to Find All Employees Under Each Manager in SQL Have you ever been tasked with building an organizational chart – i.e. finding the employees under each manager in a company? In SQL, you can do that easier than you think! This article will show you how to get the answer using a recursive query. In the business world, it’s a very common task to list all the subordinate employees for a given manager. Is doing this hard enough to warrant a whole article? Read more 28 Oct 2020 Tihomir Babic What are SQL CTE Best Practices? Are you wondering what the best practices for using common table expressions are? This article will help you learn when to use a CTE and how to write it. If you have heard about SQL CTEs, you have probably noticed they’re often mentioned together with subqueries. Sometimes, people think there’s no difference compared to the subqueries, and there’s often debate about whether a CTE or subquery should be used to get a particular result. Read more 23 Oct 2020 Kateryna Koidan How to Learn SQL Common Table Expressions (CTEs) Common table expressions are very useful for organizing, traversing, and improving the readability of long SQL queries. Moreover, recursive CTEs, which can reference themselves, solve problems that cannot be addressed with other queries. But how do you master common table expressions in SQL? In this article, I discuss the most effective strategies for learning CTEs and suggest how to address the common challenges of learning common table expressions on your own. Read more 28 Aug 2020 Adrian Więch How CTEs Work The SQL language offers a feature named Common Table Expressions, or CTEs. Also known as WITH clauses, CTEs are a fairly new addition to SQL. They help you break longer queries into smaller chunks, making your queries much easier to understand. Read the article to find out how to use CTEs and how they differ from traditional subqueries. Common Table Expressions (CTEs), which are essentially named subqueries, were first mentioned in the SQL standard between 1999 and 2000. Read more 20 Feb 2019 Michał Kołodziejski Get to Know the Power of SQL Recursive Queries Most commonly, the SQL queries we run on a database are quite simple. Well, that depends on your role, of course. Analysts in data warehouses retrieve completely different sorts of information using (very often) much more complicated queries than software engineers creating CRUD applications. However, sometimes it's simpler or more elegant to run a query that is a little bit more sophisticated without needing further data processing in the code. Read more 21 Dec 2017 Aldo Zelen How to Draw a Christmas Tree in SQL You can use SQL to manipulate all kinds of data, from huge analytical queries to brief single-purpose statements. But you can also use SQL just for fun, without any business requirements stifling your creativity. So, get out your jolly hat and prepare to sing O Christmas Tree as we create some quirky art with plain old SQL. Today, we’re going to generate some holiday-themed ASCII art, just for fun. That’s right. Read more 12 Dec 2017 Aldo Zelen Simplify SQL Code: Recursive Queries in DBMS Hey SQL users! Are you repeating the same query in every report? Are your queries getting too complicated? Use recursive queries to simplify SQL code! Too many SQL reports can lead to clutter on your desktop and in your head. And is it really necessary to code each of them separately? Ad-hoc queries can share much of the same SQL code with managerial reports and even regulatory reports. Suppose you’ve been writing basic SQL code for a while. Read more 16 Oct 2017 Aldo Zelen How to Organize SQL Queries with CTEs Common table expressions (CTEs) allow you to structure and organize SQL queries. Knowing how to organize SQL queries is a necessity when you begin to move deeper into SQL, so if you want to become an SQL master, you need to know CTEs. The SQL CTE has been part of standard SQL for some time now. CTEs – which are also called WITH statements – are available in all major RDBMS. Read more 10 Oct 2017 Aldo Zelen Long SQL Query vs. Recursive SQL Query Recursion is one of the central ideas in computer science. We can define it as a method for solving problems where the solution of the problem depends on solving a smaller instance of a problem. If this sounds complicated do not fret, in this article we will learn about recursion in SQL that you can practice and deepen at LearnSQL.com. Recursion is a way of solving hierarchical problems we find in data with common SQL. Read more 31 Jul 2017 Marek Pankowski How Recursive Common Table Expressions Work Recursive Common Table Expressions are immensely useful when you're querying hierarchical data. Let's explore what makes them work. Common Table Expressions (CTEs) are some of the most useful constructions in SQL. Their main purpose is improving query design, which makes queries easier to read. One of the reasons CTEs are so popular is that they let you divide longer queries into shorter subqueries. These are easier to read and edit. Read more 26 Nov 2016 Michał Kołodziejski Do it in SQL: Recursive SQL Tree Traversal In the previous article, I described how to use Common Table Expressions to find the shortest path in a directed graph. That example could be hard to follow, I admit. Let’s do something much more common, something that is implemented on almost every website – a menu. Instead of writing the code, we’ll take advantage of the SQL tree structure writing just one query. We’ll use CTEs for PostgreSQL and the hierarchical query clause for Oracle. Read more