runtime
66 / 88

lesson 66 Database engineering · advanced

Stored procedures: parameters & contracts

Understand parameters and the expectations of stored procedures.

the idea

A SQL Server stored procedure can expose parameters, transactions, and error handling. SQLite has no CREATE PROCEDURE. Here you practice the retrieval contract in SQLite; the separate T-SQL reference shows its SQL Server wrapper. Choose procedures or external orchestration based on the system, not a blanket preference.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

orders6 columns · 12 rows
idinteger · keycustomer_idintegerproduct_idintegerquantityintegerstatustextordered_attext
101112paid2025-01-01
102231paid2025-01-02
103121paid2025-01-05
104352pending2025-01-08
105445paid2025-02-01

showing 5 of 12 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT id,customer_id,quantity
FROM orders
WHERE customer_id=1;

Watch for: Parameters should be bound by a client or procedure, not concatenated from untrusted text.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Call out the parameter boundary. Procedures package a contract: inputs in, result set out. Here the “parameter” is customer_id = 1 in the filter.

    step-1.sql
    SELECT DISTINCT customer_id
    FROM orders
    ORDER BY customer_id;
  2. Return rows for one parameter value. Filtering to a single customer_id models a parameterized lookup. Keep the contract explicit and narrow.

    step-2.sql
    SELECT id, customer_id, quantity
    FROM orders
    WHERE customer_id = 1;

challenge 1

For customer_id=1, return id, customer_id and quantity from orders. This practices a procedure’s retrieval contract; no procedure is created in SQLite.

Return idcustomer_idquantityany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.