Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
-- Drop the table if it exists (for re-runs) DROP TABLE IF EXISTS Students; -- Create the Students table CREATE TABLE Students ( StudentID INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Age INT NOT NULL, Gender CHAR(1) NOT NULL, Major VARCHAR(100) NOT NULL ); -- Insert sample data INSERT INTO Students (Name, Age, Gender, Major) VALUES ('Alice', 20, 'F', 'CS'), ('Bob', 21, 'M', 'Math'), ('Charlie', 19, 'M', 'Physics'); -- Drop procedure if it exists DROP PROCEDURE IF EXISTS InsertStudent; -- Create stored procedure without custom delimiter CREATE PROCEDURE InsertStudent( IN name VARCHAR(100), IN age INT, IN gender CHAR(1), IN major VARCHAR(100) ) BEGIN INSERT INTO Students(Name, Age, Gender, Major) VALUES (name, age, gender, major); END; -- Call the stored procedure to add another student CALL InsertStudent('David', 22, 'M', 'Biology'); -- Drop function if it exists DROP FUNCTION IF EXISTS GetAverageAge; -- Create function to get average age CREATE FUNCTION GetAverageAge() RETURNS DECIMAL(5,2) DETERMINISTIC BEGIN DECLARE avgAge DECIMAL(5,2); SELECT AVG(Age) INTO avgAge FROM Students; RETURN avgAge; END; -- Use the function SELECT GetAverageAge() AS AverageAge;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear