Computer Science

SQL Conditional Statements

SQL Conditional Statements are used to control the flow of execution in SQL queries. They allow you to specify conditions that must be met before certain actions are taken. The most common conditional statements in SQL are IF, CASE, and NULLIF.

Written by Perlego with AI-assistance

3 Key excerpts on "SQL Conditional Statements"

  • A Comprehensive Study of SQL
    eBook - ePub

    A Comprehensive Study of SQL

    Practice and Implementation

    • Jagdish Chandra Patni(Author)
    • 2022(Publication Date)
    • CRC Press
      (Publisher)
    6 Conditional Statements and Operators in SQL
    DOI: 10.1201/9781003324690-6
    Conditional statements allow us to change the way our program behaves based on the input it receives, the contents of variables, or several other factors.

    6.1 Introduction

    A database encompasses one or multiple tables with rows and columns. Each table is represented by a name, and SQL statements are used to perform any action on a database. Here, we will discuss conditional statements in SQL, which are based on some conditional expressions. An expression can be any arrangement of SQL literals, like variables, operators, and functions that, on execution, returns a logical value if the condition is satisfied.
    Although SQL isn’t a general-purpose language, it includes conditional statements with a similar structure to other languages. The classic IF and CASE statements permit the ability to change data on the query level instead of modifying them in another environment, such as a worksheet or a data frame.

    6.2 Conditional Evaluation

    While neither of these statements is hard to master, especially with previous exposure to programming languages, they offer plenty of power and flexibility to modify query results. To demonstrate how these statements may be used, a simple table will be created detailing a student ID, name, department, and project.
    Create table student ( studentID Integer Primary Key, name varchar (10), department varchar(10), project char(10) );

    6.3 Types of Condition

    6.3.1 IF Condition

    The most basic form of an IF statement in SQL looks very similar to conditional statements in most other programming languages and software.
    • If (condition, True, False) from table;
    An IF statement simply introduces some condition and then returns a result based on whether the condition is true or false. When the condition is true, the second parameter is returned, and when false, the third parameter.
  • ANSI C Programming
    eBook - ePub

    ANSI C Programming

    Learn ANSI C step by step

    Chapter 1 , we have used sequence control structure in which the various steps are executed sequentially, i.e. in the same order in which they appear in the program. In fact, to execute the instructions sequentially, we don’t have to do anything at all. By default, the instructions in a program are executed sequentially. However, in serious programming situations, seldom do we want the instructions to be executed sequentially. Many a time, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt with in C programs using a decision control instruction. As mentioned earlier, a decision control instruction can be implemented in C using:
    (a) The if statement
    (b) The if-else statement
    (c) The conditional operators Now let us learn each of these and their variations in turn.

    The if Statement

    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
    if ( this condition is true )   execute this statement;
    The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true, then the statement is not executed; instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity? As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.
    Figure 4.1
    The relational operators should be familiar to you except for the equality operator == and the inequality operator != . Note that = is used for assignment, whereas, == is used for comparison of two quantities. Here is a simple program, which demonstrates the use of if and the relational operators.
    On execution of this program, if you type a number less than 10, you get a message on the screen through printf( ) . If you type some other number the program doesn’t do anything. The flowchart given in Figure 4.2
  • Learn T-SQL From Scratch
    eBook - ePub

    Learn T-SQL From Scratch

    An Easy-to-Follow Guide for Designing, Developing, and Deploying Databases in the SQL Server and Writing T-SQL Queries Efficiently

    SELECT commands for variable value assignment.
    You would be wondering that the CASE and IF statements have similar functioning, but IF does not support the features which CASE does, then why to use the IF statement? That’s a very valid question. So, let’s understand when and why to use the IF statement.
    If you wish to perform an action based on a certain condition such as – assigning the value to a variable, executing a query (SELECT , INSERT , UPDATE , and DELETE ), or wish to execute any T-SQL code, then you can do it only with IF statement. The CASE statement will not help. So, if you want to manipulate the data in a T-SQL code or query, then you should use CASE . But if you wish to define control over the flow of the code (running a specific or set of T-SQL code based on a condition) then the IF statement is the only solution.
    Let us quickly have a look into the syntax of the IF statement:
    IF (<condition>) ELSE IF (<condition>) . . . ELSE IF (<condition>) ELSE
    Let us understand the attributes of the IF statement:
    • IF statement can be simple IF , or IF … ELSE IF , or IF … ELSE , or IF … ELSE IF … ELSE .
    • IF and ELSE IF are used to define the condition. The resulting action of IF , ELSE IF , and ELSE has to be defined in a BEGIN … END block.
    • IF and ELSE IF can be related to the WHEN keyword we discussed in the CASE statement.
    • ELSE is same in both IF and CASE statements.
    • In an IF
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.