MySQL

Fetch columns with SELECT

To get multiple columns from a table, specify multiple column names after the SELECT keyword, and each column must be separated by a comma.Be sure to specify a comma between each column name, but not after the last column name.

The following SELECT statement retrieves three columns from the products table:

SELECT prod_id, prod_name, prod_price
FROM products;

This statement uses the SELECT statement to retrieve data from the products table. In the above example, three column names are specified, each separated by a comma. The output from this statement is given below:

+---------+------------+------------+
| prod_id | prod_name  | prod_price |
+---------+------------+------------+
| 1       | product 1  |       5.99 |
| 2       | product 2  |       3.99 |
| 3       | product 3  |       8.99 |
| 4       | product 4  |      13.99 |
| 5       | product 5  |       9.09 |
| 6       | product 6  |       8.99 |
| 7       | product 7  |       7.00 |
| 8       | product 8  |       1.80 |
| 9       | product 9  |      20.00 |
| 10      | product 10 |       4.75 |
| 11      | product 11 |      10.25 |
+---------+------------+------------+

Data Formatting

SQL statements return raw and unformatted data. The formatting, i.e. alignment and displaying the price values as currency amounts with the currency symbol and commas is typically specified in the application that displays the data, for example PHP or Perl.

Clause

SQL statements are made up of clauses, some required and some optional. A clause usually consists of a keyword and supplied data. An example of this is the SELECT statement's FROM clause, which you saw in the above code.