Hi! Could we please enable some services and cookies to improve your experience and our website?
No, thanks.
Okay!
SQLize
Online
/
PHPize Online
/
SQLtest Online
A
A
A
Share code
Donate
Blog
Popular
FAQ
Donate
A
A
A
Share
Blog
Popular
FAQ
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
SQL code:
Upload
Copy
Format
Clear
-- Step 1: Create students table CREATE TABLE students ( roll_no INT PRIMARY KEY, name VARCHAR(50), marks INT ); -- Step 2: Insert sample data INSERT INTO students VALUES (1, 'Arun', 92), (2, 'Priya', 76), (3, 'Rahul', 63), (4, 'Sneha', 49), (5, 'Karthik', 85); -- Step 3: Change delimiter for function DELIMITER $$ -- Step 4: Create get_grade() function CREATE FUNCTION get_grade(score INT) RETURNS VARCHAR(2) DETERMINISTIC BEGIN DECLARE grade VARCHAR(2); IF score >= 90 THEN SET grade = 'A'; ELSEIF score >= 75 THEN SET grade = 'B'; ELSEIF score >= 60 THEN SET grade = 'C'; ELSEIF score >= 50 THEN SET grade = 'D'; ELSE SET grade = 'F'; END IF; RETURN grade; END$$ -- Step 5: Reset delimiter DELIMITER ; -- Step 6: Create student_grades table CREATE TABLE student_grades ( roll_no INT PRIMARY KEY, grade VARCHAR(2) ); -- Step 7: Insert grades using function INSERT INTO student_grades (roll_no, grade) SELECT roll_no, get_grade(marks) FROM students; -- Step 8: View final result SELECT s.name, s.marks, g.grade FROM students s JOIN student_grades g ON s.roll_no = g.roll_no;
SQL
Server:
MySQL 8.0
MySQL 8.0 Sakila (ReadOnly)
MySQL 9.3.0
MariaDB 11.4
MariaDB 11.8
MariaDB 10
MariaDB 10 Sakila (ReadOnly)
SQLite 3
SQLite 3 Preloaded
PostgreSQL 10 Bookings (ReadOnly)
PostgreSQL 13
PostgreSQL 14
PostgreSQL 15
PostgreSQL 16
PostgreSQL 17
MS SQL Server 2017
MS SQL Server 2019
MS SQL Server 2022
MS SQL Server 2022 AdventureWorks (ReadOnly)
Firebird 4.0
Firebird 4.0 (Employee)
Oracle Database 19c (HR)
Oracle Database 21c
Oracle Database 23c Free
SOQOL
Version
ER Diagram
Preserve result
Stuck with a problem?
Got Error?
Ask ChatGPT!
Result:
Copy
Clear