Function vs procedure
- SQL course
- Advanced
- Lesson 46/81
- 4 min
Compare the two routine types.
Function vs procedure
A function returns a value, a procedure does work. Function in queries, procedure via CALL.
-- function: SELECT get_tax(100);
-- procedure: CALL add_product('Kalem', 5);Function value, procedure action.
Function vs procedure — details
A function cannot usually do INSERT/UPDATE, a procedure can. Function in queries, procedure outside.
In short: write a function when you compute a value, and a procedure when you run a sequence of steps that changes data.
Practice Function vs procedure
Create a get_tax function and call it in a SELECT.
Starter code
-- function vs procedure
CREATESample solution
CREATE FUNCTION get_tax(price DECIMAL)
RETURNS DECIMAL
LANGUAGE SQL
AS $$
SELECT price * 0.18;
$$;Expected output
Function oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Function
____ → returns a value
Function → returns a value
Explanation: Function is the right answer.
2 Function vs procedure: match the terms
| Term | Meaning |
|---|---|
function |
returns value |
procedure |
does work |
CALL |
call |
SELECT |
query |
3 Which returns a value?
- Procedure
- Function Correct answer
- Trigger
- A view
Explanation: A function returns a value.