The Wayback Machine - https://web.archive.org/web/20240810005053/https://www.geeksforgeeks.org/dual-table-in-sql/
Open In App

DUAL table in SQL

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In SQL databases, the DUAL table serves a unique purpose, primarily found in Oracle and related systems. It’s a special one-row, one-column table that doesn’t actually store any data but is essential for performing certain types of queries and calculations.

What is the DUAL table used for?

  • Testing Expressions: Developers often use the DUAL table to test SQL expressions without querying a real table. For example, checking the result of a function or evaluating an arithmetic expression can be done using DUAL.
  • Generating Constants: It’s handy for generating constant values like current timestamps (SELECT SYSDATE FROM DUAL;) or other system-specific values (SELECT USER FROM DUAL;).
  • Simulating Data: In scenarios where you need to generate a single row of dummy data or simulate a result set for testing purposes, DUAL can be used effectively.

Why is it called DUAL?

The name “DUAL” signifies its dual-purpose nature: it provides a single dummy row and column for SQL operations that require a result set but don’t need to access any actual table data.

Examples of Using DUAL in Oracle

1. Example

SELECT SYSDATE;

Output:

ORA-00923: FROM keyword not found where expected

2. Example

SELECT 'GeeksforGeeks';

Output:

GeeksforGeeks

Details about the DUAL table in Oracle

The DUAL table is automatically created by Oracle Database along with the data dictionary. It resides in the schema of the user SYS but is accessible by the name DUAL to all users. It consists of one column, DUMMY, defined as VARCHAR2(1), and contains one row with a value ‘X‘.

Further Examples in Oracle:

1. Example

SELECT * FROM DUAL;

Output:

X 

Selecting from the DUAL table is useful for computing constant expressions with the SELECT statement. Because DUAL has only one row, the constant is returned only once.

2. Example

SELECT 'GeeksforGeeks' 
AS NAME FROM DUAL;

Output:

+---------------+
| NAME |
+---------------+
| GeeksforGeeks |
+---------------+

3. Example

SELECT 2+2 FROM DUAL;

Output :

4 

DUAL in Other Databases

Several other databases, including MS SQL Server, MySQL, PostgreSQL, and SQLite, do not have a DUAL table. They allow the omission of the FROM clause in certain contexts:

MySQL Example:

In MySQL, you don’t need to use the ‘FROM DUAL' clause if no tables are referenced. However, you can still use DUAL as a dummy table name if needed.

For example:

SELECT 1+1 FROM dual;
SELECT 'GeeksforGeeks' FROM dual;
SELECT NOW() FROM dual;
SELECT CONCAT('fname', ' ', 'lname') FROM dual;

These queries are equivalent to:

SELECT 1+1;
SELECT 'GeeksforGeeks';
SELECT NOW();
SELECT CONCAT('fname', ' ', 'lname');

Note:

In MySQL, using SELECT * FROM DUAL; will result in an error: “No tables used.” This is similar to running SELECT *; without specifying a table.

Changes in Oracle 23c

Starting with Oracle 23c, the requirement to use FROM DUAL has been relaxed for queries that do not reference any tables explicitly. This update simplifies syntax in Oracle SQL

-- Oracle 23c and later: Omitting FROM DUAL
SELECT SYSDATE;
-- Output: Current date

SELECT 2+2;
-- Output: 4

SELECT 'GeeksforGeeks';
-- Output: GeeksforGeeks

DUAL in Other Databases

Unlike Oracle, several other databases like MS SQL Server, MySQL, PostgreSQL, and SQLite do not have a DUAL table. They allow the omission of the FROM clause in certain contexts:

-- MySQL Example: No need for FROM DUAL
SELECT 1+1;
-- Output: 2

SELECT 'GeeksforGeeks';
-- Output: GeeksforGeeks

SELECT NOW();
-- Output: Current timestamp

SELECT CONCAT('fname', ' ', 'lname');
-- Output: Combined string

Conclusion

The DUAL table in SQL, particularly in Oracle, simplifies querying for constant values and testing expressions without the need for real data tables. Its simplicity and utility make it a unique feature in Oracle databases, though other databases have alternative ways to achieve similar functionality without a dedicated DUAL table.

FAQs on DUAL table in SQL

What is the purpose of the DUAL table in SQL?

The DUAL table is used for testing expressions, generating constants, and simulating data without querying actual tables. It’s primarily found in Oracle and related systems.

Why do we use DUAL in SQL queries?

DUAL is used because it provides a single dummy row and column, allowing developers to perform SQL operations that require a result set but don’t need to access any actual table data.

What happens if you try to use SELECT * FROM DUAL; in MySQL?

MySQL will return an error saying “No tables used” because it doesn’t require the DUAL table for such queries. It’s similar to running SELECT *; without specifying a table.



Similar Reads

How to Select All Records from One Table That Do Not Exist in Another Table in SQL?
We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries. In this let us see How to select All Records from One Table That Do Not Exist in Another Table step-by-step. Creating a Database Use the below command to create a database named Geeks
2 min read
SQL Query to Filter a Table using Another Table
In this article, we will see, how to filter a table using another table. We can perform the function by using a subquery in place of the condition in WHERE Clause. A query inside another query is called subquery. It can also be called a nested query. One SQL code can have one or more than one nested query. Syntax: SELECT * FROM table_name WHERE col
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL vs NO SQL vs NEW SQL
SQL stands for Structured Query Language. Which is based on relational algebra and schema is fixed in this which means data is stored in the form of columns and tables. SQL follows ACID properties which means Atomicity, Consistency, Isolation, and Durability are maintained. There are three types of languages present in SQL : Data Definition Languag
2 min read
How to Update a Table Data From Another Table in SQLite
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queries and operations on the database. In this article
3 min read
How to Select Rows from a Table that are Not in Another Table?
In MySQL, the ability to select rows from one table that do not exist in another is crucial for comparing and managing data across multiple tables. This article explores the methods to perform such a selection, providing insights into the main concepts, syntax, and practical examples. Understanding how to identify and retrieve rows that are absent
3 min read
Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another table, using an ID match. ID match ensures that the
3 min read
Removing Duplicate Rows (Based on Values from Multiple Columns) From SQL Table
In SQL, some rows contain duplicate entries in multiple columns(>1). For deleting such rows, we need to use the DELETE keyword along with self-joining the table with itself. The same is illustrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to crea
3 min read
How to Get the names of the table in SQL
The syntax provided in this article works only for SQL Server and MySQL. If you want to know how many tables are present in your database and the details of the table like TABLE_SCHEMA, TABLE_TYPE and all. Syntax (When we have only single database): Select * from schema_name.table_name Syntax (When we have multiple databases): Select * from databas
1 min read
Article Tags :
three90RightbarBannerImg