text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
in our database?
3,623.74
0.85
Let's try that one out too and see how that works with ASC
3,624.59
3.2
for ascending and DESC for descending.
3,627.79
2.97
So I'll go back to my terminal.
3,630.76
1.59
And I'll demonstrate here how we can use this for some text.
3,632.35
3.36
So let's try to simply sorting our books alphabetically for, let's say,
3,635.71
5.07
our library.
3,640.78
1.02
I'll say SELECT "title" from "longlist," Enter.
3,641.8
5.1
And I want to order by title, just plain and simple.
3,646.9
3.31
And then hit Semicolon.
3,650.21
1.38
Let's see what happens.
3,651.59
1.16
I'll hit Enter.
3,652.75
1.41
And now I'll see that these books are ordered.
3,654.16
3.54
But they seem to be ordered alphabetically.
3,657.7
2.28
So here, we have some titles lower in the alphabet.
3,659.98
3.72
And up here, we have titles earlier in the alphabet.
3,663.7
2.62
So by default ORDER BY seems to order alphabetically.
3,666.32
4.46
If I change that default, though, from ascending to descending,
3,670.78
4.29
let's see what happens.
3,675.07
1.89
I'll go back over here.
3,676.96
1.29
And I'll try the same query but now using DESC.
3,678.25
4.74
SELECT "title" from "longlist."
3,682.99
1.77
ORDER BY "title" now in descending order.
3,684.76
4.35
Hit Enter.
3,689.11
1.29
And now I'll see these titles in reverse alphabetical order.
3,690.4
4.23
So notice how earlier on, we have titles that are lower in the alphabet.
3,694.63
4.56
But down below, we have titles that are earlier in the alphabet here.
3,699.19
3.55
So you can use ORDER BY with these texts.
3,702.74
3.59
But you then have to specify whether you want it in alphabetical order
3,706.33
3.36
or in reverse alphabetical order.
3,709.69
4.27
OK, so let's show a few other concepts here
3,713.96
4.74
we can use alongside of these orderings.
3,718.7
3.04
One thing we could also do is try to find more information
3,721.74
4.34
about the ratings of these books.
3,726.08
1.77
So let's say I want not just to order these books
3,727.85
3.12
but try to find the average rating, or to try to find the number of books,
3,730.97
4.74
or try to find let's say maybe the sum of my total votes on each of my books.
3,735.71
5.37
Well, for this, we could introduce some new concepts,
3,741.08
3.78
these ones called SQL's aggregate functions.
3,744.86
4.2
These allow us to take a whole set of rows and return not each of those rows
3,749.06
6.33
individually.
3,755.39
1.17
But instead, in this case, one number based on the values in those rows.
3,756.56
5.73
You could imagine trying to count the number of rows you have
3,762.29
3.06
or take the average of the number of rows,
3,765.35
2.61
or take the average of let's say a rating, for instance.
3,767.96
2.67
Finding the minimum rating of the maximum rating
3,770.63
2.07
or finding the sum of some votes.
3,772.7
2.49
And we'll see each of these in action here.
3,775.19
3.04
Let's go back to our terminal, try some of these out.
3,778.23
3.23
I will try, in this case first trying to find
3,781.46
4.26
the average rating from my longlist.
3,785.72
2.9
Well, I just from experience, and as you now know too,
3,788.62
3.43
I can try to find the average of some column
3,792.05
2.82
by using the AVG aggregate function.
3,794.87
3.52
So I'll say SELECT not just rating in this case
3,798.39
4.58
but select the average rating FROM "longlist."
3,802.97
4.95
Notice how in this case, I'm using this kind of syntax, where I take rating,
3,807.92
5.64
my column I want to aggregate or to sum up or to average like this.
3,813.56
4.77
And I apply the function by saying its name
3,818.33
3.18
followed by some parentheses around that column name.
3,821.51
3.67
So this will return to me not all of the rating
3,825.18
2.45
rows but the average of the rating rows in one single cell.
3,827.63
4.8
Let me try this.
3,832.43
1.65
I'll come back, and I will then hit Enter.
3,834.08
2.91
And I'll see this is the average rating.
3,836.99
3.45
We have 3.7537179471795 is our average rating for all of these books.
3,840.44
8.28
But of course, this isn't great.
3,848.72
3.06
What might I want to do if I was going to show this to somebody else?
3,851.78
4.14
I could probably improve the presentation of this in some way.
3,855.92
5.02
So I could probably round this result. I have 3.75371,
3,860.94
4.7
we can probably stop after two decimal points, right?
3,865.64
2.88
Just simply like 3.75.
3,868.52
2.1
So I could introduce some new keyword here, this one around the results.
3,870.62
4.71
Let me show you this one in action.
3,875.33
2.52
I'll come back, and I'll try not just select average rating
3,877.85
3.69
but select the rounded average rating.
3,881.54
2.83
So I'll say SELECT ROUND and then take average of "rating" and round
3,884.37
7.55
to 2 decimal points FROM "longlist" Semicolon.
3,891.92
6.3
So now this query decides to first find the average of the rating column.
3,898.22
6.69
Then take the result and round it using two decimal points.
3,904.91
5.95
Notice how round takes two inputs or two arguments, the first one
3,910.86
3.65
being the rating, the average rating, the second one being number
3,914.51
3.99
of decimal points to round, 2.
3,918.5
1.8
And we complete our query in the way we usually do by saying FROM this table.
3,920.3
4.33
So let's try this one to figure this out.
3,924.63
2.24
I'll come back.
3,926.87
0.75
And I'll hit Enter.
3,927.62
1.5
And now I'll see we do get back 3.75.
3,929.12
3.99
But there is still one thing to improve here.
3,933.11
2.61
When I write this query, I see this ugly title name--
3,935.72
3.87
ROUND average "rating" comma 2.
3,939.59
2.4
I wouldn't send this to my boss or somebody else who I
3,941.99
2.785
work for or maybe even a friend, right?
3,944.775
1.625
I want to make sure it's pretty so they can read it correctly.
3,946.4
2.77
So what could I do then to try to make this prettier?
3,949.17
4.18
I could maybe rename this column.
3,953.35
1.74
I could try to take this and make it not just this ugly mess of SQL
3,955.09
4.59
keyword but to give it some name I could use instead.
3,959.68
3.13
So for this, we'll introduce a brand new one--
3,962.81
2.36
new brand new keyword called AS.
3,965.17
2.13