PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

CASE

← All stacks

Theory

129/439

CASE

CASE WHEN marks >= 60 THEN 'A' WHEN marks >= 40 THEN 'B' ELSE 'F' END as a column. Simple CASE: CASE grade WHEN 'A' THEN 5 ELSE 0 END. END is required. Works in SELECT, ORDER BY, sometimes UPDATE SET.

More readable than nested IF(). Always ELSE so NULL doesn’t sneak in.

Trap — forgetting END. Trap: overlapping WHEN without noticing order (first match wins).

On the example next to this theory: CASE: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Exam tip

CASE WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END

CASE — sample query

-- CASE
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

CASE: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Short notes

  • DefCASE WHEN … THEN … ELSE … END.
  • RuleFirst matching WHEN. Always ELSE. Don’t forget END.
  • TrapNested IF instead of CASE.

Questions

1

Why CASE over nested IF?

129 / 439

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.