Now, in the last lesson, we looked at how we would create new tables using the SQL language and how
we would insert data into our tables. In this lesson,
we're going to look at the next part of CRUD which is 'R' for read. How would you read data in your table?
Well, the most often used keyword is the SQL SELECT keyword.
And what this does is it allows you to read the data from your database.
So for example, whenever I right-clicked on my tables, and I clicked on Show table, you can see up here
this syntax show up automatically because I'm able to show the table by running this statement. And what
it says is SELECT * FROM products. Products is the table that I want to see and * stands for a
wildcard,
so it means select everything from the products table,
and this is why we see everything in our table.
Now if you only wanted one column or two columns from this table, then instead of an asterisk or *, you
can simply write SELECT name, price FROM this products table. And if you hit Run, you can see we've
now removed that id column and we're reading from our database only the columns that we want.
Now what if you only wanted a particular row from your database?
Say for example, you only wanted to see the first row,
the one with an id of 1,
how would you do that with SQL? Well, this is when you would need to search through your database by
using the WHERE keyword.
So this is the syntax we would SELECT a particular column, or if you want to select all of the columns
then you would put in asterisks, and then we specify the table that you want to read the data from
and then the WHERE is a search condition.
So you could say something like
WHERE Country = 'Mexico', or WHERE customer ID = 1.
If we tried out in the browser we can write SELECT everything which is * FROM the products table
WHERE id = 1.
So now if I hit Run, it isolates that single record where the id is equal to 1 and we select a single
row.
Now if you read the documentation, you can see that there's other operators where you can get fancier
than what we've done.
So you can use things such as Equal, or Greater, or Less than, Between, like in all sorts of things
to modify this WHERE statement so that you can select the pieces of data that you need.
So this statement is for reading data from your database and grabbing the data that meets certain criteria.
And you'll see the statement a lot
when you start working with SQL databases.