Introduction to Conjunctions and Disjunctions
Introduction to Conjunctions and Disjunctions
Logical Conjunctions
Introduction
So far, we have stated the conditions one at a time. This made their interpretation kind of easy. Sometimes, you will need to test one condition that depends on, or is related to, another. Boolean algebra allows you to combine two conditions and use the result, or to test two conditions but consider if either is true.
Imagine you have a list of students and you want to study some statistics of your database. You already know that, to get a list of girls, you can filter the students based on the gender. You would execute a statement as:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE Gender = N'Female'; GO
This would produce:
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 [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE City = N'silver spring'; GO
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.
Practical Learning: Introducing Conjunctions and Disjunctions |
SELECT ItemNumber AS [Item #], Manufacturer, Category, SubCategory AS [Sub-Category], ItemName AS [Name/Description], UnitPrice AS [Unit Price] FROM Inventory.StoreItems
Creating 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:
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 [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE Gender = N'female' AND City = N'silver spring'; GO
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 [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'female') AND (City = N'silver spring')
This would produce:
Practical Learning: Creating a Logical Conjunction |
USE LambdaSquare1; GO SELECT "Unit #" = aparts.UnitNumber, Beds = aparts.Bedrooms, Baths = aparts.Bathrooms, [Monthly Rent] = aparts.Price, "Primary Deposit" = aparts.Deposit, (aparts.Price + aparts.Deposit) "Due Before Moving", CASE aparts.Available WHEN 0 THEN N'No or Not Yet' WHEN 1 THEN N'Yes' END "Available" FROM Presentation.Units aparts WHERE (Bedrooms = 1) AND (Available = 1) ORDER BY [Monthly Rent], "Primary Deposit"; GO
Logical Disjunctions |
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.
Creating a Logical Disjunction |
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:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (City = N'bethesda') OR (City = N'silver spring'); GO
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'female') OR (City = N'silver spring'); GO
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 [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (City = N'bethesda') OR (City = N'silver spring'); GO
The interpreter would check the City value of a record:
The interpreter then moves to the next record.
Consider our second statement:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'female') OR (City = N'silver spring'); GO
Once again, the interpreter would check each record:
Practical Learning: Creating a Logical Disjunction |
Topics on Logical Conjunctions and Disjunctions
Introduction
So far, for our introduction to conjunctions and disjunctions, we used only string-based columns. When it comes to string-based conditions, you can use the LIKE operator to either match a string exactly or specify an approximation (a character, a range of characters, or a sub-string). Here is an example:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'male') AND (LastName LIKE N'%an'); GO
This LIKE condition is meant to find all students whose last name end with an:
Practical Learning: Using Conjunctions and Disjunctions in Strings |
Conjunctions, Disjunctions, and NULL Fields |
To include the ability to check whether a field is null, in its condition, apply IS NULL to the column's name.
Practical Learning: Checking for Nullity |
Conjunctions, Disjunctions, and the Right Values |
Besides string-based fields, any column can participate in a logical conjunction or disjunction. For types other than strings, follow their rules:
Practical Learning: Filtering for the Right Values |
Negating a Condition |
As seen in previous lessons, there are many ways you can negate a Boolean condition. Remember that, to negate IS NULL, you can use IS NOT NULL.
Probably the easiest way to negate a condition is to precede it with the NOT operator. Here is an example:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'female') AND NOT (City = N'silver spring'); GO
Or better, include NOT and its statement in parentheses. Here is an example:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'female') AND (NOT (City = N'silver spring')); GO
In the same way, you can negate the conditions individually. You can also negate the whole statement. In this case, after WHERE, type NOT() and, in the parentheses, include the logical conjunction or disjunction.
Practical Learning: Negating a Condition |
Combining Conditions |
The SQL allows you to create statements as complex as you want by combining the logical conjunctions and/or disjunctions.
To visually combine conditions, you have various options. If you are combining somewhat independent conditions, type each in the desired Filter or Or... box. If you are creating logical disjunctions that must be evaluated in groups, type each disjunctions in it own Filter box
Using SQL, you can create as many conditions as possible. Here is an example:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE (Gender = N'male') AND (City = N'silver spring') AND (LastName LIKE N'%n'); GO
If the statement contains only conjunctions, the interpreter would examine the first condition:
The interpreter follows these steps for as many conjunctions as are included in the statement. If the statement contains only disjunctions, the approach is logically different. The interpreter would examine the first condition:
The interpreter follows these steps for as many conjunctions as are included in the statement.
You can create a statement that is a combination of logical conjunctions and disjunctions. To make the statement easier to read and because of precedence rules, you should group the conditions in parentheses. Consider the following example:
SELECT StudentNumber [Student #], FirstName [First Name], LastName "Last Name", Gender, City, ParentsNames [Parents Names] FROM Registration.Students WHERE ((Gender = N'female') AND (City = N'silver spring')) OR ((Gender = N'male') AND (City = N'bethesda')); GO
This statement is essentially one main logical disjunction that checks two logical conjunctions. The first conjunction asks the interpreter to get a list of girls who live in Silver Spring. The second conjunction wants a list of boys who live in Bethesda. In reality, the clerk wants a list of girls who live in Silver Spring and boys who live in Bethesda:
In the same way, you can create complex conditions.
Practical Learning: Combining Conditions |
SELECT ItemNumber AS [Item #],
Manufacturer,
Category,
SubCategory AS [Sub-Category],
ItemName AS [Name/Description],
UnitPrice AS [Unit Price],
DiscountRate AS [%Discount]
FROM Inventory.StoreItems
WHERE (Manufacturer LIKE N'%lauren%') AND (DiscountRate IS NOT NULL)
OR
(Manufacturer LIKE N'%klein%') AND (DiscountRate IS NOT NULL)
USE LambdaSquare1;
GO
SELECT "Unit #" = aparts.UnitNumber,
Beds = aparts.Bedrooms,
Baths = aparts.Bathrooms,
[Monthly Rent] = aparts.Price,
"Primary Deposit" = aparts.Deposit,
(aparts.Price + aparts.Deposit) "Due Before Moving",
CASE aparts.Available
WHEN 0 THEN N'No or Not Yet'
WHEN 1 THEN N'Yes'
END "Available"
FROM Presentation.Units aparts
WHERE (aparts.UnitNumber < 200) AND
(aparts.Bedrooms = 1) AND
(aparts.Available = 1)
ORDER BY [Monthly Rent], "Primary Deposit";
GO
|
||
Previous | Copyright © 2008-2022, FunctionX, Inc. | Next |
|