In a recent labs release a new feature was introduced by Oracle, or actually two very related new features were introduced. The first new feature is Common Table Expressions (CTEs), which is also known as
WITH. The second feature is recursive CTEs, also known as
An example of
WITH non_root_users AS (SELECT User, Host FROM mysql.user WHERE User<>'root') SELECT Host FROM non_root_users WHERE User = ?
The non-CTE equivalent is this:
SELECT Host FROM (SELECT User, Host FROM mysql.user WHERE User<>'root') non_root_users WHERE User = ?
This makes it easier to understand the query, especially if there are many subqueries.
Besides using regular subqueries or CTEs you could also put the subquery in a view, but this requires more privileges. It is also difficult to change the views later on as other quieries might have started to use them.
But views are still very useful. You can make it easier for others to query data or you can use views to restrict access to certain rows.
So CTEs are basically views which are bound to a query. This makes it easier to write complex queries in a way that they are easy to understand. So don’t expect CTEs to replace views.
In the PostgreSQL world CTEs existed since version 8.4 (2009) and it is used a lot.
There are some cool things PostgreSQL allows you to do with CTEs and MySQL doesn’t:
test=# create table t1 (id serial, name varchar(100)); CREATE TABLE test=# insert into t1(name) values ('foo'),('bar'); INSERT 0 2 test=# with deleted_names as (delete from t1 where id = 2 returning name) test-# select name from deleted_names; name ------ bar (1 row)
The blog post has more details and examples about recursive CTEs, the second new feature.
One of the examples is generating a range of numbers.
If you’re familiar with PostgreSQL that will remind you of the
generate_series function. This function can be used to generate a series of intergers or timestamps. So I tried to make a stored procedure which together with the recursive CTE support would emulate
generate_series in MySQL, but no such luck as you can’t return a table from a stored fuction yet.
In the PostgreSQL world CTEs are also used to trick the optimizer but note that this depends on the specific CTE implementation, so don’t assume this trick will work in MySQL.
If you want to see the progress of MySQL and MariaDB on other modern SQL features check out this page.