OneCompiler

Result

226

MySql

This query uses the CASE statement to evaluate multiple conditions and return the corresponding result.

Let's create a Database.

Open MySql Workbench.

-- step 1
CREATE DATABASE Result;
-- step 2
USE Result;
-- step 3
CREATE TABLE result (
    roll_number INT PRIMARY KEY,
    name VARCHAR(255),
    marks INT
);
-- step 4 insert some data.
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
INSERT INTO result (roll_number, name, marks) VALUES
(1, 'Roshan', 85),
(2, 'Vivek', 92),
(3, 'Ajay', 78),
(4, 'Gurpreet', 95),
(5, 'Satish', 45);
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- step 5
-- Display the data in the 'result' table
SELECT * FROM result;

Output

roll_numbernamemarks
1Roshan85
2Vivek92
3Ajay78
4Gurpreet95
5Satish45

CASE statement

-- let's use CASE statement to evaluate multiple conditions and return the corresponding result. 
SELECT 
    roll_number, 
    name, 
    marks,
    CASE 
        WHEN marks >= 90 THEN 'A'
        WHEN marks >= 80 THEN 'B'
        WHEN marks >= 70 THEN 'C'
        WHEN marks >= 50 THEN 'D'
        ELSE 'F'
    END AS result
FROM result;

Output

namemarksresult
Roshan85B
Vivek92A
Ajay78C
Gurpreet95A
Satish45F