Search code examples
sqlelasticsearchelasticsearch-opendistro

Explanation for the following SQL query


I came across this SQL query and am not able to understand how is it working.

SELECT answer AS answer
FROM
  (SELECT answer
   FROM "default"."enriched-responses-dev") AS virtual_table
LIMIT 1000;

And how is it different from this?

SELECT answer AS answer
FROM "default"."enriched-responses-dev"
LIMIT 1000;

Solution

  • I think the second SQL statement is more efficient because there is no need for a temporary result. As far as the output is concerned, both will return the same result.

    There is no need for a sub-query in this instance.

    It can be simplified to:

    SELECT answer
    FROM "default"."enriched-responses-dev"
    LIMIT 1000;