Stepping through Sub-Queries in Oracle (Page 1 of 5 )
This is the first article in a series concentrating on working with sub-queries in Oracle. Sub-queries really have tremendous depth. In this series I will show you several scenarios where they can be efficiently used to retrieve information from Oracle databases.
As this is the first in series, I will be concentrating on the following topics:
- How to work with a “dual” table.
- How to analyze and identify the steps needed to deal with a sub-query.
- How to frame queries for each of the identified steps.
- How to combine all the framed queries and design a single command to retrieve the final output.
A primer on the “dual” table in Oracle
This section mainly explains the “dual” table in Oracle. I shall use this table in a creative manner wherever required in this article as well as upcoming articles. If you are already familiar with the "dual” table, feel free to skip to the next section.
What is a “dual” table? It is a simple table which is created/installed automatically during the installation of the Oracle database. To understand it, let us consider the following SELECT statement:
SELECT 123 FROM dual;
The above statement simply returns 123. Let us work with another statement:
SQL> SELECT 10,20,'Jagadish Chatarji',3400 FROM dual;
10 20 'JAGADISHCHATARJI' 3400
----- ----- ------------------ ----------------------
10 20 Jagadish Chatarji 3400
1 rows selected
This returns any constant values you provide as columns. So “dual” is just a convenience table. It is simply a one column and one row table that exists as part of SYS user. You can use the DESC command to display the structure of a “dual” table as follows:
DESC dual;
The above statement returns the following output:
Name | Null | Type |
---------------------- | ------------------------- | ------------------------ |
DUMMY | | VARCHAR2(1) |
You can observe that there exists only one column named “dummy” in the "dual" table. Similarly, you can even display all the rows in the "dual" table as follows:
SQL> SELECT * FROM dual;
DUMMY
-----
X
1 rows selected
From the above, you can observe that there exists only one row with a dummy value of "x."
You don't really need to use the “dual” table at all. It is only used when you want to add/include any constant values in your queries. You can even do calculations as follows:
SQL> SELECT 12 * 13 + 14 FROM dual;
12*13+14
----------------------
170
1 rows selected
Next: The simplest sub-query in Oracle >>
More Oracle Articles
More By Jagadish Chatarji