MySQL + SQL · Lesson 54

CASE Expression in MySQL

CASE — IF/ELSE in SQL

CASE lets a query make decisions, like an if-else, to produce different output per row.

Grade from Marks

SELECT name, marks,
  CASE
    WHEN marks >= 90 THEN 'A+'
    WHEN marks >= 75 THEN 'A'
    WHEN marks >= 33 THEN 'Pass'
    ELSE 'Fail'
  END AS grade
FROM students;
Each student gets a grade based on their marks.

Summary

  • CASE WHEN ... THEN ... ELSE ... END picks output by condition.
  • Great for grades, labels and categories inside a query.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp