Querying MySQL tables
Our employee_data table now contains enough data for us to work with. Let us see how we can extract (query) it. Querying involves the use of the MySQL SELECT command.
Data is extracted from the table using the SELECT SQL command. Here is the format of a SELECT statement:
SELECT column_names from table_name [WHERE ...conditions];
The conditions part of the statement is optional (we’ll go through this later). Basically, you require to know the column names and the table name from which to extract the data.
For example, in order to extract the first and last names of all employees, issue the following command.
SELECT f_name, l_name from employee_data;
The statement tells MySQL to list all the rows from columns f_name and l_name.
mysql> SELECT f_name, l_name from employee_data; +---------+------------+ | f_name | l_name | +---------+------------+ | Manish | Sharma | | John | Hagan | | Ganesh | Pillai | | Anamika | Pandit | | Mary | Anchor | | Fred | Kruger | | John | MacFarland | | Edward | Sakamuro | | Alok | Nanda | | Hassan | Rajabi | | Paul | Simon | | Arthur | Hoopla | | Kim | Hunter | | Roger | Lewis | | Danny | Gibson | | Mike | Harper | | Monica | Sehgal | | Hal | Simlai | | Joseph | Irvine | | Shahida | Ali | | Peter | Champion | +---------+------------+ 21 rows in set (0.00 sec)
On close examination, you’ll find that the display is in the order in which the data was inserted. Furthermore, the last line indicates the number of rows our table has (21).
To display the entire table, we can either enter all the column names or use a simpler form of the SELECT statement.
SELECT * from employee_data;
Some of you might recognize the * in the above statement as the wildcard. Though we don’t use that term for the character here, it serves a very similar function. The * means ‘ALL columns’. Thus, the above statement lists all the rows of all columns.
SELECT f_name, l_name, age from employee_data;
Selecting f_name, l_name and age columns would display something like:
mysql> SELECT f_name, l_name, age from employee_data; +---------+------------+------+ | f_name | l_name | age | +---------+------------+------+ | Manish | Sharma | 28 | | John | Hagan | 32 | | Ganesh | Pillai | 32 | | Anamika | Pandit | 27 | | Mary | Anchor | 26 | | Fred | Kruger | 31 | | John | MacFarland | 34 | | Edward | Sakamuro | 25 | | Alok | Nanda | 32 | | Hassan | Rajabi | 33 | | Paul | Simon | 43 | | Arthur | Hoopla | 32 | | Kim | Hunter | 32 | | Roger | Lewis | 35 | | Danny | Gibson | 34 | | Mike | Harper | 36 | | Monica | Sehgal | 30 | | Hal | Simlai | 27 | | Joseph | Irvine | 27 | | Shahida | Ali | 32 | | Peter | Champion | 36 | +---------+------------+------+ 21 rows in set (0.00 sec)
ASSIGNMENTS
Last name: Reindeer
Title: Business Analyst
Age: 34
Years of service: 2
Salary: 95000
Perks: 17000
email: rudolf@bugnet.com
ANSWERS
|
« Previous
|
Next »
|
Possible Answers create database addressbook; OR CREATE DATABASE addressbook; Note: SQL statements are case-insensitive, though table names and database names might be sensitive to case ...
Ordering data This section of the online MySQL tutorial looks at how we can change the display order of the data extracted from MySQL tables ...
selecting data using conditions In this section of the MySQL tutorial we'll look at the format of a SELECT statement we met in the last ...
Limiting data retrieval This section of the online MySQL lesson looks at how to limit the number of records displayed by the SELECT statement. As ...
IN and BETWEEN This section of the tutorial MySQL looks at the In and BETWEEN operators. To list employees who are Web Designers and System ...