So we're moving onto 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 a 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 the 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 me and give me all the columns in the cats 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 Heidi name age and breed we want all of them coming back to us.
So let's do that now.
Select a 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.
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 it home here we can also do a select let's do cat ID
from cats and we get one two three four five six seven.
OK.
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 post comes in.
So this whole thing is the select expression where saying select name and age from cat 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 common breed which would be the same thing.
So like 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 Heidi common name comma age from cats and we hit
enter and we get those three fields.
Notice we're missing breed but we could just go at that and if we wanted
and noticed that the order matters.
So I said can I do the 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 we could reverse it if I wanted to I select age then breed then name it then
Cat ID from cats.
And there we go.
OK.
So that's pretty much all there is to this 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.