Search code examples
sqlsubquery

SQL Nested /Sub-query and SQL produce the same result. Why using Nested/Sub-Query?


I don't understand why the solution gives me a nested solution when I can get the same result with non-nested SQL stmt. Is it better to use Nested SQL ? Thanks !

Here is the code :

SQL :

select EMP_ID, F_NAME, L_NAME, DEP_ID from employees;

SQL Nested/Sub-Query solution :

select * from ( select EMP_ID, F_NAME, L_NAME, DEP_ID from employees) AS EMP4ALL;

Solution

  • The query optimser of whichever platform you use will almost certainly parse these to be identical and produce the same execution plan for each.

    In short, they are the same; the derived table query may be a cut-down version of something more complex and the author just chose to leave it that way, unknown really without more context.