Conditional Selection

 Introduction

Instead of using all data as we have done so far using the SELECT keyword, you can present a condition that the database would follow to isolate specific records. One of the keywords you can use to formulate conditions is WHERE. Its basic syntax is:

`SELECT What FROM WhatObject WHERE Expression;`

The expressions used in conditions are built using algebraic, logical, and string operators. The Expression factor is called a criterion. Although a group of expressions, making it plural is called criteria, the word criteria is sometimes used for a singular expression also.

The expression is written using the formula:

`ColumnName=Value`

The ColumnName factor must be an existing column of the table. It is followed by the assignment operator. The Value factor is the value that would set the condition. If the value is a word or a group of words (also called a string), you must include it in single-quotes. Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE Gender='Male';",

rstPeople->Close();
}```

If it is a number, you can type its numeric value.
 The TRUE and FALSE Constants

In Boolean algebra, something is considered TRUE when it holds a value. The value is also considered as 1 or Yes. By contrast, if something doesn't hold a value, it is considered non-existent and non-worthy of consideration. Such a thing has a value of FALSE, 0, or No. To retrieve such a value, you can just find out if the value of a field exists or not.

The comparison for a True or False value can be performed on a Boolean column. If a record has a value of 1, the table considers that such a field is True. If the field has a 0 value, then it holds a FALSE value.

 The NULL Constant

While the True and False constants are used to find out whether a Boolean expression has a positive or a negative value, the database provides another constant used to find out whether a field is empty. This can be done using the NULL constant.

When a field holds a value, the value would be considered using the comparison operators we will learn shortly. If the field is empty, it holds a value of NULL.

 Filtering Records With Boolean Operators

 Equality =

The equality operator is used to compare two values for similarity. The syntax of this operation is:

`Value1 = Value2`

If Value1 and Value2 hold the same value, the comparison produces a TRUE result. If they hold different values, the comparison renders a FALSE value. Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE Gender='Male';",

rstPeople->Close();
}```

 Inequality <>

To find out if two fields hold different values, you can use the inequality operator which is represented by <>. Its syntax is:

`Value1 <> Value2`

This comparison is performed between Value1 and Value2. If they hold different values, then the comparison produces a TRUE value. If they hold the same value, the comparison produces FALSE. This shows that the equality (=) and the inequality (<>) operators are opposite each other. Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE Gender <> 'Female';",

rstPeople->Close();
}```
 Less Than <

The "Less Than" operator uses the following syntax:

`Value1 < Value2`

If Value1 holds a value that is lower than that of Value2, the comparison produces TRUE. If Value1 holds a value that is greater than or similar to that of Value2, the comparison renders FALSE.

 Less Than or Equal <=

When comparing two values, you may want to know whether two fields hold the same value or if one is lower than the other. This comparison can be performed with the "Less Than Or Equal To" operator. It is represented by <= and its formula is:

`Value1 <= Value2`

If both operands (Value1 and Value2) hold the same value, then the comparison produces a TRUE result. If Value1 holds a value that is lower than that of Value2, the comparison still produces a TRUE result. By contrast, if the value of Value1 is higher than that of Value2, the comparison renders a FALSE result. Notice that the > and the <= operators are opposite each other.

 Greater Than >

The > operator is used to find out whether one value is "Greater Than" another. Its syntax is:

`Value1 > Value2`

The operation is performed on the values of Value1 and Value2. If Value1 holds a value greater than that of Value2, then the comparison produces TRUE. Otherwise, the comparison produces FALSE. That is, if the value of Value2 is greater than or equal to that of Value1, then the comparison produces FALSE.

 Greater Than or Equal >=

If you have two values and want to find out whether they hold similar values or the first is greater than the second, you can use the >= operator whose syntax is:

`Value1 >= Value2`

If both Value1 and Value2 hold the same value, then the comparison renders a TRUE result. Similarly, if the left operand, Value1, holds a value greater than that of the right operand, Value2, the comparison still produces TRUE. If the value of Value1 is less than the value of Value2, the comparison produces a FALSE result. Therefore, < and >= are opposite.

 The NOT Operator

To deny the presence, the availability, or the existence of a value, you can use the NOT operator. This operator is primarily used to reverse a Boolean value. For example, we have learned that FALSE is the opposite of TRUE. In the same way, TRUE is the opposite of FALSE. If you want to compare a value as not being TRUE, the NOT TRUE would produce the same result as the FALSE value. For the same reason, the expression NOT FALSE is the same as TRUE.

Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE NOT (Gender='Male');",

rstPeople->Close();
}```
 The IS Operator

To validate something as being possible, you can use the IS operator. For example, to acknowledge that something is NULL, you can use the IS NULL expression. Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE Gender IS NULL;",

rstPeople->Close();
}```

In the same way, to validate that something is not null, you can use the expression IS NOT NULL

Here is an example:

```System::Void btnPeople_Click(System::Object^  sender, System::EventArgs^  e)
{

L"Data Source='C:\\Programs\\People.mdb'", L"", L"", 0);
rstPeople->Open(L"SELECT LastName, FirstName, Gender "
L"FROM Persons "
L"WHERE Gender IS NOT NULL;",

rstPeople->Close();
}```

 Comparison Operators: IN

If you have a series of records and want to find a record or a group of records among them, you can use the IN operator.