Conditional logic

SQL Conditional Logic Interview Questions

Conditional logic is how you decide a value row by row, using CASE WHEN in SQL. It is what turns a business rule into something a query can produce: sorting numbers into bands, labeling rows, or splitting one column into several.

What conditional logic questions ask you to do

Across the questions here, these are the shapes that come up:

  • Sort a number into named bands such as high, mid and low
  • Count only the rows that meet a condition
  • Turn values from one column into several columns
  • Flag a row using a rule that spans two columns
  • Replace an empty value with something meaningful

Common mistakes with conditional logic

A CASE stops at the first branch that matches, so bands have to be written narrowest first. Put "over 100" before "over 500" and nothing is ever labeled high, because everything matches the first branch. A CASE with no ELSE is the other trap: every row that falls through comes back empty and then vanishes from any total built on it.

Questions to practice

Roles this comes up in

Companies these come from

Other topics