IF

If the first argument is true, returns second argument. If the first argument is false or NULL, returns third argument.

Syntax

IF(condition, value_if_true, value_if_false)

Arguments

  • Any SQL objects

Return Type

Returns a value based on a specified condition.

Examples

Create a table and insert values:

CREATE TABLE orders(comp_id INT(10), comp_name VARCHAR(50),
order_number INT(10), order_total INT(10));
INSERT INTO orders VALUES("64","Wolf-Conn","4722","9"),
("924","Blick, Von and Lynch","9610","52"),
("487","Sipes-Bauch","5180","14"),
("835","Shields-Kiehn","7961","684"),
("476","Bogisich, Brown and Hessel","4687","482"),
("809","Cole-Durgan","9358","467"),
("232","Towne LLC","3774","445");

Create a query using the IF function:

SELECT order_number,
comp_id,
order_total,
IF(order_total > 100, 'Yes', 'No') AS amount_greater_than_100
FROM orders;
+--------------+---------+-------------+-------------------------+
| order_number | comp_id | order_total | amount_greater_than_100 |
+--------------+---------+-------------+-------------------------+
|         4687 |     476 |         482 | Yes                     |
|         9358 |     809 |         467 | Yes                     |
|         5180 |     487 |          14 | No                      |
|         3774 |     232 |         445 | Yes                     |
|         7961 |     835 |         684 | Yes                     |
|         4722 |      64 |           9 | No                      |
|         9610 |     924 |          52 | No                      |
+--------------+---------+-------------+-------------------------+

Last modified: March 13, 2024

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK