Updated April 12, 2023
Introduction to MariaDB with
MariaDB allows to run subquery expressions; most of the time, the query having a temporary table that is only available for a specified duration of a query. With keyword is used for common table expression. With keyword is help full to run the subquery expression in the statement. We will use common table expressions for recursive common expression data structure and non-recursive common expression data structure with clauses. In recursive common expression, we can execute the query as per the sequence defined by the user. In the non-recursive common table expression sequence of the query, it depends on the other terms. We can use two clauses at a time that is with and cycle clause as per the requirement of the user.
Syntax
with recursive table reference [(colm 1 colm 2)] as (
select……..
)
[cycle cycle colm list restrict ]
Select….
Explanation
In the above syntax, we use a keyword followed by a table reference name with all column name lists with respective referenced table; after that, we use a select clause to execute the subquery in the statement. The cycle keyword is used to avoid the infinite loops from the statement. MariaDB supports nonstandard grammar.
When we use cycle ………restrict so there is no difference between union all or union distinct that is union all means all rows without cycles and union distinct all rows should be different.
How with statement works in MariaDB?
Let’s see how with works in MariaDB as follows.
Basically, with keyword refers to the common table expression and common table expression and clause is used to execute subquery, and there are two types of common table expression as follows.
- Recursive CTE
The common table expression allows the query to reference itself. A recursive common table expression will repeatedly execute a subquery in the statement, or we can say it will execute a subset of data until it obtained the correct result. Mainly recursive common table expression helps handle hierarchical or tree data structures.
- Non-Recursive CTE
Basically, non-recursive common table expressions execute the local query, in which we can refer to the other places as per user requirement. Nonrecursive CTE means it does not have any sequence; it depends on any other term in the sequence.
Example of MariaDB with
Let’s see the different examples with clauses as follows.
First, we need to create three different tables to implement them with the clause; now, we create tables by using the create table statement as follows.
create table math (no_1 int(20), no_2 int(20), no_3 int(20));
Explanation
By using the above syntax, we created table name math with different attributes such as no_1, no_2, and no_3, as shown in the above statement. After that, we insert some records into the math by using insert into statements. The final output of the show databases queries we illustrate by using the following snapshot.
Similarly, we created a second table by using the create table statement as follows.
create table math1 (no_a int(20), no_b int(20), no_c int(20));
Explanation
By using the above syntax, we created table name math1 with different attributes such as no_a, no_b, and no_c, as shown in the above statement. After that, we insert some records into math1 by using insert into statement. The final output of the show databases queries we illustrate by using the following snapshot.
Now create one more table by using the same process as follows.
create table m (no_1 int(20), no_2 int(20), no_3 int(20));
Explanation
By using the above syntax, we created table name m with different attributes, as shown in the above statement. After that, we insert some records into them by using insert into statement. The final output of the show databases queries we illustrate by using the following snapshot.
Now we have three different tables now used with clauses to execute subquery as follows.
select math.no_1, math.no_2 from math, math1
where math.no_1 > math1.no_c
and math1.no_c in(with m as (select * from math where math.no_1 > 5)
select math1.no_c from math1, m where math1.no_c = m.no_1 );
Explanation
In the above example, we have three different tables, such as math, math1, and m, with different conditions. In this example, we also used select, where, and with a clause to satisfy the condition for subquery as shown in the above statement. Here we have two subqueries that we need to execute by using with clause. The final output of the show databases queries we illustrate by using the following snapshot.
Now let’s see a recursive example as follows.
We have a table name as BR, and it has different attributes such as origin and end and this we created by using a create table statement.
WITH RECURSIVE bus_dst as (
SELECT origin as end FROM BR WHERE origin='New York'
UNION
SELECT BR.end FROM BR, bus_dst WHERE bus_dst.end= BR.origin
)
SELECT * FROM bus_dst;
Explanation
In the above example, we use clause and recursive keyword to execute the subquery as shown above. In this example, there are two subqueries see in the above statement, and we need to execute recursively. First is calculated generated result by query then it starts from Mumbai now it added origin cities. The next part of this example is that recursive it selects some cities from the first subquery union with the second subquery; finally, it computes the path for query execution, so in this way, execution of the above statement will be executed. The final output of the show databases queries we illustrate by using the following snapshot.
Similarly, we implement non-recursive common table expressions as per the requirement of the user. We can also use cycle clauses with different options.
Conclusion
We hope from this article you have understood about MariaDB. From this article, we have learned the basic syntax of MariaDB with, as well as common table expressions, and we also see different examples MariaDB with. From this article, we learned how and when we use MariaDB with.
Recommended Articles
We hope that this EDUCBA information on “MariaDB with” was beneficial to you. You can view EDUCBA’s recommended articles for more information.