in SQL Server is possible to execute a SELECT, without reference to a table; something like:
Select 1.2 +3, 'my dummy string'
As Oracle does not allow a SELECT without a FROM, I use the dual table for this type of operation; something like:
Select 1,2+3, 'my dummy string' FROM DUAL
There is a better way of doing this type of query? it is good practice to use the dual table?
don't forget that most of the times you don't actually need to use SELECT.
Instead of:
you can use
No, in
Oracle
there is noSELECT
withoutFROM
.Using the
dual
table is a good practice.dual
is an in-memory table. If you don't selectDUMMY
from it, it uses a special access path (FAST DUAL
) which requires noI/O
.Once upon a time,
dual
had two records (hence the name) and was intended to serve as a dummy recordset to duplicate records being joined with.Now it has but one record, but you can still generate an arbitrary number of rows with it:
MySQL
also supportsdual
(as well as the fromless syntax).Actually, SQL Server's implementation is non-standard. The SQL-92 Standard (Section 7.9) requires a FROM clause in a SELECT statement. DUAL is Oracle's way of providing a table to select from to get a scalar row.
Yes, the dual table is usually used for this exact purpose. It's pretty standard in Oracle when you have no table to select from.
Yes, the dual table is the usual way to do this in Oracle. As a matter of fact, it was introduced just for this.
The main advantage of DUAL is that the optimizer in Oracle knows it is special, so queries using it can be faster than if you used a single-row table you made yourself. Other than that, there's nothing special about it.
I think you gotta use dual. it is used when you need to run SQL that does not have a table name. I can't say I use it much other than in SQL scripts to echo out the date something is ran or something stupid like that.