WEBVTT

1
00:00:00.300 --> 00:00:03.060
Let's select everything
from our books table again.

2
00:00:03.060 --> 00:00:07.740
We have an ID, which is a unique
identifier for the database entry.

3
00:00:07.740 --> 00:00:12.590
This is a common practice and guarantees
that every row is not a duplicate of

4
00:00:12.590 --> 00:00:16.910
another, and can be isolated
from all other rows if needed.

5
00:00:16.910 --> 00:00:22.820
We have the title, author,
genre, and first_published.

6
00:00:22.820 --> 00:00:26.140
Remember, querying a table's
contents allows us to present

7
00:00:26.140 --> 00:00:29.030
the results of a database
in many different ways.

8
00:00:29.030 --> 00:00:32.070
The results are designed for
humans to read.

9
00:00:32.070 --> 00:00:35.270
But sometimes the column
names aren't that friendly.

10
00:00:35.270 --> 00:00:38.980
Let's say I was running a query for
the library administrator.

11
00:00:38.980 --> 00:00:42.624
They want a report of all the books in
the database along with the year that they

12
00:00:42.624 --> 00:00:43.759
were first published.

13
00:00:47.642 --> 00:00:50.870
The column headers are all in lower case.

14
00:00:50.870 --> 00:00:53.140
This may be distracting
to the report reader.

15
00:00:53.140 --> 00:00:58.190
We can change the title of any column,
by using the keyword AS.

16
00:00:58.190 --> 00:01:02.770
After the AS, you can write what you
want to alias the column name to.

17
00:01:02.770 --> 00:01:05.370
Let's use Title with an uppercase T.

18
00:01:06.530 --> 00:01:10.340
When we run the query now, we get
the results back with a capital T

19
00:01:10.340 --> 00:01:12.300
in the title column.

20
00:01:12.300 --> 00:01:15.551
How about the first_published column?

21
00:01:15.551 --> 00:01:20.970
We can use the AS keyword again but
with the words First Published.

22
00:01:23.570 --> 00:01:26.178
If you try and execute this query,
you'll get an error.

23
00:01:26.178 --> 00:01:29.240
Error: near "Published": syntax error.

24
00:01:29.240 --> 00:01:33.210
The key for
us here is that it's a syntax error.

25
00:01:33.210 --> 00:01:35.420
Remember the syntax is the grammar or

26
00:01:35.420 --> 00:01:39.140
the rules surrounding the way
we write programming code.

27
00:01:39.140 --> 00:01:42.500
The computer doesn't
understand the word Published.

28
00:01:42.500 --> 00:01:47.890
It's expecting a FROM or a comma,
followed by another column name.

29
00:01:47.890 --> 00:01:51.380
We can't use multiple words like
this when writing SQL queries.

30
00:01:52.570 --> 00:01:57.980
So what do we do for something such as
our first_published alias, in our reports?

31
00:01:57.980 --> 00:02:00.510
There is a way we can
use some punctuation,

32
00:02:00.510 --> 00:02:03.910
which is part of the SQL syntax,
to solve this problem.

33
00:02:03.910 --> 00:02:10.407
We can define column names with spaces
by wrapping them in quotes like this.

34
00:02:10.407 --> 00:02:13.601
The opening quote is just
before the first letter, and

35
00:02:13.601 --> 00:02:16.040
the closing quote after the last letter.

36
00:02:17.740 --> 00:02:19.380
But here's another got ya.

37
00:02:19.380 --> 00:02:22.840
Writing SQL in different
databases like Microsoft SQL, or

38
00:02:22.840 --> 00:02:28.610
PostgreSQL my require you to use different
punctuation for writing aliases.

39
00:02:28.610 --> 00:02:32.870
Some require single quotes,
others even require square brackets.

40
00:02:32.870 --> 00:02:38.360
The SQL we're using in SQL Playground can
use double quotes, but just be mindful.

41
00:02:38.360 --> 00:02:43.700
SQL in different settings may have
these slight variations in syntax.

42
00:02:43.700 --> 00:02:47.512
Most database documentations have help
to guide you around these difficulties.

43
00:02:47.512 --> 00:02:51.360
When we execute this query now,
we see Title and

44
00:02:51.360 --> 00:02:55.590
First Published with a space
as the column titles.

45
00:02:55.590 --> 00:02:59.973
There is a short hand for this syntax,
where you remove the AS keyword.

46
00:03:02.749 --> 00:03:05.672
You can try it too.

47
00:03:05.672 --> 00:03:06.472
See?

48
00:03:06.472 --> 00:03:08.320
It's the same results.

49
00:03:08.320 --> 00:03:11.440
I avoid this syntax because
it's not very clear.

50
00:03:11.440 --> 00:03:12.740
I just wanted to bring you up so

51
00:03:12.740 --> 00:03:15.730
you weren't confused if
you saw it somewhere else.

52
00:03:15.730 --> 00:03:19.960
To recap, to alias a column name
to something of your choosing,

53
00:03:19.960 --> 00:03:25.280
type SELECT &lt;column name&gt; the keyword AS,
followed by the new name.

54
00:03:27.020 --> 00:03:32.230
So, if I were to bring back all patron
names in a more user-friendly format,

55
00:03:32.230 --> 00:03:34.080
I could do something like this.

56
00:03:34.080 --> 00:03:39.048
With quotes around the first name and
last name.

57
00:03:42.860 --> 00:03:46.430
These characters surrounded by
quote marks are called strings,

58
00:03:46.430 --> 00:03:48.450
meaning strings of characters,

59
00:03:48.450 --> 00:03:52.310
letters, numbers,
special characters and spaces.

60
00:03:52.310 --> 00:03:55.070
One final note about the AS keyword.

61
00:03:55.070 --> 00:03:59.710
When you bring back data with
a new label using the AS keyword

62
00:03:59.710 --> 00:04:03.370
it does not change the name of
the column in the actual database.

63
00:04:03.370 --> 00:04:07.680
For example, when we aliased our
first_published column

64
00:04:07.680 --> 00:04:10.770
as First Published in our report.

65
00:04:10.770 --> 00:04:14.380
The column name is always first_published.

66
00:04:14.380 --> 00:04:18.880
Only in our report do we see the column
represented as First Published.

67
00:04:18.880 --> 00:04:22.220
This is why we call it an alias.

68
00:04:22.220 --> 00:04:27.230
As an aside, the AS keyword can
be used to alias table names for

69
00:04:27.230 --> 00:04:30.910
more complex queries working
with multiple tables.

70
00:04:30.910 --> 00:04:33.060
We'll be covering that in another course.
