Now to fetch data that's inside a table,
we can use the select command.
For this all, start creating a new SQL statement
by clicking this icon again,
hence I got this new tab.
And then in there,
to get the data that's stored in a table,
we use the select keyword
to run a command that selects data from a table.
Now the select keyword or the select command
works such that we, first of all,
have to define what we wanna select.
And here between parentheses,
we could specify the different columns
in which we are interested
so that we only retrieve data from these columns.
If you wanna get a complete record,
so the values for all the columns,
then you can simply specify a star here.
The star is a wild card and means select everything,
all the data from all the columns
for all the rows that match my condition.
Now, regarding the condition,
I'll come back to that.
Before we think about adding conditions,
we, first of all,
need to specify from which table we wanna get our data,
because we haven't specified this yet.
And it's not automatically taking this table here,
the restaurant's table,
because we could have more than one table in the database.
And how would MySQL and the MySQL server then know
which table to query?
That's why after defining which fields you're interested in,
you need the from keyword
to specify the table you're interested in.
And then here, you use your table name.
In this case, restaurants.
Now you could add a condition
if you don't wanna fetch all the entries,
but right now, we don't need that.
I do want all the entries.
And therefore, that's already it.
Now we can again click that flash
and we see that it succeeded.
And then here, we also got the results of this query.
And here we see our table in the end
with all the entries we got there.
And at the moment, that's of course only one entry
where we got our Web Dev Mealery restaurant,
which is of type German.
Now if we add another entry,
and for this, I go back to this first tab
where I still got my previous insert into query saved.
And I use that and change it
to insert another different restaurant
like
Pizza
House.
And that could be Italian.
And I then run this.
And then maybe we add a third restaurant,
the
Beergarden,
which may be again is German.
Then of course, I should now have three records
in my database.
And to see them, we can go back to this tab
and simply run this query again.
We still have the old result here
from the last time we executed it.
But now, since I added two new restaurants,
if I execute this query again,
now I have these 3 results here in my result set.
And that's how we can get data from our database tables.
Now, as I said before,
we can also filter,
because very often, you don't want to get
all your data pieces, all your records,
but only a subset of the records of a database table.
And to only get a subset,
you can add the where keyword after select from.
This allows you to define one or more conditions.
And for example, here,
we could be interested in only the restaurants
that are of type German.
Then we can simply specify the column name
on which we wanna filter.
In this case, type.
And then we can filter with the greater than,
smaller than, greater or equal,
or just equal sign,
depending on what we wanna express.
And here, I wanna filter for all the restaurants
where the type is equal to German.
German between quotes because it is a string,
and very important, a single equal sign.
In JavaScript, when we wrote if statements or conditions,
then we had double or triple equal signs.
A single equal sign in JavaScript was used
to assign a value to a variable.
Here in SQL, it's different.
Here, we don't assign values to any variables.
We don't really have that here.
Instead, here,
a single equal sign in a where condition
is used to express equality.
So you're looking for all the restaurants
where the type is equal to German.
That's what we're saying here.
You could also add more conditions.
For example, with and,
or or.
You can add more conditions that will be evaluated together.
And we could say that we're interested
in all the restaurants where the type is German,
and the name is greater than
W.
You can use greater and smaller than on strings as well,
because the characters are also ordered alphabetically.
But this is not really an extra restriction I need.
Instead, I'm only interested in the type.
And therefore, that's the only condition.
And hence, now if we execute this query,
I get back a result set with only two entries.
Now just to make this clear,
the database still has all three entries.
The entry with the restaurant of type Italian
wasn't deleted.
Instead, here,
we adjust our fetching some data from the database table.
And it turns out that with this query,
we're only fetching two of three records.
The other entry is still there,
but not fetched with this query.
That's why we only see two results here.
Now we can tell by the IDs that,
probably, one element is missing.
And that's of course the restaurant with the ID 2
which has a type of Italian.
But it's the power of queries like this
that we can select certain pieces of a database table,
certain records that meet a certain condition
that makes SQL so powerful and useful.
Because with that, we can store large amounts of data
in a database table.
And then depending on what we plan on doing,
for example, if on our website,
we wanna show a list of Italian restaurants,
we can run a query like this and only get back the data
that's relevant for the exact problem
we're trying to solve at the moment.
And that's, therefore,
how we can select data that meets a certain condition
from our database tables.
Now just to add onto this,
you also can restrict only a certain amount of columns.
At the moment, because of the wild card,
I'm fetching all the fields for the records
that meet this condition.
That means in this case, I'm fetching ID,
name and type.
Now if I'm only interested in the name,
for example, I can say select name like this.
And I can have a comma separated list of columns here.
So if I only select the name like this and I execute this,
then I only get back the name,
not the ID and type.
Just as with the other rows,
the ID and type columns were not deleted.
They are just not part of this result set here.
If I, for example,
add ID in front of name,
I get that as well.
We should remove these parentheses though.
Here, you don't have parentheses.
That's my bad.
So here, you simply separate your columns with a comma
without parentheses around.
So with that, I now get these two fields
for the records that meet this condition.
Now last but not least,
you could also run aggregation functions.
For example, if you wanna find out how many restaurants
you have in that database table that are of type German.
Of course, we can count manually,
but that can quickly become very cumbersome.
And that's why there are built in aggregation functions
that allow you to build the sum across a certain column
for all the records that meet a certain condition.
Or, for example,
you have count,
which allows you to count all the records
that meet this condition.
Now you need to tell SQL for which column
it should count.
But for count, of course,
that doesn't really matter
because we got values in all the columns.
And hence, I'll use a star here.
This would be more relevant if you use something
like sum here,
because if you wanna build the sum across all the values
of all the records that meet a certain condition,
you need to let SQL know which column holds the values
you wanna sum up.
But for count, of course,
this doesn't matter.
And therefore, now if I run this,
I get back a single number,
and that's the count,
the number of records that meet this condition.
And that's a first look at this select statement
and how we can use it to select data
from our database tables.