Query a database

We’ve learned that a table consists of rows and columns. Often, you want to see a subset rows, a subset of columns, or a combination of two. To query data from tables in MySQL, the SELECT statement is used. Here is the basic syntax:

SELECT column_name,column_name FROM table_name

The SELECT statement is used to control which columns and rows will be displayed. For example, if we only want to display the first name and the surname of all users in our testtb, we would use the following command:

mysql> SELECT name, surname FROM testtb;
 +------+-----------+
 | name | surname |
 +------+-----------+
 | Amy | Goodridge |
 | Mark | Smith |
 +------+-----------+
 2 rows in set (0.00 sec)

To display every column in the table, we can use the asterix (*) character:

mysql> SELECT * FROM testtb;
 +------+-----------+------+
 | name | surname | year |
 +------+-----------+------+
 | Amy | Goodridge | 1991 |
 | Mark | Smith | 1955 |
 +------+-----------+------+
 2 rows in set (0.00 sec)
Geek University 2021