# if

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: if
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/boolean-functions/if-function
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/boolean-functions/if-function.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/boolean-functions/if-function.adoc
description: The `if()` function returns one value if the condition is TRUE and another value if the condition is FALSE.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/boolean-functions/if-function.md -->

The `if()` function returns one value if the condition is `TRUE` and another value if the condition is `FALSE`.

## [](#syntax)Syntax

```sql
IF(expression, true_result, else_result)
```

The `expression` must evaluate to a Boolean (`TRUE` or `FALSE`).

## [](#examples)Examples

### [](#if-with-a-table)`if()` with a table

The following example uses a `test_result` table to determine which participants passed and failed:

```sql
CREATE TABLE test_result (
  applicant_id int,
  name text,
  score int
);

INSERT INTO test_result VALUES
(78765,'Mike Aoki',677),
(78786,'Julie Grahams',650),
(78986,'Alexandra Jones',450),
(79742,'Lucas Moore',487),
(79769,'Augustine Harkness',572);
```

View the table:

```sql
SELECT * FROM test_result;
```

The query returns:

```sql
+---------------+--------------------+--------+
| applicant_id  | name               | score  |
+---------------+--------------------+--------+
| 78765         | Mike Aoki          | 677    |
| 78786         | Julie Grahams      | 650    |
| 78986         | Alexandra Jones    | 450    |
| 79742         | Lucas Moore        | 487    |
| 79769         | Augustine Harkness | 572    |
+---------------+--------------------+--------+
```

1.  The following query returns `'PASSED'` if the score is 500 or greater, and `'NOT PASSED'` otherwise:

    ```sql
    SELECT name, IF(score>=500, 'PASSED', 'NOT PASSED') AS result FROM test_result;
    ```

2.  The query returns:

    ```sql
    +--------------------+-------------+
    | name               | result      |
    +--------------------+-------------+
    | Mike Aoki          | PASSED      |
    | Julie Grahams      | PASSED      |
    | Alexandra Jones    | NOT PASSED  |
    | Lucas Moore        | NOT PASSED  |
    | Augustine Harkness | PASSED      |
    +--------------------+-------------+
    ```


### [](#if-with-expressions-as-return-value)`if()` with expressions as return value

The following example uses a `deptcost` table to determine which departments exceeded the budget:

```sql
CREATE TABLE deptcost (
  dept text,
  budget int,
  actual int,
  status text
);
INSERT INTO deptcost VALUES
('Finance', 800,677,'within budget'),
('HR', 700,930,'over budget'),
('Marketing', 500,677,'over budget'),
('Project', 720,700,'within budget'),
('Sales', 910,860,'within budget');
```

View the table:

```sql
SELECT * FROM deptcost;
```

The query returns:

```sql
+-----------+--------+--------+---------------+
| dept      | budget | actual | status        |
+-----------+--------+--------+---------------+
| Finance   | 800    | 677    | within budget |
| HR        | 700    | 930    | over budget   |
| Marketing | 500    | 677    | over budget   |
| Project   | 720    | 700    | within budget |
| Sales     | 910    | 860    | within budget |
+-----------+--------+--------+---------------+
```

1.  The following query returns the budget difference if `actual` is less than `budget`, and `0` otherwise:

    ```sql
    SELECT dept, IF(actual < budget, budget - actual, 0) AS budget_remaining FROM deptcost;
    ```

2.  The query returns:

    ```sql
    +-----------+------------------+
    |   dept    | budget_remaining |
    +-----------+------------------+
    | Finance   |              123 |
    | HR        |                0 |
    | Marketing |                0 |
    | Project   |               20 |
    | Sales     |               50 |
    +-----------+------------------+
    ```