Home > Mobile >  Explanation for the following SQL query
Explanation for the following SQL query

Time:06-22

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;

CodePudding user response:

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;

CodePudding user response:

this code select "answer" column from "default" database and "enriched-responses-dev" table and named it as "virtual_table" then from this table select "answer" column and named it as answer (and in fact if you don't use this part the result is same as when you are using this part) and this selection limited to last 1000 row SELECT answer AS answer FROM (SELECT answer FROM "default"."enriched-responses-dev") AS virtual_table LIMIT 1000;

but this code:

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

just select answer column from "enriched-responses-dev" this table from this "default" database and limit it to 1000 last rows

  • Related