1
00:00:00,290 --> 00:00:05,100
SQL isn't only used for finding or
filtering on exact matches,
2
00:00:05,100 --> 00:00:09,850
it can be used to compare values
using relational operators.
3
00:00:09,850 --> 00:00:12,900
The name of relational
operators comes from their
4
00:00:12,900 --> 00:00:16,890
usage in comparing the relationship
between two values,
5
00:00:16,890 --> 00:00:19,540
not because they're used
in a relational database.
6
00:00:19,540 --> 00:00:23,380
These operators are actually found in
many of the programming languages,
7
00:00:23,380 --> 00:00:24,810
not just SQL.
8
00:00:24,810 --> 00:00:28,210
So let's take a look at
the operators we already know.
9
00:00:28,210 --> 00:00:33,500
The equality or equal to operator
compares the quality of two values.
10
00:00:33,500 --> 00:00:37,850
The text of Andrew is the same
as the text of Andrew.
11
00:00:37,850 --> 00:00:39,860
So this condition is true.
12
00:00:39,860 --> 00:00:43,620
The inequality operator, or
the not equal to operator,
13
00:00:43,620 --> 00:00:48,350
compares the values, and if they
don't match, the condition is true.
14
00:00:48,350 --> 00:00:54,130
The text of Andrew is not the same as
the text of Lauren, so this is true.
15
00:00:54,130 --> 00:00:56,760
Now onto the relational operators.
16
00:00:56,760 --> 00:01:00,570
You tend to see these operators
when comparing numbers and dates.
17
00:01:00,570 --> 00:01:02,940
We're going to focus on numbers first.
18
00:01:02,940 --> 00:01:06,460
The first one we're going to look
at is the less than operator,
19
00:01:06,460 --> 00:01:09,690
which is represented by
a left angle bracket.
20
00:01:09,690 --> 00:01:12,160
All operators read from left to right.
21
00:01:12,160 --> 00:01:16,460
If the value on the left-hand side of
the operator is smaller than the value on
22
00:01:16,460 --> 00:01:21,110
the right side of the operator,
the condition will evaluate to true.
23
00:01:21,110 --> 00:01:26,800
If the two values are equal to each other,
the condition will evaluate to false,
24
00:01:26,800 --> 00:01:30,260
because the left value is not
smaller than the right value.
25
00:01:30,260 --> 00:01:36,510
In this example, all values less than 40,
but not including 40, are true.
26
00:01:36,510 --> 00:01:40,220
If you wanted to compare values
less than or equal to each other,
27
00:01:40,220 --> 00:01:44,070
you'd use the less than operator,
followed by the equals sign.
28
00:01:44,070 --> 00:01:48,040
This is known as the less than or
equal to operator.
29
00:01:48,040 --> 00:01:50,780
This is just like the less than operator,
but
30
00:01:50,780 --> 00:01:54,290
it is inclusive of the value
on the right hand side.
31
00:01:54,290 --> 00:01:58,380
How about creating values
greater than a particular value?
32
00:01:58,380 --> 00:02:02,730
Well, we use the right angle bracket or
the greater than symbol.
33
00:02:02,730 --> 00:02:09,040
100 is greater than 40 and 99,
but is not greater than 100.
34
00:02:09,040 --> 00:02:13,020
However, 100 is greater than or
equal to 100.
35
00:02:13,020 --> 00:02:18,056
Let's use these new operators to answer
some more questions from our database.
36
00:02:18,056 --> 00:02:22,995
Let's start with the question
what books are in the library
37
00:02:22,995 --> 00:02:26,660
that were first published after 2005?
38
00:02:26,660 --> 00:02:28,946
Let's start with our
general SQL statement.
39
00:02:28,946 --> 00:02:34,800
Select star from books.
40
00:02:34,800 --> 00:02:38,992
Then let's add a where clause,
41
00:02:38,992 --> 00:02:45,760
first published is greater than 2005.
42
00:02:45,760 --> 00:02:52,960
Notice how Harry Potter and
the Half Blood Prince wasn't selected,
43
00:02:52,960 --> 00:02:55,745
since it's first published
year was in 2005.
44
00:02:55,745 --> 00:03:01,530
2005 isn't greater than itself,
it is equal to itself.
45
00:03:01,530 --> 00:03:06,710
If we wanted to include books from 2005
too, we should use the greater than or
46
00:03:06,710 --> 00:03:11,850
equal to operator to get all
books from 2005 onwards.
47
00:03:11,850 --> 00:03:14,920
Finally, let's find some
classics in our library.
48
00:03:14,920 --> 00:03:18,540
Let's find all books released
before the 20th century.
49
00:03:18,540 --> 00:03:19,820
How would you change this query?
50
00:03:21,770 --> 00:03:22,310
That's right.
51
00:03:23,890 --> 00:03:29,490
We would change the greater than or
equal to operator to be the less than
52
00:03:29,490 --> 00:03:35,879
operator and the value 1,900.
53
00:03:35,879 --> 00:03:37,020
Nice.
54
00:03:37,020 --> 00:03:40,910
Obviously, 1900 is the beginning
of the 20th Century.
55
00:03:40,910 --> 00:03:46,420
But if we wanted to include 1900, we'd
use the less than or equal to operator.
56
00:03:47,590 --> 00:03:50,980
Our results are the same since we have
no books in our library from 1900.
57
00:03:50,980 --> 00:03:54,480
With these comparison
operators under your belt,
58
00:03:54,480 --> 00:03:57,850
you can now filter your
data in more exciting ways.