Questions
14 of 30
1What are functions and operators in MySQL, and how are they different?
2What is the difference between single-row functions and aggregate functions?
3Give examples of commonly used string functions in MySQL.
4What is the use of the CONCAT() function? How is it different from using ||?
5Explain the difference between NOW(), CURDATE(), and SYSDATE().
6What are arithmetic operators in MySQL? Give examples.
7What is the difference between the = and <=> operators?
8What does the DISTINCT keyword do when used with aggregate functions like COUNT()?
9What is the difference between IFNULL() and COALESCE() functions?
10What are logical operators in MySQL, and how do AND, OR, and NOT work?
11What is the difference between LENGTH() and CHAR_LENGTH() functions?
12How does MySQL handle type conversion when using operators on different data types?
13What is the difference between ROUND(), TRUNCATE(), and FORMAT() functions?
14Explain how CASE and IF() functions can be used for conditional logic.
15What are comparison operators in MySQL, and how are they used with NULL values?
16How do aggregate functions like AVG(), SUM(), and MAX() behave when NULLs are present?
17Explain the use of REGEXP and LIKE operators. What’s the difference between them?
18What are user-defined functions (UDFs), and how do they differ from stored procedures?
19What are the differences between DATE_ADD() and ADDDATE() functions?
20How can you use STR_TO_DATE() and DATE_FORMAT() for converting and displaying date values?
21How does MySQL internally optimize and cache results of deterministic functions?
22What are window functions in MySQL 8.0, and how do they differ from aggregate functions?
23Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() window functions.
24How does MySQL evaluate operator precedence when multiple operators are used in a single expression?
25Can functions be used in the WHERE clause? What are the performance implications?
26How can you use JSON_EXTRACT() and JSON_CONTAINS() to work with JSON data in MySQL?
27What are the performance trade-offs of using scalar functions inside JOIN or GROUP BY clauses?
28Explain how collation affects comparison operators in string functions.
29What are deterministic and non-deterministic functions? How does this affect replication and indexes?
30How would you combine multiple functions and operators to clean, transform, and aggregate data efficiently in one query?
14 / 30

Explain how CASE and IF() functions can be used for conditional logic.

Difficulty: 5/10
conditional expressions, SQL query logic, performance

Using CASE and IF() for Conditional Logic in MySQL

MySQL provides two powerful tools for conditional logic: CASE (standard SQL) and IF() (MySQL-specific). Both allow you to return different values based on conditions.

1. IF() Function
  1. 1

    MySQL-specific conditional function.

  2. 2

    Takes three arguments: IF(condition, value_if_true, value_if_false).

  3. 3

    Works like a simple ternary operator.

Example of IF()
When to Use IF()
  1. 1

    When you need simple, single-condition logic.

  2. 2

    When you want shorter, more compact expressions.

2. CASE Expression
  1. 1

    Part of standard SQL (portable across databases).

  2. 2

    Supports multiple conditions.

  3. 3

    Can be used in SELECT, WHERE, ORDER BY, and HAVING clauses.

Simple CASE Example
Searched CASE Example (Multiple Conditions)
When to Use CASE
  1. 1

    When multiple conditions must be evaluated.

  2. 2

    When you need more readable logic.

  3. 3

    When writing SQL that should work across different database systems.

Key Differences
  1. 1

    IF(): MySQL-only, simpler, handles only one condition.

  2. 2

    CASE: Standard SQL, more flexible, supports multiple branches.

In summary: Use IF() for simple true/false checks, and CASE when you need structured, multi-condition logic.

Scenario Questions

0-2 years experience

  1. 1We have a table `orders` with a numeric `status` column (0 = pending, 1 = shipped, other = unknown). Write a SELECT that returns a readable status string using a CASE expression.
  2. 2How would you use the IF() function in a WHERE clause to filter rows where `quantity` is NULL or zero?
  3. 3Show me a query that uses IF() to compute a column `is_active` that is 1 when `last_login` is within the past 30 days, otherwise 0.

2-5 years experience

  1. 1Our sales report needs to apply three different discount rates based on `customer_type` and `order_total`. Explain how you'd implement this with CASE and why you might prefer it over nested IF() calls.
  2. 2A query groups sales by region and applies a tax rate that varies per region code. The totals look off—walk me through how you'd debug the CASE expression.
  3. 3We want to flag rows as 'high', 'medium', or 'low' risk using three numeric columns. Discuss the trade‑offs of using CASE versus multiple IF() functions in the SELECT list.

5-8 years experience

  1. 1Our nightly ETL processes millions of rows and currently uses many IF() functions for data cleansing. How would you refactor to CASE, evaluate performance impact, and verify correctness?
  2. 2Design a view that normalizes status codes coming from three legacy tables, each using different numeric codes, by mapping them to a common textual status with CASE. Discuss maintainability and any indexing considerations.
  3. 3Explain how placing a CASE expression inside a JOIN condition could affect the optimizer's choice of join order, and what steps you’d take to keep the query performant at scale.

8+ years experience

  1. 1Our codebase heavily embeds business rules in IF() statements inside stored procedures. Lead a migration to a standardized CASE‑based approach—what process, tooling, and cross‑team communication would you put in place?
  2. 2Discuss the architectural pros and cons of encoding complex business logic directly in CASE expressions versus moving that logic to an application layer or a dedicated rule engine.
  3. 3We plan to expose a set of analytical APIs that rely on many CASE‑driven calculations. How would you design the underlying schema and query layer to ensure long‑term extensibility and minimal technical debt?

Follow-up Questions

  • What changes would you make if you needed to add another condition to the CASE expression?
  • How does MySQL evaluate IF() versus CASE in terms of short‑circuiting?
  • Can you describe any indexing strategies that help queries with conditional logic?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.