Explain the difference between subquery,CTE and stored procedures


  1. Stored Procedure

A stored procedure is a precompiled collection of SQL statements (and sometimes control logic) stored in the database. It’s like a function that can be called whenever needed. It can accept parameters such as input and output, and is also often used for complex logic, such as loops.

  1. CTE (Common Table Expression)
    A CTE is a temporary named result set you can use within a single query. Think of it as a short-term, readable alias for a subquery. It exists only during query execution. CTE’s make complex queries easier to read and maintain.

  1. Subquery
    A subquery is a query nested inside another query; in other words, it is like a query within a query. It can appear within the SELECT, FROM, or WHERE clause, and it executes each time the main query is run.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *