🗄️ SQL for beginners · Class 12 · Lesson 13 of 18 · Free
SQL Stored Procedures – MySQL Procedures and What SQLite Does Instead
⏱ About 35 minutes · ▶ run real SQL here — nothing to install · no sign-in
In this lesson you will
- Read a MySQL stored procedure with IN parameters, IF and CALL
- Explain why SQLite has no stored procedures
- Replace a procedure in SQLite with a view, a trigger or app code
The school office pays fees, prints report cards and checks dues every day, always with the same steps. Big schools keep those steps inside the database as named programs called stored procedures, so every app calls the same tested logic. This lesson shows real MySQL procedures, and is honest about SQLite: it has none, so you will learn what people use there instead.
What is a stored procedure in SQL?
A stored procedure is a named set of SQL statements saved inside the database server. It can take inputs, called parameters. You run it with CALL name(values).
Below is a MySQL procedure that prints one student's report card. IN p_student_id INT means: an input number. DELIMITER // tells the MySQL command line that the procedure ends at //, not at the first ; inside it.
This box is read-only: the browser runs SQLite, and SQLite cannot run it. You will run the SQLite version in the next section.
DELIMITER //
CREATE PROCEDURE student_report(IN p_student_id INT)
BEGIN
SELECT sub.name AS subject, m.marks
FROM marks m
JOIN subjects sub ON sub.id = m.subject_id
WHERE m.student_id = p_student_id
ORDER BY sub.name;
END //
DELIMITER ;
CALL student_report(1);Does SQLite have stored procedures?
No. SQLite has no CREATE PROCEDURE and no CALL. That is a design choice. MySQL and PostgreSQL are servers that many apps talk to over a network. SQLite is a library that usually lives inside one app, so the app's own code can do the job of a procedure.
In SQLite you write the same query and pass the student id from your app with a ? placeholder, as in Lesson 6. To practise here, a tiny CTE (Lesson 3) called params holds the input in one place.
Try it: change 1 to 5 and run again. That is your "CALL student_report(5)".
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
┌──────────────────┬───────┐ │ subject │ marks │ ├──────────────────┼───────┤ │ Chemistry │ 79 │ │ Computer Science │ 95 │ │ English │ 84 │ │ Maths │ 92 │ │ Physics │ 88 │ └──────────────────┴───────┘ (5 rows)
How do I write a MySQL stored procedure with IF and a transaction?
Procedures can do more than one SELECT. This one pays a fee from the wallet, like Lesson 12. DECLARE makes local variables. SELECT … INTO stores a value in a variable.
IF … THEN … END IF checks the rules. SIGNAL stops the procedure with your own error message. The three changes run inside START TRANSACTION … COMMIT, so they happen together.
Every app (website, office desktop, mobile) now calls pay_fee(2, '2026-07') and gets the same checks. That shared logic is the main reason people use procedures.
DELIMITER //
CREATE PROCEDURE pay_fee(IN p_student INT, IN p_month CHAR(7))
BEGIN
DECLARE v_amount INT;
DECLARE v_balance INT;
SELECT amount INTO v_amount FROM fees
WHERE student_id = p_student AND month = p_month AND paid = 0;
SELECT balance INTO v_balance FROM wallets
WHERE student_id = p_student;
IF v_amount IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'No unpaid fee for that month';
END IF;
IF v_balance IS NULL OR v_balance < v_amount THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Not enough money in the wallet';
END IF;
START TRANSACTION;
UPDATE wallets SET balance = balance - v_amount WHERE student_id = p_student;
UPDATE fees SET paid = 1 WHERE student_id = p_student AND month = p_month;
INSERT INTO receipts (student_id, month, amount) VALUES (p_student, p_month, v_amount);
COMMIT;
END //
DELIMITER ;
CALL pay_fee(2, '2026-07');How can a SQL view replace a read-only procedure?
Many procedures only read data. A view from Lesson 10 does that job in SQLite. Leave the "parameter" out of the view, and add it as a WHERE when you use it.
So CALL student_report(3) in MySQL becomes SELECT … FROM student_report WHERE student_id = 3 in SQLite. The saved logic lives in one place, just like a procedure.
Try it: change 3 to 7 and run again. Then add AND marks >= 80 to see only good subjects.
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
View student_report created. ┌──────────────────┬───────┐ │ subject │ marks │ ├──────────────────┼───────┤ │ Chemistry │ 85 │ │ Computer Science │ 70 │ │ English │ 88 │ │ Maths │ 81 │ │ Physics │ 77 │ └──────────────────┴───────┘ (5 rows)
How can a trigger do a procedure's job in SQLite?
Some procedures exist to keep tables in step, like "when a fee is paid, write a receipt". SQLite can do that with a trigger: SQL that runs by itself when a row changes. Lesson 14 covers triggers fully.
Below, the trigger watches the paid column of fees. When it changes from 0 to 1, it inserts a receipt. NEW means the row after the change.
The money checks from pay_fee stay in the app, inside a transaction (Lesson 12). Try it: also pay Dev's fee by adding his UPDATE (student 4, '2026-07') and run again.
📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1); CREATE TABLE wallets ( student_id INTEGER PRIMARY KEY REFERENCES students(id), balance INTEGER NOT NULL CHECK (balance >= 0) ); INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000); CREATE TABLE receipts ( id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL );
Trigger fee_paid_receipt created. 1 row updated. ┌────────────┬─────────┬────────┐ │ student_id │ month │ amount │ ├────────────┼─────────┼────────┤ │ 2 │ 2026-07 │ 4500 │ └────────────┴─────────┴────────┘ (1 row)
🤔 Predict the output
Think like SQL: read the code and choose what it prints. You get the reason straight away.
Question 1. Meera Iyer is student 5. What does this "procedure" return?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
WITH params AS (SELECT 5 AS student_id) SELECT COUNT(*) FROM params p JOIN marks m ON m.student_id = p.student_id;
Question 2. This UPDATE touches all 11 fee rows. How many receipts are written?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1); CREATE TABLE wallets ( student_id INTEGER PRIMARY KEY REFERENCES students(id), balance INTEGER NOT NULL CHECK (balance >= 0) ); INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000); CREATE TABLE receipts ( id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL ); CREATE TRIGGER fee_paid_receipt AFTER UPDATE OF paid ON fees WHEN OLD.paid = 0 AND NEW.paid = 1 BEGIN INSERT INTO receipts (student_id, month, amount) VALUES (NEW.student_id, NEW.month, NEW.amount); END;
UPDATE fees SET paid = 1; SELECT COUNT(*) FROM receipts;
Question 3. Kabir (student 6) scored 55, 62, 49 and 67. What is returned?
📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE VIEW student_total AS SELECT student_id, SUM(marks) AS total FROM marks GROUP BY student_id; SELECT total FROM student_total WHERE student_id = 6;
🧩 Your challenge
MySQL has a procedure subject_report(IN p_subject) that shows the number of students, the average (1 decimal) and the top marks for one subject. Build the SQLite version as a view called subject_report with columns subject, students, average, top_marks, then "call" it for Physics. Expected one row: Physics, 6, 74, 91.
View subject_report created. ┌─────────┬──────────┬─────────┬───────────┐ │ subject │ students │ average │ top_marks │ ├─────────┼──────────┼─────────┼───────────┤ │ Physics │ 6 │ 74 │ 91 │ └─────────┴──────────┴─────────┴───────────┘ (1 row)
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
⚠️ Common mistakes (and how to fix them)
❌ Wrong
CREATE PROCEDURE class_list(IN p_class INT)
BEGIN
SELECT name FROM students WHERE class = p_class;
END;Error: near "PROCEDURE": syntax error
✅ Right
CREATE VIEW class_list AS SELECT name, class FROM students; SELECT name FROM class_list WHERE class = 12 ORDER BY name;
SQLite has no stored procedures, so CREATE PROCEDURE is a syntax error. Use a view (or app code with a ? placeholder) instead.
❌ Wrong
CALL student_report(1);
Error: near "CALL": syntax error
✅ Right
SELECT subject, marks FROM student_report WHERE student_id = 1 ORDER BY subject;
CALL exists in MySQL and PostgreSQL, not in SQLite. Select from the view and pass the value with WHERE.
❌ Wrong
SELECT * FROM student_report(1);
Error: 'student_report' is not a function
✅ Right
SELECT * FROM student_report WHERE student_id = 1 ORDER BY subject;
A view is not a function and cannot take parameters in brackets. Filter it with WHERE.
📌 Remember
- A stored procedure is named SQL saved in a database server, run with CALL name(values).
- MySQL procedures use IN parameters, DECLARE, IF and SIGNAL; DELIMITER // is only for the command line.
- SQLite has no stored procedures: it lives inside one app, so the app's code plays that role.
- In SQLite, a view replaces a read-only procedure; a WHERE replaces its parameter.
- A trigger can do automatic follow-up work, like writing a receipt when a fee is paid.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a stored procedure and a function in SQL?
A function returns one value and can be used inside a SELECT, like ROUND(). A procedure is run on its own with CALL, can change data and can return result tables. MySQL and PostgreSQL let you write both.
Do stored procedures stop SQL injection?
Only if the values are passed as parameters, not glued into a SQL string. A ? placeholder in app code (Lesson 6) gives the same protection. The safety comes from parameters, not from procedures.
Does PostgreSQL have stored procedures?
Yes. PostgreSQL has CREATE PROCEDURE (run with CALL) and CREATE FUNCTION, usually written in its PL/pgSQL language. The syntax differs from MySQL, but the idea is the same.
Should I put all my logic in stored procedures?
Teams differ. Procedures keep shared rules in one place, close to the data. App code is easier to test, track in version control and move to another database. Many apps use a few procedures and keep the rest in code.