So we're moving on to the R of crud, which is read how do we retrieve and search data from a database?
And we've actually already seen this.
The magic command is select.
So remember when we were doing this earlier, select star from Cats?
I told you not to worry about it too much because we'd spend more time on it.
And it was really just something that we needed to have to check our work at at that time when we were
inserting data, but we didn't actually talk about how it works, what the star means.
That's what we're going to do now.
So the star in select star from Cats means give me all the columns in the cat's table.
So we're asking for the Cats table.
That's the from cats.
And in that table we just want everything that we have.
So in our case that means cat ID, name, age and breed.
We want all of them coming back to us.
So let's do that now.
Select star from Cats.
You can see we get cat ID, name, breed and age and we also get every single cat no one is missing.
These are the seven that we inserted.
So that's the basics of Select Star, which you'll use often just to check things out.
But we can also get more specific about what data we want back, which is where the select expression
comes in.
Basically, there's a way for us to specify what columns we want.
In particular, I only want the ID or I only want cats names and age, but not the breed.
So to do that, rather than saying Select Star, we can do things like select name from cats.
And if we do that select name from Cats, what do you think the table will look like?
It looks like this we only get name, so it gives us all seven names, but nothing else.
We could also do.
Select Age from Cats.
And hopefully this is what you expected.
We just get a bunch of ages and just to hammer at home here, we could also do a select let's do cat
ID from cats and we get one, two, three, four, five, six, seven.
So that's useful enough.
But oftentimes we want more than one piece of data, but we don't want everything.
And that's where this comma separated list comes in.
So this whole thing is the select expression.
We're saying select name and age from cats.
So we can try it now.
Select name, comma, age from cats.
And what do you think?
We'll see.
So we get a table with name and age returned back.
So there's no limit on that.
We could do name, age, comma, cat ID, comma, breed, which would be the same thing as select star.
We could do name, age and breed or cat ID and name and age and so on.
I think you get it.
We can mix and match whatever fields we want back.
So just to illustrate my point, let's do a select cat ID, comma, name, comma, age from cat and
we hit enter and we get those three fields notice we're missing breed, but we could just go add that
in if we wanted
and notice that the order matters.
So I said cat ID, then name, then age, then breed.
And that's what we get.
But if I just did a select star,
it uses the default order, which is cat ID, name, breed and then age, which is just how we entered
it when we created the table.
So I could reverse it if I wanted to by select age, then breed, then name, then cat ID from cats
and there we go.
So that's pretty much all there is to the select expression for now at least, where we're just specifying
exactly which columns we want or we can default and use the star which will give us all columns.