Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views21 pages

Logic Operators (Slides)

Uploaded by

oseyikay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Logic Operators (Slides)

Uploaded by

oseyikay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Querying data

Logic operators
Please do not copy without permission. © ALX 2024.
Logic operators

Logic operators

| Logic (or boolean) operators combine, exclude, or negate conditions in order to evaluate
the overall truth of a condition or a set of conditions.

AND combines two conditions and is only TRUE if IN combines several OR operators. It returns TRUE if
both conditions are TRUE. a value is within a list of possible values.

OR combines two conditions and is only TRUE if BETWEEN combines the > and < operators. It
either condition is TRUE. returns TRUE if a value is within a specified range.

NOT reverses the truth of a condition. TRUE LIKE matches a string to a pattern. It returns TRUE
becomes FALSE and FALSE becomes TRUE. if a string matches the search pattern.
2
Logic operators

OR

| The OR operator is used to filter records based on multiple conditions. If at least one of the
specified conditions is TRUE, the record will be included in the results set.

Syntax: … WHERE condition1 OR condition2;

Table_1 Results
SELECT
* col1 col2 col3 col1 col2 col3

FROM x 34 s TRUE x 34 s
db.Table_1 y 73 m TRUE y 73 m
WHERE OR
col2 >= 25
z 22 l FALSE
OR col3 = “m”; w 12 m TRUE w 12 m

Rows that meet any of the conditions (col2 ≥ 25 or col3 = “m”) are included.

3
Logic operators

Multiple OR conditions
OR ● More than two OR statements can be combined.

● Rows that meet any of the specified conditions are


SELECT
* included.
FROM
db.Table_2 ● Rows are included in the results if col1 = car or
WHERE when col2 > 60 or col3 = l.
col1 = “car” -- Condition 1
OR col2 < 60 -- Condition 2
OR col3 = “l”; -- Condition 3

car matches the first condition, so the row is included.

Table_2 Results
col1 col2 col3 col1 col2 col3
1 and l meet conditions 2 and 3, so the row is included.
car 68 s car 68 s

cat 1 l cat 1 l 7 and l meet conditions 2 and 3, so the row is included.


pet 7 l pet 7 l

cart 56 m cart 56 m
56 meets condition 2, so the row is included.

4
Logic operators

AND

| The AND operator is used to filter records based on more than one condition. All conditions
connected by an AND clause must be TRUE for the record to be included in the results.

Syntax: … WHERE condition1 AND condition2;

Table_1 Results
SELECT
col1 col2 col3 col1 col2 col3
* FALSE
FROM x 34 s
db.Table_1 TRUE
y 73 m y 73 m
WHERE AND FALSE
z 22 l
col2 >= 20 FALSE
w 12 m
AND col3 = “m”;

Only rows that are TRUE for both conditions (col2 ≥ 20 AND col3 = “m”) are included.

5
Logic operators

Multiple AND conditions


AND ● More than two AND statements can be combined.

● Rows that meet all of the conditions are included.


SELECT
* ● Rows are only included in the results if col1 = cat
FROM and col2 > 0 and col3 = l.
db.Table_2
WHERE
col1 = “cat” -- Condition 1
AND col2 > 0 -- Condition 2
AND col3 = “l”; -- Condition 3

Table_2 Results
col1 col2 col3 col1 col2 col3
Only one row meets all conditions.
car 68 s

cat 1 l cat 1 l

pet 7 l
cat meets condition 1
cart 56 m
AND 1 meets condition 2,
AND l meets condition 3, so the row is included.

6
Logic operators

IN

| IN is used to check if a value in a column matches any value in a list.

Syntax: … WHERE col IN (value1, value2, ...);

SELECT
* Table_1 Results
FROM
db.Table_1 col1 col2 col3 col1 col2 col3

WHERE x 34 s x 34 s
col1 y 73 m IN y 73 m
IN(
z 22 l
“w”,
“x”, w 12 m w 12 m

“y”
);

col1 IN(“w”, “x”, ”y”) is a shortcut for: (col1 = “x” OR col1 = “y” OR col1 It is better to use IN when checking
= “w”). multiple OR statements.

7
Logic operators

BETWEEN

| The BETWEEN operator is used to filter records within a specific range, inclusive of the range
endpoints.

Syntax: … WHERE col BETWEEN value1 AND value2;

Table_1 Results
SELECT
col1 col2 col3 col1 col2 col3
*
FROM x 34 s
BETWEEN x 34 s

