The SELECT statement allows you to specify which columns you want to choose from the data. The list appears as the first part of the statement. The second part is the FROM clause, which specifies the table(s) you want to retrieve rows from.
Note The FROM clause can be used to join tables with the JOIN operator.
The order that you specify the columns determines the order shown in the result set.
If you want all of the columns, use an asterisks (*) instead. Listing 2-3 demonstrates three statements that generate the same result sets. That is, the same rows will be displayed in the output of each. In fact, I am using a table with only four rows for simplicity.
Listing 2-3. Example SELECT Statements
mysql> SELECT plant_name, sensor_value, sensor_event, sensor_level FROM greenhouse.plants;
+---+---+---+---+
| plant_name | sensor_value | sensor_event | sensor_level | +---+---+---+---+
| fern in den | 0.2319 | 2015-09-23 21:04:35 | NULL |
| fern on deck | 0.43 | 2015-09-23 21:11:45 | NULL |
| flowers in bedroom1 | 0.301 | 2015-09-23 21:11:45 | NULL |
| weird plant in kitchen | 0.677 | 2015-09-23 21:11:45 | NULL | +---+---+---+---+
4 rows in set (0.00 sec)
CHaPTeR 2 GeTTING STaRTed wITH MySQL
mysql> SELECT * FROM greenhouse.plants;
+---+---+---+---+
| plant_name | sensor_value | sensor_event | sensor_level | +---+---+---+---+
| fern in den | 0.2319 | 2015-09-23 21:04:35 | NULL |
| fern on deck | 0.43 | 2015-09-23 21:11:45 | NULL |
| flowers in bedroom1 | 0.301 | 2015-09-23 21:11:45 | NULL |
| weird plant in kitchen | 0.677 | 2015-09-23 21:11:45 | NULL | +---+---+---+---+
4 rows in set (0.00 sec)
mysql> SELECT sensor_value, plant_name, sensor_level, sensor_event FROM greenhouse.plants;
+---+---+---+---+
| sensor_value | plant_name | sensor_level | sensor_event | +---+---+---+---+
| 0.2319 | fern in den | NULL | 2015-09-23 21:04:35 |
| 0.43 | fern on deck | NULL | 2015-09-23 21:11:45 |
| 0.301 | flowers in bedroom1 | NULL | 2015-09-23 21:11:45 |
| 0.677 | weird plant in kitchen | NULL | 2015-09-23 21:11:45 | +---+---+---+---+
4 rows in set (0.00 sec)
Note that the first two statements result in the same rows as well as the same
columns in the same order. However, the third statement although it generates the same rows, displays the columns in a different order.
You also can use functions in the column list to perform calculations and similar operations. One special example is using the COUNT() function to determine the number of rows in the result set, as shown here. See the online MySQL reference manual for more examples of functions supplied by MySQL.
SELECT COUNT(*) FROM greenhouse.plants;
The next clause in the SELECT statement is the WHERE clause. This is where you specify the conditions you want to use to restrict the number of rows in the result set. That is, only those rows that match the conditions. The conditions are based on the columns and can be quite complex. That is, you can specify conditions based on calculations, results from a join, and more. But most conditions will be simple equalities or inequalities on
59 one or more columns to answer a question. For example, suppose you wanted to see the plants where the sensor value read is less than 0.40? In this case, we issue the following query and receive the results. Note that I specified only two columns: the plant name and the value read from sensor.
mysql> SELECT plant_name, sensor_value FROM greenhouse.plants WHERE sensor_
value < 0.40;
+---+---+
| plant_name | sensor_value | +---+---+
| fern in den | 0.2319 |
| flowers in bedroom1 | 0.301 | +---+---+
2 rows in set (0.01 sec)
There are additional clauses you can use including the GROUP BY clause, which is used for grouping rows for aggregation or counting, and the ORDER BY clause, which is used to order the result set. Let’s take a quick look at each starting with aggregation.
Suppose you wanted to average the sensor values read in the table for each sensor.
In this case, we have a table that contains sensor readings over time for a variety of sensors. Although the example contains only four rows (and thus may not be statistically informative), the example demonstrates the concept of aggregation quite plainly, as shown in Listing 2-4. Note that what we receive is simply the average of the four sensor values read.
Listing 2-4. GROUP BY Example
mysql> SELECT plant_name, sensor_value FROM greenhouse.plants WHERE plant_
name = 'fern on deck';
+---+---+
| plant_name | sensor_value | +---+---+
| fern on deck | 0.43 |
| fern on deck | 0.51 |
| fern on deck | 0.477 |
| fern on deck | 0.73 | +---+---+
4 rows in set (0.00 sec)
CHaPTeR 2 GeTTING STaRTed wITH MySQL
mysql> SELECT plant_name, AVG(sensor_value) AS avg_value FROM greenhouse.
plants WHERE plant_name = 'fern on deck' GROUP BY plant_name;
+---+---+
| plant_name | avg_value | +---+---+
| fern on deck | 0.536750003695488 | +---+---+
1 row in set (0.00 sec)
Note that I specified the average function, AVG(), in the column list and passed in the name of the column I wanted to average. There are many such functions available in MySQL to perform some powerful calculations. Clearly, this is another example of how much power exists in the database server that would require many more resources on a typical lightweight sensor or aggregator node in the network.
Also note that I renamed the column with the average using the AS keyword. You can use this to rename any column specified, which changes the name in the result set, as you can see in the listing.
Another use of the GROUP BY clause is counting. In this case, we replaced AVG() with COUNT() and received the number of rows matching the WHERE clause. More specific, we want to know how many sensor values were stored for each plant.
mysql> SELECT plant_name, COUNT(sensor_value) as num_values FROM greenhouse.plants GROUP BY plant_name;
+---+---+
| plant_name | num_values | +---+---+
| fern in den | 1 |
| fern on deck | 4 |
| flowers in bedroom1 | 1 |
| weird plant in kitchen | 1 | +---+---+
4 rows in set (0.00 sec)
Now let’s say we want to see the results of our result set ordered by sensor value. We use the same query that selected the rows for the fern on the deck, but we order the rows by sensor value in ascending and descending order using the ORDER BY clause. Listing 2-5 shows the results of each option.
61 Listing 2-5. ORDER BY Examples
mysql> SELECT plant_name, sensor_value FROM greenhouse.plants WHERE plant_
name = 'fern on deck' ORDER BY sensor_value ASC;
+---+---+
| plant_name | sensor_value | +---+---+
| fern on deck | 0.43 |
| fern on deck | 0.477 |
| fern on deck | 0.51 |
| fern on deck | 0.73 | +---+---+
4 rows in set (0.00 sec)
mysql> SELECT plant_name, sensor_value FROM greenhouse.plants WHERE plant_
name = 'fern on deck' ORDER BY sensor_value DESC;
+---+---+
| plant_name | sensor_value | +---+---+
| fern on deck | 0.73 |
| fern on deck | 0.51 |
| fern on deck | 0.477 |
| fern on deck | 0.43 | +---+---+
4 rows in set (0.00 sec)
As I mentioned, there is a lot more to the SELECT statement than shown here, but what we have seen will get you very far, especially when working with data typical of most small- to medium-sized database solutions.