MySQL CASE WHEN statements and IF statement example.
CREATE TABLE student (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
marks int(11) NOT NULL,
course_type varchar(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
SELECT name, marks, CASE
WHEN marks>75 THEN 'A'
WHEN marks>65 THEN 'B'
WHEN marks>50 THEN 'C'
ELSE 'F'
END
AS results
FROM student ORDER BY name
SELECT name,course_type,
CASE course_type
WHEN 'WE' THEN 'Week End'
WHEN 'WD' THEN 'Week Days'
END AS course
FROM student ORDER BY name
SELECT name, marks, IF(marks >= 50,'PASS','FAIL') AS result
FROM student
ORDER BY name
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city
blockchain Training in electronic city
Hibernate Training in electronic city