Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL DUAL table

What is DUAL table?

The DUAL is special one row, one column table present by default in all Oracle databases. The owner of DUAL is SYS (SYS owns the data dictionary, therefore DUAL is part of the data dictionary.) but DUAL can be accessed by every user. The table has a single VARCHAR2(1) column called DUMMY that has a value of 'X'. MySQL allows DUAL to be specified as a table in queries that do not need data from any tables. In SQL Server DUAL table does not exist, but you could create one.
The DUAL table was created by Charles Weiss of Oracle corporation to provide a table for joining in internal views.

See the following commands :

The following command displays the structure of DUAL table :

DESC DUAL;

Output:

Name                 Null?   Type
--------------------------- ------
DUMMY                        VARCHAR2(1)

The following command displays the content of the DUAL table :

SELECT * FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Displays the content of the DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Displays the content of the DUAL table.

Output:

DUMMY
----------
X

The following command displays the number of rows of DUAL table :

SELECT COUNT(*) FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Displays the number of rows of DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Displays the number of rows of DUAL table.

Output:

  COUNT(*)
----------
         1

The following command displays the string value from the DUAL table :

SELECT 'ABCDEF12345' FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Displays the string value from the DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Displays the string value from the DUAL table.

Output:

'ABCDEF1234
-----------
ABCDEF12345

The following command displays the numeric value from the DUAL table :

SELECT 123792.52 FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Displays the numeric  value from the DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Displays the numeric  value from the DUAL table.

Output:

 123792.52
----------
 123792.52

The following command tries to delete all rows from the DUAL table :

DELETE FROM DUAL;

Output:

DELETE FROM DUAL
            *
ERROR at line 1:
ORA-01031: insufficient privileges

The following command tries to remove all rows from the DUAL table :

TRUNCATE TABLE DUAL;

Note : The DELETE command is used to remove rows from a table. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. TRUNCATE removes all rows from a table. The operation cannot be rolled back.

Output:

TRUNCATE TABLE DUAL
               *
ERROR at line 1:
ORA-00942: table or view does not exist

The following command select two rows from dual :

SELECT dummy FROM DUAL 
UNION ALL 
SELECT dummy FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Select two rows from DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Select two rows from DUAL table.

Output

DUMMY
----------
X
X

Example - 1

You can also check the system date from the DUAL table using the following statement :

SELECT sysdate FROM DUAL ;

Relational Algebra Expression:

Relational Algebra Expression: Select two rows from DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Select two rows from DUAL table.

Output:

SYSDATE
---------
11-DEC-10

Example - 2

You can also check the arithmetic calculation from the DUAL table using the following statement :

SELECT 15+10-5*5/5 FROM DUAL;

Relational Algebra Expression:

Relational Algebra Expression: Select two rows from DUAL table.

Relational Algebra Tree:

Relational Algebra Tree: Select two rows from DUAL table.

Output:

15+10-5*5/5
-----------
         20

Example - 3

Following code display the numbers 1..10 from DUAL :

SELECT level 
FROM DUAL 
CONNECT BY level <=10;

Output:

     LEVEL
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10

Example - 4

In the following code, DUAL involves the use of decode with NULL.

SELECT decode(null,null,1,0) 
FROM DUAL;

Output:

DECODE(NULL,NULL,1,0)
---------------------
                    1

DUAL table : Oracle vs MySQL

We have already learned that DUAL is a special one row one column table. For Oracle, it is useful because Oracle doesn't allow statements like :

SELECT 15+10-5*5/5;

Relational Algebra Expression:

Relational Algebra Expression: Oracle vs MySQL.

Relational Algebra Tree:

Relational Algebra Tree: Oracle vs MySQL.

Output:

SELECT 15+10-5*5/5
                 *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

But the following command will execute (see the output of the previous example) :

SELECT 15+10-5*5/5 FROM DUAL;

In case of MySQL the following command will execute :

SELECT 15+10-5*5/5;

Output:

mysql no dual result

The following table shows the uses of dummy table in standard DBMS.

DBMS Dummy-table concept
MSSQL No dummy-table concept.
MySQL No dummy-table concept.
Oracle Dummy-table : DUAL.
Informix Since version 11.10, a dummy table has been included : sysmaster:sysdual
PostgreSQL No dummy-table concept.
DB2 Dummy-table : SYSIBM.SYSDUMMY1

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: SQL ordering output by column number with group by
Next: SQL Injection



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3