Database Reference
In-Depth Information
Another thing we may want to do is select only certain rows, for example, everyone
born after 1 January, 1960:
MariaDB [test]> SELECT * FROM employees
-> WHERE birthday > '1960-01-01';
+----+---------+---------------+-----------+------------+
| id | surname | givenname | pref_name | birthday |
+----+---------+---------------+-----------+------------+
| 3 | Snow | Lorenzo | NULL | 1964-04-03 |
| 4 | Smith | George Albert | George | 1970-04-04 |
+----+---------+---------------+-----------+------------+
2 rows in set (0.00 sec)
The
>
sign is a comparison operator. Just like in math, it means greater than.
There are many other comparison operators. See
https://mariadb.com/kb/en/
comparison-operators/
for a complete list.
The arrow (
->
) in the previous output example is not something we
typed. The
mysql
command-line client program inserted it to show
that we pressed the
Enter
key before ending our command with a
semicolon (
;
), and so the command we are entering is continuing on a
second line. If we pressed
Enter
and just forgot to end our command,
we can just type a semicolon and press
Enter
again. In the previous
example, I did it on purpose to split the command onto two lines to
make it easier to read.
Our output, by default, is sorted based on the order in which it was inserted into the
database. For a list of
employees
we really should sort on the
surname
column. To
do this we use an
ORDER BY
clause as follows:
MariaDB [test]> SELECT * FROM employees ORDER BY surname;
+----+----------+---------------+-----------+------------+
| id | surname | givenname | pref_name | birthday |
+----+----------+---------------+-----------+------------+
| 5 | McKay | NULL | NULL | NULL |
| 4 | Smith | George Albert | George | 1970-04-04 |
| 3 | Snow | Lorenzo | NULL | 1964-04-03 |
| 1 | Taylor | John | John | 1958-11-01 |
| 2 | Woodruff | Wilford | Will | 1957-03-01 |
+----+----------+---------------+-----------+------------+
5 rows in set (0.00 sec)