Parameters (IN, OUT, INOUT) in SQL - Time & Space Complexity
When using parameters in SQL procedures, it's important to see how the number of operations changes as input grows.
We want to know how the procedure's work scales with the size of input data passed through parameters.
Analyze the time complexity of this stored procedure using IN, OUT, and INOUT parameters.
CREATE PROCEDURE UpdateScores(
IN student_id INT,
IN new_score INT,
OUT old_score INT,
INOUT total_updates INT
)
BEGIN
SELECT score INTO old_score FROM Scores WHERE id = student_id;
UPDATE Scores SET score = new_score WHERE id = student_id;
SET total_updates = total_updates + 1;
END;
This procedure updates a student's score, returns the old score, and increments a counter of total updates.
Look for repeated actions or loops in the procedure.
- Primary operation: Single SELECT and UPDATE statements each run once per call.
- How many times: Each operation runs exactly once per procedure execution.
The procedure does a fixed number of steps regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (1 SELECT, 1 UPDATE, 1 SET) |
| 100 | 3 (same fixed steps) |
| 1000 | 3 (still the same fixed steps) |
Pattern observation: The number of operations stays the same no matter how big the input values are.
Time Complexity: O(1)
This means the procedure takes the same amount of time no matter the input size.
[X] Wrong: "The procedure takes longer if the input numbers are bigger."
[OK] Correct: The procedure runs a fixed number of steps regardless of input values; bigger numbers don't mean more work here.
Understanding how parameters affect procedure work helps you explain efficiency clearly and confidently in real situations.
"What if the procedure included a loop that updated multiple rows based on an input list? How would the time complexity change?"
Practice
IN parameter type in a SQL stored procedure do?Solution
Step 1: Understand parameter types in SQL procedures
Parameters can beIN,OUT, orINOUT.INmeans input only.Step 2: Identify the role of
INparametersINparameters pass values into the procedure but do not return values.Final Answer:
It allows the procedure to receive input values only. -> Option BQuick Check:
IN= input only [OK]
- Confusing IN with OUT or INOUT
- Thinking IN parameters can return values
- Assuming IN declares internal variables
OUT parameter named result of type INT in a MySQL stored procedure?Solution
Step 1: Recall parameter declaration syntax in MySQL
Parameters are declared asIN,OUT, orINOUTfollowed by name and type.Step 2: Check each option's syntax
CREATE PROCEDURE proc_name(OUT result INT) uses correct syntax:OUT result INT. CREATE PROCEDURE proc_name(INOUT result INT) uses INOUT, not OUT. CREATE PROCEDURE proc_name(IN result INT) uses IN, not OUT. CREATE PROCEDURE proc_name(RESULT OUT INT) has wrong order and keyword.Final Answer:
CREATE PROCEDURE proc_name(OUT result INT) -> Option CQuick Check:
OUT parameter syntax = OUT name type [OK]
- Using IN instead of OUT
- Swapping parameter order
- Using wrong keywords like RESULT OUT
CALL calculate_sum(5, 10, @total);
SELECT @total;
Given the procedure signature:
CREATE PROCEDURE calculate_sum(IN a INT, IN b INT, OUT sum INT)
BEGIN
SET sum = a + b;
END;
What will be the output of
SELECT @total;?Solution
Step 1: Understand procedure parameters and call
The procedure takes two input numbers and outputs their sum via the OUT parametersum. The call passes 5 and 10 as inputs, and @total as the OUT variable.Step 2: Calculate the output value
The procedure setssum = 5 + 10 = 15. This value is assigned to the session variable@total.Final Answer:
15 -> Option DQuick Check:
5 + 10 = 15 [OK]
- Expecting NULL because of no SELECT inside procedure
- Confusing IN and OUT parameters
- Not using session variables for OUT
CREATE PROCEDURE update_value(INOUT val INT)
BEGIN
SET val = val + 1;
END;
Which of the following calls will correctly update the session variable
@x from 10 to 11?Solution
Step 1: Understand INOUT parameter usage
INOUT parameters require a variable to pass in and receive updated value. Session variables like@xcan be used.Step 2: Analyze each call
CALL update_value(@x); passes@xcorrectly. CALL update_value(10); passes a literal 10, which is not a variable. CALL update_value(INOUT @x); uses invalid syntax. CALL update_value(IN @x); uses IN keyword, which is incorrect for INOUT.Final Answer:
CALL update_value(@x); -> Option AQuick Check:
INOUT needs variable, not literal [OK]
- Passing literals instead of variables
- Using IN or INOUT keywords in CALL
- Expecting procedure to update literals
INOUT parameters a and b. Which of the following procedure bodies correctly swaps the values?Solution
Step 1: Understand swapping logic with INOUT parameters
Swapping requires a temporary variable to hold one value while assigning the other.Step 2: Evaluate each option's correctness
BEGIN DECLARE temp INT; SET temp = a; SET a = b; SET b = temp; END uses a temporary variable to swap correctly. BEGIN SET a = b; SET b = a; END overwritesabefore assigningb. BEGIN SET a = a + b; SET b = a - b; SET a = a - b; END uses arithmetic swap but is valid only if no overflow and no NULLs. BEGIN SET a = b; SET b = a + b; END does not swap correctly.Final Answer:
BEGIN DECLARE temp INT; SET temp = a; SET a = b; SET b = temp; END -> Option AQuick Check:
Temporary variable swap = correct [OK]
- Overwriting variables without temp
- Using arithmetic swap without checks
- Ignoring NULL or overflow risks
Ready to prove it?
Pass the test and this pattern moves unknown
