0% found this document useful (0 votes)
12 views25 pages

Stored Procedure

The document provides a comprehensive overview of stored procedures in SQL, including their definition, advantages, creation, and execution methods. It covers various aspects such as parameters, error handling, performance improvement, and best practices for writing efficient procedures. Additionally, it includes practical examples of stored procedures for managing employee and order data.

Uploaded by

parth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

Stored Procedure

The document provides a comprehensive overview of stored procedures in SQL, including their definition, advantages, creation, and execution methods. It covers various aspects such as parameters, error handling, performance improvement, and best practices for writing efficient procedures. Additionally, it includes practical examples of stored procedures for managing employee and order data.

Uploaded by

parth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Question 1

What is a stored procedure?


A stored procedure is a pre-compiled and reusable database object that contains a
group of SQL statements. It is stored in the database and can be executed by calling
its name.

Question 2
What are the advantages of using stored procedures?
Some advantages of using stored procedures include:

• Improved performance due to pre-compilation.


• Enhanced security by controlling data access through procedures.
• Simplified complex operations by encapsulating logic.
• Reduced network traffic by sending only the procedure call rather than
multiple SQL statements.

Question 3
How do you create a stored procedure in SQL?
To create a stored procedure in SQL, you use the CREATE PROCEDURE
statement, followed by the procedure name, input parameters (if any), and the SQL
statements that define the procedure.

Question 4
How do you call a stored procedure from SQL or an application?
You can call a stored procedure using the EXECUTE or EXEC statement in SQL,
or by invoking it from application code using the appropriate database library or
framework.

Question 5
What are input and output parameters in stored procedures?
Input parameters allow you to pass values to the stored procedure when it is called.
Output parameters allow the procedure to return values back to the caller.

Question 6
How do you handle errors in a stored procedure?
Errors in stored procedures can be handled using TRY...CATCH blocks (in SQL
Server) or EXCEPTION blocks (in PostgreSQL and other databases) to catch and
handle exceptions gracefully.

Question 7
Can a stored procedure call another stored procedure?
Yes, stored procedures can call other stored procedures, either within the same
database or in different databases.

Question 8
What are the different types of parameters that a stored procedure can have?
A stored procedure can have input parameters, output parameters, and parameters
that serve both as input and output (INOUT).

Question 9
How do you pass multiple values to a stored procedure as a single
parameter?
To pass multiple values as a single parameter, you can use techniques like passing
comma-separated values, XML, or JSON data.

Question 10
What is dynamic SQL, and when is it useful in stored procedures?
Dynamic SQL involves generating and executing SQL statements at runtime within
a stored procedure. It is useful when the structure of the query needs to change
dynamically based on user inputs or conditions.

Question 11
How can you improve the performance of a stored procedure?
Performance of a stored procedure can be improved by optimizing the underlying
SQL queries, using appropriate indexes, and parameterizing queries to avoid SQL
injection.

Question 12
How do you grant permissions to execute a stored procedure?
You can grant execution permissions to a stored procedure using the GRANT
EXECUTE statement to specific users or roles.

Question 13
Can a stored procedure return multiple result sets?
Yes, a stored procedure can return multiple result sets, and they can be accessed one
by one from the application code.

Question 14
What is the difference between a stored procedure and a user-defined
function?
Stored procedures are primarily used for data manipulation and complex logic,
while user-defined functions are used to return scalar values or tablevalued results.

Question 15
How do you debug a stored procedure?
You can use debugging tools provided by the database management system or print
debugging information to identify issues in the stored procedure code.

Question 16
What are nested stored procedures?
Nested stored procedures are procedures that call other stored procedures within
their logic.

Question 17
How do you view the source code of a stored procedure?
The source code of a stored procedure can usually be viewed using system catalogs
or metadata views specific to the database system.

Question 18
Can you use transactions within a stored procedure?
Yes, stored procedures can include transactions to ensure data integrity and
consistency during complex operations.

Question 19
How do you drop a stored procedure?
You can drop a stored procedure using the DROP PROCEDURE statement.

Question 20
What are the best practices for writing efficient and maintainable stored
procedures?
Some best practices include using meaningful and consistent naming conventions,
modularizing procedures, handling errors, and commenting the code for clarity.
Here are 30 SQL coding questions on stored procedures with schema and insert into
data:

