On the other hand, imagine you want to get a list of students who live in Silver Spring. You would execute a statement as: SELECT StudentNumber, FirstName, LastName, Gender, City, ParentsNames FROM Registration.Students WHERE City = N'silver spring'; This would produce:
From these two results, notice that there is no relationship between the fact that one student is a female and the fact that a student lives in Silver Spring. Still, the SQL allows you to combine two condition and produce a result of what those two conditions have in common. This is referred to as a logical conjunction.
The simplest logical condition involves two columns of a table. You can create the conjunction visually or with code. To visually create a logical conjunction, open a table in the Query Designer and display the Criteria pane. In the Criteria section, click the Filter box that corresponds to the first desired field and type the condition. Click the Filter box that corresponds to the second desired field and type the other condition. Then execute the query. To create a logical conjunction in SQL, you use the AND operator to combine two conditions. The formula to follow is: SELECT WhatColumn(s) FROM WhatObject WHERE Condition1 AND Condition2 WhatColumn(s) and WhatObject are the same way we have used them so far. The AND keyword is the operator that joins the conditions. Each condition is written as a SQL operation using the formula: Column operator Value The Condition1 is the first that would be examined. If the first condition is false, the whole WHERE statement is false and there is no reason to examine the second condition. If the first condition is true, then the second condition would be examined. Based on this, suppose we want to get a list of female students who live in Silver Spring. The SQL statement used to get this list can be written as follows: SELECT StudentNumber, FirstName, LastName, Gender, City, ParentsNames FROM Registration.Students WHERE Gender = N'female' AND City = N'silver spring'; We stated that each condition was separately evaluated. For this reason, to make the conjunction statement easier to read, each condition should be included in parentheses. Therefore, the above SQL statement can be written as follows: SELECT StudentNumber, FirstName, LastName, Gender, City, ParentsNames FROM Registration.Students WHERE (Gender = N'female') AND (City = N'silver spring') This would produce:
The logical conjunction is used to check that two conditions are true, in which case both conditions must be valid. In a logical conjunction, if either condition is false, the whole statement is false. Sometimes you have two conditions that are equally important, in which the validity of either condition is good enough to make the whole statement true. This type of statement is referred to as logical disjunction. Besides their names, the conjunction and the disjunction have many logical differences. A logical conjunction needs two different fields. Although you can (there is no rule against it), if you use the same field for both conditions of a conjunction, the statement would produce nothing:
This means that you should always use more than one column. On the other hand, a logical disjunction can be performed on one field. In this case, you would ask the interpreter to find out if the field matches this or that value. This means that, among the many values that the field has, you want to isolate two values and consider only either of them. Like a conjunction, a disjunction can also be performed on more than one field.
As mentioned already, a logical disjunction can be used on one or more fields. You can create the disjunction visually or using code. To visually create a logical disjunction, open a table in the Query Designer and display the Criteria pane. You have various options. In the Criteria section:
After entering the conditions, execute the query. To create a logical disjunction in SQL, you use the OR operator between two conditions. The formula to follow is: SELECT WhatColumn(s) FROM WhatObject WHERE Condition1 OR Condition2 WhatColumn(s) and WhatObject are the same we have used so far. The OR keyword is the operator that joins the conditions. Each condition is written as a SQL operation using the formula: Column operator Value In this case:
Logical conjunctions and disjunctions operator differently. As mentioned already, in a logical conjunctions, both conditions must be true for the whole statement to be true. In a logical disjuction, the interpreter first examines the first condition. If that condition is true, it concludes that the whole statement is true and there is no need to examine the second condition. If the first condition is false, then it examines the second condition. If the second condition is true, even if the first condition was false, the whole statement is true. Consider the above first statement: SELECT StudentNumber, FirstName, LastName, Gender, City, ParentsNames FROM Registration.Students WHERE (City = N'bethesda') OR (City = N'silver spring'); The interpreter would check the City value of a record:
The interpreter then moves to the next record. Consider our second statement: SELECT StudentNumber, FirstName, LastName, Gender, City, ParentsNames FROM Registration.Students WHERE (Gender = N'female') OR (City = N'silver spring'); Once again, the interpreter would check each record:
|
|
|||||||||||||||||||||||||||
|