db.Table_1 y 73 m 22 AND 50
WHERE z 22 l z 22 l
col2 BETWEEN 22 AND 50;
w 12 m

● Rows where col2 is between 22 and 50 are included. BETWEEN makes SQL code more
readable, so always try to use it
● Rows where col2 is outside this range are excluded.
when specifying ranges.
8
Logic operators

IS NULL

| IS NULL is used to check whether a value is NULL or missing, essentially helping to identify
gaps in the data.

Syntax: … WHERE col IS NULL;

Table_3 Results
SELECT col1 col2 col3 col1 col2 col3
* x NULL s x NULL s
FROM
db.Table_3
x 42 s IS
WHERE z NULL s NULL z NULL s

col2 IS NULL; x 7 s

● Includes only rows where there are NULL values in the specified column. NULL values often create fallacies,
so it is best to know about any NULL
● To check multiple columns for NULL values, we can use “OR col3 IS NULL” etc.
values in a column.
9
Logic operators

NOT and BETWEEN

| NOT is used to negate a condition. NOT BETWEEN, for example, excludes a specific range of
values.

Syntax: … WHERE col NOT BETWEEN value1 AND value2;

Table_1 Results
SELECT col1 col2 col3 col1 col2 col3
* x 34 s
FROM
y 73 m BETWEEN y 73 m
db.Table_1
22 AND 50 NOT
WHERE z 22 l

col2 NOT BETWEEN 22 AND 50; w 12 m w 12 m

● 34 and 22 both satisfy the BETWEEN condition, and NOT reverses the outcome, so
34 and 22 are now FALSE, and those rows are excluded.
NOT complicates SQL logic, so the
● 12 and 73 evaluate to FALSE in the BETWEEN condition, and are reversed by NOT code becomes less readable. Use
to TRUE, so those rows are included. NOT sparingly.

10
Logic operators

IS NOT NULL

| The IS NOT NULL operator checks to see if a value is not null/empty, helping to confirm
when data do indeed exist.

Syntax: … WHERE col IS NOT NULL;

Table_3 Results
SELECT col1 col2 col3 col1 col2 col3

* x NULL s
IS NOT
FROM
db.Table_3
x 42 s
NULL x 42 s

z NULL s
WHERE
col2 IS NOT NULL; x 7 s x 7 s

We can use IS NOT NULL to


Includes only rows where there are no NULL values in the specified column.
remove any rows with missing data.

11
Logic operators

NOT and IN

| NOT IN is used to ensure a value does not match any value in a list. The outcome of IN is
reversed by NOT.

Syntax: … WHERE col NOT IN (value1, value2, ...);

SELECT Table_1 Results


*
FROM col1 col2 col3 col1 col2 col3

db.Table_1 x 34 s
WHERE
y 73 m
IN NOT
col1
IN( z 22 l z 22 l
“w”, w 12 m
“x”,
“y”
);

● NOT IN reverses IN, so rows where col1 = (w, x, y) are excluded.


● z is NOT IN the list of options, so the row is included.

12
Logic operators

SQL text searching

Databases house an overwhelming amount of


text-based data, including names, addresses,
descriptions, and categories.

The LIKE operator in SQL is our key tool for


navigating this textual labyrinth, allowing
targeted searches within this data using
wildcards to tune our searches.

For instance, a humanitarian aid worker could


use it to quickly locate all NGOs with names that
are related to water within a massive database
using LIKE.

13
Logic operators

Searching text in SQL

| LIKE is used in a WHERE clause to search for a specified pattern in a text-based column.
These patterns can be expressed using wildcards.

Wildcards are symbols that can represent any character(s) (a-z, A-Z, 0-9), and even symbols, enabling a
pattern-based search with the LIKE operator. There are two wildcards in SQL – underscore (_) and percentage (%).

Underscore (_) Percentage (%)

Represents a single character. Represents multiple characters.

A search pattern like h_t will match with values like A search pattern like South% will match with values like
hot, hat and hit, but would not match with heat South Korea, South Africa, Southern, or
because _ specifies a single character. Southern#1594 since it can represent any number of
characters.

14
Logic operators

Wildcards

| The placement of wildcards in the search pattern provides even more search flexibility.

% at the end: Matches any string starting with the given p% %t _at
characters, for example, p% must start with p, be any
length, and can end with any character.
car FALSE FALSE FALSE

% at the start: Matches any string ending with the given cat FALSE TRUE TRUE
characters, for example, %t can start with any
character, can be any length, but must end with t.
pet TRUE TRUE FALSE

