w3resource

Stored Procedures and Functions Exercises, Solutions with Explanations


SQL Stored Procedures and Functions Exercises with solutions [15 exercises with solution]

1. Create a Stored Procedure with Parameters

Write a SQL query to create a stored procedure that takes parameters and returns results.

Click me to see the solution

2. Create a User-Defined Function

Write a SQL query to create a scalar user-defined function that calculates an employee's annual salary.

Click me to see the solution

3. Create a Trigger for Logging Changes

Write a SQL query to create a trigger that logs changes to a table.

Click me to see the solution

4. Create a Table-Valued Function

Write a SQL query to create a table-valued function that returns all employees in a specific department.

Click me to see the solution

5. Execute a Stored Procedure with Output Parameters

Write a SQL query to create and execute a stored procedure that uses output parameters.

Click me to see the solution

6. Create a Stored Procedure with Error Handling

Write a SQL query to create a stored procedure that includes error handling using TRY...CATCH.

Click me to see the solution

7. Create a Trigger for Enforcing Business Rules

Write a SQL query to create a trigger that enforces a business rule (e.g., preventing negative salary updates).

Click me to see the solution

8. Create a Scalar Function with Conditional Logic

Write a SQL query to create a scalar function that calculates a bonus based on employee performance.

Click me to see the solution

9. Create a Trigger for Cascading Updates

Write a SQL query to create a trigger that cascades updates from one table to another.

Click me to see the solution

10. Create a Stored Procedure with Dynamic SQL

Write a SQL query to create a stored procedure that uses dynamic SQL to execute a query based on user input.

Click me to see the solution

11. Create a Stored Procedure with Pagination

Write a SQL query to create a stored procedure that retrieves paginated results from a table.

Click me to see the solution

12. Create a Table-Valued Function with Joins

Write a SQL query to create a table-valued function that retrieves data using joins between multiple tables.

Click me to see the solution

13. Create a Trigger for Audit Logging

Write a SQL query to create a trigger that logs all insert, update, and delete operations on a table.

Click me to see the solution

14. Create a Scalar Function with Aggregation

Write a SQL query to create a scalar function that calculates the average salary of employees in a department.

Click me to see the solution

15. Create a Stored Procedure with Temporary Tables

Write a SQL query to create a stored procedure that uses temporary tables to process intermediate results.

Click me to see the solution

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.