Junior SQL

What is the difference between a view and a stored procedure?

  • View:
  • A view is a virtual table defined by a SELECT query. It provides a way to view

and query data without modifying the underlying tables directly.

  • Views do not support procedural logic, loops, or variables.
  • Stored Procedure:
  • A stored procedure is a set of SQL statements that can be executed as a

single unit. It can include complex logic like loops, conditionals, and multiple

queries.

  • Stored procedures can perform INSERT, UPDATE, DELETE, and other

operations on the database. They can also return results.

Example of a stored procedure:

CREATE PROCEDURE GetEmployeeSalary(IN emp_id INT)

BEGIN

SELECT salary FROM employees WHERE id = emp_id;

END;

More from SQL Server Tutorial

All questions for this course