pat TRUE TRUE TRUE


_ in place of one character: Matches any single
character in that position, for example, _at must
contain only three characters and end with t. cart FALSE TRUE FALSE

15
Logic operators

Wildcards
% inside: Matches any string that begins and ends with
the given characters, and can be any length, for
example, c%t must start with c, can contain any c%t _a_ _a%
number of characters, and must end with t.
car FALSE TRUE TRUE

Wildcards can be combined:


cat TRUE TRUE TRUE
_ at both ends: Matches any string containing the given
characters, three characters long, for example, _a_ must be
three characters long and can start and end with any pet FALSE FALSE FALSE
character, but must have an a in the middle.
pat FALSE TRUE TRUE

cart TRUE FALSE TRUE


% and _: Using both % and _ we can limit strings further, for
example, _a% matches with cat and cart. _a% can start with
any single character that must be followed by an a and can
end with any number of characters.

16
Logic operators

LIKE

| Only rows that match the LIKE search pattern (in the specified column) are included in
the results.

Syntax: … WHERE col LIKE “pattern + wildcard”;

Table_2 Results
SELECT
col1 col2 col3 col1 col2 col3
*
FROM car 68 s
LIKE car 68 s

db.Table_2 cat 1 l _a% cat 1 l


WHERE pet 7 l
col1 LIKE “_a%”; cart 56 m cart 56 m

Only rows that match the search ● pet does not contain an a, so the row is excluded.
pattern (in the specified column)
● car, cat and cart match _a% because % can be r, t, or rt.
are included in the results.
17
Logic operators

Order of operations

| Operations in parentheses () are evaluated first, then AND, and lastly, OR is evaluated.

Suppose we have this combination of conditions:


1
1 2 3
Final
OR AND
2
OR
There are no (), so we evaluate AND first:
Outcome AND
Outcome
AND = 3

Then we evaluate OR using the outcome of AND :


Final

Keep the order of operations in mind when using


OR =
AND and OR together.

18
Logic operators

Order of operations

| Parentheses () can interrupt the order of operations.

Suppose we have this combination of conditions:


1 2 3 1
Outcome Final
OR AND
OR AND

We evaluate the OR inside the () first: 2


1 2 Outcome

OR =
3

Then we evaluate AND using the outcome of OR :


3 Final
By using parentheses, we can alter the order in
AND = which conditions are checked. Using this we create
complex logic in SQL to search for data using WHERE.

19
Logic operators

Order of operations using WHERE

SELECT Table_1 Results


* col1 col2 col3 col1 col2 col3
FROM C C
db.Table_1
x 34 s OR x 34 s
D D
WHERE y 73 m y 73 m
A A
col1 = “w” z 22 l z 22 l
OR col1 = “x” B AND B
w 12 m w 12 m
OR col1 = “y”
OR col1 = “z”
AND col2 > 20;

This condition includes a row where: A


col1 = z AND col2 > 20 is TRUE.

SQL will execute AND first, then OR , This condition includes a row where: B A row is included if it is
so the query that SQL executes is: col1 = w is TRUE. TRUE for:

A OR B OR C OR D
WHERE C
This condition includes a row when:
col1 = “z” AND col2 > 20 col1 = x is TRUE.
OR col1 = “w”
OR col1 = “x”
This condition includes a row when: D
OR col1 = “y”;
col1 = y is TRUE.
20
Logic operators

Using parentheses with WHERE


SELECT
* Table_1 Results
FROM OR
db.Table_1 col1 col2 col3 col1 col2 col3
C C AND E
WHERE ( x 34 s x 34 s
col1 = “w” D D AND E
y 73 m y 73 m
OR col1 = “x” A E A AND E
z 22 l AND z 22 l
OR col1 = “y” B AND E
B
OR col1 = “z” w 12 m
)
AND col2 > 20;

This condition includes a row where: A


col1 = z is TRUE.

This condition includes a row where: B


SQL will evaluate the contents
col1 = w is TRUE.
of () first, then AND : A row is only included if
… This condition includes a row when: C both:
WHERE col1 = x is TRUE. (A, B, C, OR D) are TRUE
(col1 = “z” AND E is also TRUE.
OR col1 = “w” This condition includes a row when: D
OR col1 = “x” col1 = y is TRUE.
OR col1 = “y”)
This condition includes a row when: E
AND col2 > 20 ;
col2 > 20 is TRUE.
21

You might also like