About SELECT statement without FROM clause(SQL)

[日本語]

This is about my experience and also a note that summarizes what I learned from workplace and self-study.

Previously, I was involved in unit test of SQL scripts for securities settlement infrastructure system in Japan. I am beginner engineer and I would like to share my experience here. Please read until the end and please leave a comment.

This time, I would like to share what I was worried about in the PostgreSQL SELECT statement.

As shown below, we recognized that ordinary SELECT statements are used together with the FROM clause.

SELECT *
FROM sample_table;

However, I encountered a SELECT statement that does not have a FROM clause as shown below.

SELECT 'abc'::text AS sample_column;

The result of executing this is as follows:

sample_column
----------
abc

As a result, it succeeded even though there was no FROM clause.

To solve this question, I opened a browser and searched for “SELECT statement without FROM clause” as a keyword. However, no answer was found in the search results.

So I decided to ask my senior at work site. I received a hint from my senior. He said “The FROM clause is omitted sometimes, it is not missing from the SELECT statement”.

I opened the browser again and searched for “SELECT statement with the FROM clause omitted” to find the answer.

SQL: omit the FROM clause

I found that if you do not need to refer to the database table, you can omit the FROM clause in the SELECT statement. For example, when using SELECT instead of a calculator as follows:

SELECT 1*2;

Note that there are DBMS that can omit the FROM clause in SELECT statements and some are cannot.

Thank you for reading.

One thought on “About SELECT statement without FROM clause(SQL)

Leave a comment