Once you’ve inserted your data into a database, you can extract that data to make business decisions. You pull data from the database by using the SELECT command, which has the following format :
SELECT <columns>
FROM <databaseTable>
WHERE <conditions for data>
The SELECT command has three different components at its core. The first is the <columns> element, which tells the database the columns where values should be returned. The element can be * (representing all columns) or a list of columns separated by commas. The second component is the <databaseTable> element, which represents the exact table from which the data should come.The third component is the <conditions for data> element, which represents under what conditions the data should be pulled from the database.First, we pull data using the simplest SELECT:
mysql> select * from person;
+---------+-----------+----------+
| id | username | password |
+---------+-----------+----------+
| 1034033 | John | jhn |
| 1034055 | Michael | Mic |
+---------+-----------+----------+
2 rows in set (0.00 sec)
This SELECT command tells the database to pull all columns, using the * character, from the person table. The database responds with a “table” using a heading with the column names found in the database used when we first defined the table. Next, all of the data from the table is placed in the output “table” and displayed accordingly.
Now we can limit the columns of data with our SELECT:
mysql> SELECT id, username FROM person;
+---------+-----------+
| id | username |
+---------+-----------+
| 1034033 | john |
| 1034055 | Michael |
+---------+-----------+
2 rows in set (0.00 sec)
In this example, we have specifically listed the columns we wish to pull data from and at the same time requested all of the data. The system will output the data in the familiar table format. The same query will be used, but a condition is placed on the data we wish to pull.
mysql> SELECT id, username FROM person WHERE username = ’john’;
+---------+----------+| id | username |
+---------+----------+
| 1034034 | john |
+---------+----------+
1 row in set (0.00 sec)
The same query is used here, but a WHERE clause limits the data to be pulled based on the actual value found in the username field. The condition with the SELECT query can hold logical operators to further refine the selection criteria.
For example:
mysql> SELECT * FROM person WHERE password IS NULL
AND username = ’michael’;
+---------+----------+----------+
| id | username | password |
+---------+----------+----------+
| 1034034 | michael | NULL |
+---------+----------+----------+
1 row in set (0.00 sec)
In this query, the system selects all of the rows in the person table where a value is NULL, and the username value is michael.