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_number | name | marks |
|---|
| 1 | Roshan | 85 |
| 2 | Vivek | 92 |
| 3 | Ajay | 78 |
| 4 | Gurpreet | 95 |
| 5 | Satish | 45 |
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
| name | marks | result |
|---|
| Roshan | 85 | B |
| Vivek | 92 | A |
| Ajay | 78 | C |
| Gurpreet | 95 | A |
| Satish | 45 | F |