| by Arround The Web | No comments

SQLite Case Statements

SQLite has a case statement which allows us to include conditional logic in our SQL statements. SQLite case expressions execute a list of conditions and return an expression based on the results. SQLite case statements behave identically to IF – THEN – ELSE statements like the other scripting languages. If the phrase is valid, we can use the SQLite case statement inside any of the clauses or SQL statements. In general, the SQLite Case statement starts with an optional expression and one or more WHEN… THEN clauses, an optional ELSE clause, and a necessary END keyword.

Syntax of the CASE statement in SQLite:

In SQLite, the case statement usually has one of two forms.

SQLite simple Case statement:

CASE test_statement
WHEN [condition1] THEN [statement1]
WHEN [condition2] THEN [statement2]
......
......
......
WHEN [condition(n)] THEN [statement(n)]
ELSE [statement]
END

We use the syntax above to establish many conditions to reach the desired outcome. When and then clauses are used in the above syntax. They function in a logical order. To return the final output, this method compares each statement to the list of statements. This is a fairly easy approach for executing the case statement according to the user’s requirements. Each condition and statement are interdependent, meaning, when the first condition is true, the statement is only executed after that. This way, all conditions, and statements are executed. If a condition is false, control is transferred to the else part as seen in the above syntax.

To choose the outcome, the query case statement evaluates a list of statements. It’s worth noting that the simple case expression just looks for equity. Whereas, the looked-through case statement can use any form of inspection.

SQLite Search case statement:

CASE test_statement
WHEN [BOOLEAN statement1] THEN [statement1]
WHEN[BOOLEAN statement2] THEN[statement2]
 ELSE [ statement] END

If the Boolean statement in the specified grouping is valid, the Search case statement evaluates it and returns the corresponding result. When no valid statement is found, the query case statement returns the statement in the ELSE condition. If the ELSE clause is neglected, the looked-through case statement returns NULL. When the stated criteria are met, the search case statement terminates the assessment and execution.

Creating table for CASE statement in SQLite:

First, we have created a table and gave the name “Student” with the CREATE query. The table is set with the different columns such as ID with the data type integer and create ID as a primary key, NAME, and EMAIL is set with the data type TEXT. The last column CGPA is assigned a TEXT data type. The table Student and its attributes are shown in the SQLite shell as follows:

CREATE TABLE Student(
   ...>       ID INT PRIMARY KEY    ,
   ...>       NAME           TEXT   ,
   ...>       EMAIL          TEXT   ,
   ...>       CGPA           FLOAT
   ...>     );

Now, we have to insert the values against each column of the table Student. With the SQLite INSERT query, we have inserted five rows in each of the columns specified in the table Student. The screenshot below is showing the way of inserting records into the table columns.

sqlite> INSERT INTO Student VALUES (2, 'Ibrahim','Ibrahim31@gmail.com', 3.20 );
sqlite> INSERT INTO Student VALUES (3, 'Maha','Maha89@gmail.com', 3.9);
sqlite> INSERT INTO Student VALUES (4, 'Jennifer', 'Jennifer22@gmail.com', 2.5);
sqlite> INSERT INTO Student VALUES (5, 'Rehan', 'Rehan99@gmail.com', 3.10 );

Let’s view whether the data is recorded in the table Student. By using the SELECT statement, we have retrieved the entire data in the table Student in a table format. The aforementioned statement is represented in the screenshot below:

sqlite> SELECT * FROM Student;

Example 1: Program of using SQLite simple CASE statement:

We have used a simple CASE statement with the SELECT statement to show the workings of the CASE statement in SQLite.

Here, we have used a SELECT statement and selected a column ID, NAME, and CGPA from the table Student. After that, we used a CASE statement that compares the CGPA of the Student. It matches the CGPA with the condition whether the CGPA is greater than or equal to 4.00 or not. If so, then it gives that CGPA an A+. But if the condition is false, then CASE will switch to the next statement and check whether the CGPA is greater than 3.5. If the condition is true, then it assign this CGPA a grade “A”.

Like this, the control is passed to each case statement until the CGPA does not meet the given condition. If all the cases are false, then the else clause will be executed and will print a statement FAIL.

sqlite> SELECT ID, NAME, CGPA,
   ...> CASE
   ...> WHEN CGPA >= 4.00 THEN "A+"
   ...> WHEN CGPA >= 3.5 THEN "A"
   ...> WHEN CGPA >= 3.0 THEN "B"
   ...> WHEN CGPA >= 2.5 THEN "C"
   ...> ELSE "FAIL"
   ...> END AS "GRADE"
   ...> FROM Student;

Now, we will perform the query and look at the outcome. It should look like this: The GRADE column is included in the table Student along with the values from the CASE statement.

Example 2: Program of using SQLite search CASE statement:

To determine the outcome, the searched CASE statement analyses a set of expressions. The simple CASE expression simply compares for equality. Whereas, the searching CASE expression can compare in any way.

A Boolean case statement is used in the example given. For this, we have a SELECT query through which we have selected three attributes ID, NAME, and CGPA from the table Student. Only these attributes and their corresponding values will appear as the output. Then, we have a CASE keyword for the SQLite CASE statement. The condition is applied with the WHEN keyword. First, case statement checks if the CGPA is greater than 3.5 and then assigns the grade A. If the condition is not satisfied here, then we will move to our second case statement which checks the condition whether is satisfied here or not. If both our cases are false, then the else part will execute and print the grade C.

sqlite> SELECT ID, NAME, CGPA,
   ...> CASE
   ...> WHEN CGPA > 3.5 THEN "A"
   ...> WHEN CGPA >  3.0 AND CGPA < 2.5 THEN "B"
   ...> ELSE
   ...> "C"
   ...> END "GRADE"
   ...> FROM Student;

When executing the above CASE query, the results are obtained like this:

Conclusion:

We studied the basic syntax of case statements in this article. We also saw a variety of case statement instances. The rules for case statements were also taught. We learned using the SQLite case statement in this post and when to do so.

Share Button

Source: linuxhint.com

Leave a Reply