We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20
Become an SQL Master
With 1 Simple Trick
Use CTEs.
Jump to page 15 if you
are impatient to see the final example. A Common Table Expression, is a temporary result set that you can reference within another SELECT, INSERT, UPDATE, or DELETE statement.
Think of it like creating a temporary table
which you can then query as part of your overall SQL statement.
They are used to simplify complex queries,
breaking them down into simpler, more readable components.
Let’s see an example!
Example #1 We’ll use the famous ‘employees’ table for the example.
Each employee has an id, a name, a
manager_id and a salary.
(I didn’t use FKs or a sequence to simplify matters)
We’ll insert data to generate the following org chart.
John Smith
Sara Micha Emma Rob Don
Select * from employees
*You can follow along using sqliteonline.com!
Let’s start.
You need to select all the
employees in big teams (>5 employees in the team).
Try it out! Option #1 - Using Subquery
Option #2 - Using CTE
3 main CTE advantages: 1. Readability and Maintainability: Instead of having complex and nested subqueries, you can create a CTE at the beginning of your query. This makes the logic of your query easier to understand.
2. Reusable within a Single Query: Once a
CTE is defined, it can be referred to multiple times within the same query.
3. Recursive Queries: Unlike subqueries,
CTEs can be used to execute recursive queries, which are queries that refer to themselves. This is extremely useful for dealing with hierarchical or tree-structured data Try solving this without CTE:
Find all managers of big & well paid
teams:
Managers who manage more than
five employees and whose teams have an average salary that is greater than the overall average salary. Still not impressed?
We are getting to the BEST part.
For the final example, let’s create a table
with all England & Belgium Monarchs (Kings and Queens).
Trust me, this serves a point.
Example #2 - Monarchs table
…. Solve this!
Who is the 3rd monarch in each
country?
This should be the result:
Take a minute to try it out!
Before I reveal the answer - it is based on a real query we have.
As an AgTech company, we store
information about fields. Each field, is planted multiple times (at least once a year). Each such instance, is called a cycle.
We need to select the latest cycle for
each field. The concept was the same, I used ‘monarchs’ to simplify. Behold The SQL
Magic CTE + RANK + Partition Explanation
● The RANK() function assigns a rank to each
monarch based on their ascending order of ascending to the throne.
● The PARTITION BY clause groups the
monarchs based on their country, allowing the ranking to be done separately for each country.
Together, they determine the order and ranking
of the monarchs within each country, helping us find the third monarch in both. Elizabeth + Albert = <3 Let’s say you want the PREVIOUS monarch in each country (Elizabeth and Albert).