Suppose I have the following table
| id | category_name | parent_id |
|---|---|---|
| 1 | movies | 0 |
| 2 | technology | 0 |
| 3 | sci-fiction | 1 |
| 4 | romantic | 1 |
| 5 | documentries | 0 |
| 6 | space | 3 |
| 7 | heros | 3 |
| 8 | saturn | 6 |
| 9 | mars | 6 |
| 10 | black holes | 6 |
What I want is order these items in a way that every parent followed by his childs like this:
| id | category_name | parent_id |
|---|---|---|
| 1 | movies | 0 |
| 3 | sci-fiction | 1 |
| 6 | space | 3 |
| 8 | saturn | 6 |
| 9 | mars | 6 |
| 7 | heros | 3 |
| 4 | romantic | 1 |
| 2 | technology | 0 |
| 5 | documentries | 0 |
| 10 | black holes | 6 |
How to write a query to perform action like this?

Recursive CTE expressions are supported in MariaDB 10.2. You tagged your question mariadb-10.1 so I think you'll have to upgrade if you want to stay with MariaDB.
If you can store your data differently, you could use one of the alternative ways of storing hierarchical data that became popular for MySQL or MariaDB before they supported recursive CTE expressions. For some suggestions for this, see: