What if you could send and receive data in one simple step without rewriting everything?
Why Parameters (IN, OUT, INOUT) in SQL? - Purpose & Use Cases
Imagine you have a recipe book where you write down every step of cooking a dish. Now, if you want to change an ingredient or get the cooking time, you have to rewrite the entire recipe each time or guess the details yourself.
Manually changing or sharing information like ingredients or cooking time is slow and confusing. You might forget to update some parts or mix up details, causing mistakes and wasted effort.
Using parameters like IN, OUT, and INOUT in SQL is like having a smart recipe card where you can input ingredients, get cooking times, and even adjust quantities without rewriting the whole recipe. It makes sharing and updating information clear and easy.
CALL CalculatePrice(100, NULL, NULL); -- manually guessing outputCALL CalculatePrice(100, @totalPrice, @discountRate);It allows you to pass information in, get results back, and even update values dynamically within one simple call.
Think of an online store where you input how many items you want (IN), get the total price back (OUT), and maybe adjust a discount rate as you go (INOUT) without extra steps.
Parameters let you send data into and get data out of database procedures.
IN passes information in, OUT returns results, and INOUT does both.
This makes database tasks faster, clearer, and less error-prone.
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
