Having read today's topic โSQL.Entertaining tasks, โ I recalled that I had long wanted to recommend an excellent book for the advanced level of Oracle SQL from our excellent Oracle specialist, Alex Reprintsev -โ The Power of Oracle SQL โ. Not only is it extremely useful in itself for those who want to know Oracle SQL at a high level, it is also free ! In addition, there are versions in both Russian and English.
And to seed a couple of examples of tasks from it:
Connected components
there is an undirected (non-directional) graph defined by a list of edges and
required to get connected components.
For the data in the table below:
createtable edge(x1, x2) asselect10,20from dual union all select50,40from dual union all select20,30from dual union all select20,40from dual union all select60,70from dual union all select80,60from dual union all select20,90from dual;
The following result is expected (the numbering order of the components is not critical):
Now we consider the problem on a directed (directed) graph.
There is a table with dependencies between objects that does not contain cyclic
dependencies. However, more than one path may exist between pairs of vertices, therefore
Such a structure cannot be called a tree.
createtable d(name, referenced_name) as (selectnull, 'a'from dual union all selectnull, 'd'from dual union all select'a', 'b'from dual union all select'd', 'b'from dual union all select'b', 'e'from dual union all select'b', 'c'from dual union all select'e', 'c'from dual);
It is necessary to go around all the objects in the minimum number of steps, while at each step
You can bypass only those objects for which all dependent objects are bypassed. That is, on
the first step bypasses objects that do not have dependencies; in the second step, those that depend
from the objects of the first step and so on. In other words, depth dependencies are numbered.
Covering ranges
Suppose there is a table of the following form:
createtable t_range(a, b) as (select1, 15from dual union all select3, 17from dual union all select6, 19from dual union all select10, 21from dual union all select17, 26from dual union all select18, 29from dual union all select20, 32from dual union all select24, 35from dual union all select28, 45from dual union all select30, 49from dual);
b> a for each pair a, b;a unique
It is necessary to get the segments (1:15), (17:26), (28:45), that is, we start from the line with
minimum a , and take the next line such that for it a is greater than b from the current line and so
Further.
Top paths
For a table with a list of directories in the file system, output only those that do not have
subdirectory.
createtable t_path(path) asselect'/tmp/cat/'from dual union all select'/tmp/cata/'from dual union all select'/tmp/catb/'from dual union all select'/tmp/catb/catx/'from dual union all select'/usr/local/'from dual union all select'/usr/local/lib/liba/'from dual union all select'/usr/local/lib/libx/'from dual union all select'/var/cache/'from dual union all select'/var/cache/'||'xyz'||rownum||'/'from dual connectbylevel <= 1e6;