Easy Level (1-10)

1. Create a stored procedure to insert a new employee into the employees table.

CREATE TABLE employees (

id INT,

name VARCHAR(255),

department VARCHAR(255)

);

INSERT INTO employees (id, name, department)

VALUES (1, 'John', 'Sales'),

(2, 'Alice', 'Marketing'),

(3, 'Bob', 'IT');

CREATE PROCEDURE sp_insert_employee(


@name VARCHAR(255),

@department VARCHAR(255)

AS

BEGIN

INSERT INTO employees (name, department)

VALUES (@name, @department);

END;

2. Create a stored procedure to retrieve all employees from the employees table.

CREATE PROCEDURE sp_get_employees

AS

BEGIN

SELECT * FROM employees;

END;
3. Create a stored procedure to update an employee's department.

CREATE PROCEDURE sp_update_department(

@id INT,

@department VARCHAR(255)

AS

BEGIN

UPDATE employees

SET department = @department

WHERE id = @id;

END;

4. Create a stored procedure to delete an employee.

CREATE PROCEDURE sp_delete_employee(

@id INT
)

AS

BEGIN

DELETE FROM employees

WHERE id = @id;

END;

5. Create a stored procedure to retrieve an employee's details by id.

CREATE PROCEDURE sp_get_employee_by_id(

@id INT

AS

BEGIN

SELECT * FROM employees

WHERE id = @id;

END;
6. Create a stored procedure to retrieve all employees in a specific department.

CREATE PROCEDURE sp_get_employees_by_department(

@department VARCHAR(255)

AS

BEGIN

SELECT * FROM employees

WHERE department = @department;

END;

7. Create a stored procedure to retrieve the total number of employees.

CREATE PROCEDURE sp_get_total_employees

AS

BEGIN
SELECT COUNT(*) AS total_employees

FROM employees;

END;

8. Create a stored procedure to retrieve the average salary of all employees.

CREATE PROCEDURE sp_get_average_salary

AS

BEGIN

SELECT AVG(salary) AS average_salary

FROM employees;

END;

9. Create a stored procedure to retrieve the highest salary of all employees.

CREATE PROCEDURE sp_get_highest_salary


AS

BEGIN

SELECT MAX(salary) AS highest_salary

FROM employees;

END;

10. Create a stored procedure to retrieve the lowest salary of all employees.

CREATE PROCEDURE sp_get_lowest_salary

AS

BEGIN

SELECT MIN(salary) AS lowest_salary

FROM employees;

END;

Medium Level (11-20)


11. Create a stored procedure to insert a new order into the orders table.

CREATE TABLE orders (

id INT,

customer_id INT,

order_date DATE,

total DECIMAL(10, 2)

);

INSERT INTO orders (id, customer_id, order_date, total)

VALUES (1, 1, '2022-01-01', 100.00),

(2, 1, '2022-01-15', 200.00),

(3, 2, '2022-02-01', 50.00);

CREATE PROCEDURE sp_insert_order(

@customer_id INT,

@order_date DATE,

@total DECIMAL(10, 2)

)
AS

BEGIN

INSERT INTO orders (customer_id, order_date, total)

VALUES (@customer_id, @order_date, @total);

END;

12. Create a stored procedure to retrieve all orders for a specific customer.

CREATE PROCEDURE sp_get_orders_by_customer(

@customer_id INT

AS

BEGIN

SELECT * FROM orders

WHERE customer_id = @customer_id;

END;
13. Create a stored procedure to update an order's total.

CREATE PROCEDURE sp_update_order_total(

@id INT,

@total DECIMAL(10, 2)

AS

BEGIN

UPDATE orders

SET total = @total

WHERE id = @id;

END;

14. Create a stored procedure to delete an order.

CREATE PROCEDURE sp_delete_order(

@id INT
)

AS

BEGIN

DELETE FROM orders

WHERE id = @id;

END;

15. Create a stored procedure to retrieve the total number of orders.

CREATE PROCEDURE sp_get_total_orders

AS

BEGIN

SELECT COUNT(*) AS total_orders

FROM orders;

END;

Medium Level (16-20)


16. Create a stored procedure to retrieve the average order total for a specific
customer.

CREATE PROCEDURE sp_get_average_order_total_by_customer(

@customer_id INT

AS

BEGIN

SELECT AVG(total) AS average_order_total

FROM orders

WHERE customer_id = @customer_id;

END;

17. Create a stored procedure to retrieve the highest order total for a specific
customer.

CREATE PROCEDURE sp_get_highest_order_total_by_customer(


@customer_id INT

AS

BEGIN

SELECT MAX(total) AS highest_order_total

FROM orders

WHERE customer_id = @customer_id;

END;

18. Create a stored procedure to retrieve the lowest order total for a specific
customer.

CREATE PROCEDURE sp_get_lowest_order_total_by_customer(

@customer_id INT

AS

BEGIN

SELECT MIN(total) AS lowest_order_total


FROM orders

WHERE customer_id = @customer_id;

END;

19. Create a stored procedure to retrieve the total number of orders for a
specific customer.

CREATE PROCEDURE sp_get_total_orders_by_customer(

@customer_id INT

AS

BEGIN

SELECT COUNT(*) AS total_orders

FROM orders

WHERE customer_id = @customer_id;

END;
20. Create a stored procedure to retrieve the total order value for a specific
customer.

CREATE PROCEDURE sp_get_total_order_value_by_customer(

@customer_id INT

AS

BEGIN

SELECT SUM(total) AS total_order_value

FROM orders

WHERE customer_id = @customer_id;

END;

Hard Level (21-30)

21. Create a stored procedure to retrieve the top 10 customers with the highest
total order value.
CREATE PROCEDURE sp_get_top_customers_by_order_value

AS

BEGIN

SELECT TOP 10 customer_id, SUM(total) AS total_order_value

FROM orders

GROUP BY customer_id

ORDER BY total_order_value DESC;

END;

22. Create a stored procedure to retrieve the bottom 10 customers with the
lowest total order value.

CREATE PROCEDURE sp_get_bottom_customers_by_order_value

AS

BEGIN

SELECT TOP 10 customer_id, SUM(total) AS total_order_value

FROM orders
GROUP BY customer_id

ORDER BY total_order_value ASC;

END;

23. Create a stored procedure to retrieve the average order total for each
customer.

CREATE PROCEDURE sp_get_average_order_total_by_customer

AS

BEGIN

SELECT customer_id, AVG(total) AS average_order_total

FROM orders

GROUP BY customer_id;

END;

24. Create a stored procedure to retrieve the highest order total for each
customer.
CREATE PROCEDURE sp_get_highest_order_total_by_customer

AS

BEGIN

SELECT customer_id, MAX(total) AS highest_order_total

FROM orders

GROUP BY customer_id;

END;

25. Create a stored procedure to retrieve the lowest order total for each
customer.

CREATE PROCEDURE sp_get_lowest_order_total_by_customer

AS

BEGIN

SELECT customer_id, MIN(total) AS lowest_order_total

FROM orders
GROUP BY customer_id;

END;

26. Create a stored procedure to retrieve the total number of orders for each
customer.

CREATE PROCEDURE sp_get_total_orders_by_customer

AS

BEGIN

SELECT customer_id, COUNT(*) AS total_orders

FROM orders

GROUP BY customer_id;

END;

27. Create a stored procedure to retrieve the total order value for each
customer.
CREATE PROCEDURE sp_get_total_order_value_by_customer

AS

BEGIN

SELECT customer_id, SUM(total) AS total_order_value

FROM orders

GROUP BY customer_id;

END;

28. Create a stored procedure to retrieve the top 10 products with the highest
total order value.

CREATE PROCEDURE sp_get_top_products_by_order_value

AS

BEGIN

SELECT TOP 10 product_id, SUM(total) AS total_order_value

FROM order_items

GROUP BY product_id
ORDER BY total_order_value DESC;

END;

29. Create a stored procedure to retrieve the bottom 10 products with the lowest
total order value.

CREATE PROCEDURE sp_get_bottom_products_by_order_value

AS

BEGIN

SELECT TOP 10 product_id, SUM(total) AS total_order_value

FROM order_items

GROUP BY product_id

ORDER BY total_order_value ASC;

END;

30. Create a stored procedure to retrieve the total order value for each product,
along with the product name and category.

You might also like