SELECT is a keyword. Then there is star *. The star
is issued when we want to select all the columns from
the table. Then there is a FROM keyword specified.
And the last word is the table name (users).
Next we will try to select only certain columns
from the user table but still all rows:
select user_name from users;
gives us:
user_name
-----------
user1
user2
user3
user4
The database returns only 1 column (user_name) but
still all the rows. What if we would like to limit
the result of the query to only one row and e.g. two
columns?
We would provide following query:
select user_key, user_name
from users
where user_key = 1;
The result of the query would be following:
user_key user_name
---------- -----------
1 user1
In the above SELECT query we added the WHERE
keyword. After this keyword we need to specify filter
clause to tell the database what rows it has to
return to us.
WHERE is the filter keyword. And then in the
expression user_key = 1 we specify selection of only
1 row (because we have user_key as unique primary
key) and limit the results to the row where user_key
equals 1.
Let's say we would like to explore the user's list
and search for the users where theirs username
contains certain string. This could be achieved by
the LIKE clause:
select user_key, user_name
from users
where user_name like '%2';
The result would be:
user_key user_name
---------- -----------
2 user2
What we have specified here is the LIKE keyword and
specified the filter to '%2'. That would read as
search for rows where the user_name contains any
string from the beginning but the last character is
2. The percentage sign (%) translates here to "any
characters".