Functions & Procedures Flashcards

1
Q

What are functions and procedures in SQL?

A

They are a set of SQL statements that accomplish a specific task, packaged under a name for reuse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why are functions and procedures useful?

A

They abstract logic, promote reuse, and can be permission-controlled by the database admin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the main role of a function in SQL?

A

To operate on input data and return a single usable output without affecting the table directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two types of functions in MySQL?

A

Built-in (system) functions and User-defined functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a built-in function in SQL?

A

A predefined function provided by MySQL, such as COUNT(), AVG(), NOW(), etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a user-defined function?

A

A function created by the user to operate on inputs and return a result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax to define a user-defined function in MySQL?

A

DELIMITER // CREATE FUNCTION name (…) RETURNS type BEGIN … RETURN …; END // DELIMITER ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why must every SQL function return something?

A

Because functions are designed to return a single value as output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the DELIMITER keyword used for in MySQL?

A

To change the statement terminator so SQL doesn’t misinterpret the function body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why change the delimiter when creating a function?

A

So that semicolons inside the function body are not treated as the end of the entire function declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does deterministic mean for a function?

A

It always returns the same output for the same input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does non-deterministic mean for a function?

A

It may return different outputs for the same input (e.g., using NOW()).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a procedure in SQL?

A

A block of SQL statements that may return none, one, or many outputs, usually executed using CALL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between a function and a procedure?

A

A function returns a value and is used in expressions, while a procedure performs actions and uses SELECT or OUT parameters for output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a procedure be used as an expression in another SQL statement?

A

No, only functions can be used as expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define a procedure in SQL?

A

Using CREATE PROCEDURE with optional IN, OUT, and INOUT parameters.

17
Q

What is the syntax to create a procedure in MySQL?

A

DELIMITER // CREATE PROCEDURE name ([params]) BEGIN … END // DELIMITER ;

18
Q

What does IN mean in procedure parameters?

A

The variable is used inside the procedure (input only).

19
Q

What does OUT mean in procedure parameters?

A

The variable is used to return data from the procedure (output only).

20
Q

What does INOUT mean in procedure parameters?

A

The variable is both input and output.

21
Q

How do you execute a procedure?

A

Using the CALL keyword, e.g., CALL procedure_name();

22
Q

What command deletes a function?

A

DROP FUNCTION IF EXISTS function_name;

23
Q

What command deletes a procedure?

A

DROP PROCEDURE IF EXISTS procedure_name;

24
Q

If you want to reuse a calculation logic across queries, what should you use?

A

A user-defined function.

25
Q

If you want to perform multiple actions and possibly return multiple results, what should you use?

A

A procedure.

26
Q

Spot the mistake: ‘CREATE FUNCTION myfunc() BEGIN RETURN 1; END;’

A

Missing RETURN type and proper delimiter syntax.

27
Q

Spot the mistake: ‘CALL myfunc();’

A

CALL is used for procedures, not functions.

28
Q

Explain when you’d choose a function over a procedure.

A

Use a function when you need a single return value that can be embedded in an expression; use a procedure for complex workflows.

29
Q

Describe the role of DELIMITER in function and procedure definitions.

A

It prevents early termination of SQL statements inside BEGIN…END blocks by changing the SQL statement terminator.