WDN Logo

home-icon Home / MySQL / MySQL guide – Querying MySQL tables

MySQL guide – Querying MySQL tables

MySQL guide - 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.

Sponsored Links

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.

Querying tables with MySQL Select – Another example

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)

Sponsored Links

Search

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

"You" represented by the individual content creator on the WWW was name the Time magazine's "Person of the Year" in 2006. [more...]

Share on Whats app Share on Facebook Share on Twitter Share on Google plus