What is the purpose of the CREATE PROCEDURE statement in SQL?
It is used to create a stored procedure, which is a saved set of SQL statements that can be executed repeatedly.
Click to reveal answer
beginner
Which keyword starts the definition of a stored procedure in SQL?
The keyword CREATE PROCEDURE starts the definition.
Click to reveal answer
intermediate
How do you specify input parameters in a stored procedure?
Input parameters are listed in parentheses after the procedure name, each with a name and data type, e.g., (IN param_name INT).
Click to reveal answer
beginner
What keyword is used to mark the start of the procedure body?
The keyword BEGIN marks the start of the procedure body, and END marks its end.
Click to reveal answer
intermediate
Why use stored procedures instead of running SQL statements directly?
Stored procedures help reuse code, improve performance, and enhance security by controlling access and reducing SQL injection risks.
Click to reveal answer
Which of the following is the correct way to start creating a stored procedure named GetUser?
ACREATE PROCEDURE GetUser()
BMAKE PROCEDURE GetUser()
CNEW PROCEDURE GetUser()
DSTART PROCEDURE GetUser()
✗ Incorrect
The correct syntax to create a stored procedure starts with CREATE PROCEDURE followed by the procedure name.
How do you define an input parameter named userId of type INTEGER in a procedure?
A(userId INTEGER)
B(PARAM userId INTEGER)
C(INPUT userId INTEGER)
D(IN userId INTEGER)
✗ Incorrect
Input parameters are defined with the IN keyword followed by the parameter name and data type.
What keywords enclose the body of a stored procedure?
ABEGIN ... END
BBEGIN ... FINISH
COPEN ... CLOSE
DSTART ... STOP
✗ Incorrect
The procedure body is enclosed between BEGIN and END keywords.
Which of these is NOT a benefit of using stored procedures?
AImproved security
BSlower query execution
CCode reuse
DEasier maintenance
✗ Incorrect
Stored procedures usually improve performance, so slower execution is not a benefit.
In SQL, what does the CREATE PROCEDURE statement do?
ADeletes a procedure
BExecutes a procedure
CCreates a new stored procedure
DUpdates an existing procedure
✗ Incorrect
CREATE PROCEDURE is used to create a new stored procedure.
Explain the basic syntax structure of the CREATE PROCEDURE statement.
Think about how you name the procedure, add parameters, and write the commands it runs.
You got /5 concepts.
Describe why stored procedures are useful in managing database operations.
Consider how stored procedures help both developers and database systems.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the CREATE PROCEDURE statement in SQL?
easy
A. To display data from a table
B. To delete data from a table
C. To create a new database
D. To save a set of SQL commands that can be run later
Solution
Step 1: Understand the purpose of CREATE PROCEDURE
The CREATE PROCEDURE statement is used to save a group of SQL commands as a procedure that can be executed later.
Step 2: Compare options with the purpose
Options B, C, and D describe other SQL commands, not the purpose of CREATE PROCEDURE.
Final Answer:
To save a set of SQL commands that can be run later -> Option D
Quick Check:
CREATE PROCEDURE saves commands = A [OK]
Hint: Remember: procedures store commands to run later [OK]
Common Mistakes:
Confusing CREATE PROCEDURE with CREATE TABLE
Thinking it runs commands immediately
Mixing it up with SELECT statement
2. Which of the following is the correct basic syntax to create a procedure named GetUsers without parameters?
easy
A. CREATE PROCEDURE GetUsers AS SELECT * FROM Users;
B. CREATE PROCEDURE GetUsers BEGIN SELECT * FROM Users; END;
C. CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM Users; END;
D. CREATE PROCEDURE GetUsers SELECT * FROM Users;
Solution
Step 1: Recall standard procedure syntax
The correct syntax includes parentheses for parameters (empty if none), and a BEGIN...END block to enclose statements.
Step 2: Check each option
CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM Users; END; correctly uses CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM Users; END;. Options A, C, and D miss parentheses or BEGIN...END or have syntax errors.
Final Answer:
CREATE PROCEDURE GetUsers() BEGIN SELECT * FROM Users; END; -> Option C
Quick Check:
Procedure syntax needs () and BEGIN...END = B [OK]
Hint: Always use () and BEGIN...END in procedure syntax [OK]
Common Mistakes:
Omitting parentheses after procedure name
Not using BEGIN and END to wrap statements
Missing semicolon inside procedure
3. Given the procedure below, what will be the output when calling CALL GetUserCount();?
CREATE PROCEDURE GetUserCount()
BEGIN
SELECT COUNT(*) AS TotalUsers FROM Users;
END;
medium
A. No output because procedure does not return anything
B. The total number of rows in the Users table
C. Syntax error because of missing parameters
D. An error because SELECT cannot be used in procedures
Solution
Step 1: Understand what the procedure does
The procedure runs a SELECT COUNT(*) query on the Users table and returns the count as TotalUsers.
Step 2: Analyze the CALL statement output
Calling CALL GetUserCount(); executes the procedure and returns the count of rows in Users table as a result set.
Final Answer:
The total number of rows in the Users table -> Option B
Quick Check:
CALL runs procedure and returns SELECT result = C [OK]
Hint: CALL runs procedure and returns SELECT results [OK]
Common Mistakes:
Thinking procedures cannot return SELECT results
Assuming parameters are always required
Confusing procedure call with function call
4. Identify the error in the following procedure definition:
CREATE PROCEDURE AddUser(
IN username VARCHAR(50),
IN email VARCHAR(100)
)
INSERT INTO Users (Username, Email) VALUES (username, email);
END;
medium
A. Missing BEGIN before the INSERT statement
B. Parameters should not have IN keyword
C. END keyword is not needed
D. Procedure name cannot have parameters
Solution
Step 1: Check procedure block structure
Procedures must enclose statements inside BEGIN and END blocks. This code misses BEGIN before INSERT.
Step 2: Verify other syntax parts
IN keyword for parameters is correct, END is needed, and procedure names can have parameters.
Final Answer:
Missing BEGIN before the INSERT statement -> Option A
Quick Check:
Procedures need BEGIN...END blocks = A [OK]
Hint: Always wrap procedure statements with BEGIN and END [OK]
Common Mistakes:
Forgetting BEGIN before statements
Removing IN keyword from parameters
Omitting END keyword
5. You want to create a procedure UpdateUserEmail that takes a user ID and a new email, then updates the user's email in the Users table. Which of the following is the correct complete procedure?
hard
A. CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) BEGIN UPDATE Users SET Email = newEmail WHERE Id = userId; END;
B. CREATE PROCEDURE UpdateUserEmail(userId INT, newEmail VARCHAR(100)) UPDATE Users SET Email = newEmail WHERE Id = userId;
C. CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) UPDATE Users SET Email = newEmail WHERE Id = userId; END;
D. CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) BEGIN UPDATE Users SET Email = 'newEmail' WHERE Id = userId; END;
Solution
Step 1: Check parameter declaration and statement block
Parameters must have IN keyword and the statements must be inside BEGIN...END.
Step 2: Verify the UPDATE statement correctness
CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) BEGIN UPDATE Users SET Email = newEmail WHERE Id = userId; END; correctly uses parameters without quotes around newEmail, so it updates with the variable value. CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) BEGIN UPDATE Users SET Email = 'newEmail' WHERE Id = userId; END; incorrectly uses quotes, making it a string literal.
Final Answer:
CREATE PROCEDURE UpdateUserEmail(IN userId INT, IN newEmail VARCHAR(100)) BEGIN UPDATE Users SET Email = newEmail WHERE Id = userId; END; -> Option A
Quick Check:
Parameters with IN and BEGIN...END, no quotes on variables = D [OK]
Hint: Use IN parameters and BEGIN...END; no quotes on variables [OK]
Common Mistakes:
Omitting BEGIN...END block
Forgetting IN keyword for parameters
Using quotes around parameter variables in SQL
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.