Next up will see a pair of built in functions.
They go together, Min and Max.
They're sort of relatively self-explanatory.
Min helps us find the minimum value in a certain column, but also as we'll see shortly, within a certain
group.
And then Max is the opposite.
It's going to find the maximum values.
So we're going to start by just taking a look at how these work without grouped by in the same way that
we saw.
We can just do select count without any grouping from books, for example, or count the distinct authors
or something.
There's no group buy, but then we could also use count with a group.
We're going to start by doing Min and Max without any grouping.
So the first thing we'll look at is how we could do something like this.
Find the minimum release here, aka the earliest year a book was published in our table.
So we can just do select min released year.
From books.
And there we are, 1945.
I believe that's a Steinbeck.
But in a little bit we're going to learn how we could figure out exactly what row that belongs to.
We could also do things like find the longest book well, at least find the most number of pages, select
max of pages from books.
So there's some book that has 634 pages.
But again, we don't know exactly which road that belongs to.
What's the title of that book?
We'll get there, but this works to find the max.
That's what I need.
What about with text?
We can also do select.
Let's do min author l name and I can do multiple of these together as well.
Show that in just a moment.
Select min.
Let's do from books.
Obviously that's our only table right now and it does work alphabetically.
It will find the lowest in the alphabet.
Author last name, which is Carver and I could add on Max author l name if I wanted to.
And now we're finding the lowest or the earliest alphabetically and the latest alphabetically.
Author last name in our table, Carver and Steinbeck.
So you can use it on text.
I don't really do that often though.
Usually I use Min and max for numerical values.
And that now brings us to the question of what if I want to know the title of the longest book?
We know how to find the longest book.
We did it well.
The number of pages select maximum pages from the entire table of books, and it just gives me the number.
How do I know which book that belongs to?
Well, what if we tried this?
Select Max pages.
Comma.
Hey.
Go away.
Comma.
Title from books.
Well, we have another one of these errors that we saw earlier.
At the end of the previous video.
I talked about how whenever we aggregate things, we can only work with the data that they have in common.
And in this case, we didn't aggregate ourselves.
We didn't use Group VI.
But behind the scenes, there's an aggregated query.
I mean, this is an aggregation function and essentially it doesn't allow us to do what I'm trying right
here.
It's saying which title.
It's looking at all the pages, it's aggregating, it's finding the max, but it doesn't know which
title to display because it only gives us a single value back.
It's not smart enough or it's not set up in such a way to give us the title of that book.
But that doesn't mean we can't get it.
We just have to do some extra work and we'll talk about that in the next video.