Set custom messages on the basis of a column with student marks in MySQL


For this, use CASE statement. Let us first create a table −

mysql> create table DemoTable1952
   (
   Marks int
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1952 values(35);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1952 values(65);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1952 values(55);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1952 values(39);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1952;

This will produce the following output −

+-------+
| Marks |
+-------+
|    35 |
|    65 |
|    55 |
|    39 |
+-------+
4 rows in set (0.00 sec)

Here is the query to set custom messages on the basis of student marks:

mysql> select
   case when Marks > 40 then 'Good Marks' else 'Not Good Marks' end as Result
   from DemoTable1952;

This will produce the following output −

+----------------+
| Result         |
+----------------+
| Not Good Marks |
| Good Marks     |
| Good Marks     |
| Not Good Marks |
+----------------+
4 rows in set (0.00 sec)

Updated on: 31-Dec-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements