Summary: in this tutorial, you will learn how to use the MariaDB select
statement to query data from a table.
Introduction to MariaDB select statement
The MariaDB select
statement retrieves data from one or more tables. Here is the simplest syntax of the select
statement:
select select_list from table_name;
In this syntax:
- First, specify a list of comma-separated columns or expressions from which you want to retrieve data. The columns must be available in the
table_name
. - Then, specify the name of the table from which you want to query data.
Even though the select
clause appears before the from
clause, when evaluating the select
statement, MariaDB evaluates the from
clause before the select
clause:
MariaDB select statement examples
We’ll use the countries
table from the nation
sample database for the demonstration.
A) Using MariaDB select statement to query data from one column example
The following statement uses the select
statement to retrieve data from the name
column in the countries
table:
select name from countries;
In this example:
- First, specify the name column of the countries table in the select clause.
- Then, specify the
countries
table in the from clause.
The select
statement returns a set of result rows which is often called a result set.
B) Using the MariaDB select statement to query data from multiple columns example
This statement uses the select
statement to retrieve data from the name
, area
, and national_day
columns of the countries
table:
select name, area, national_day from countries;
In this example, the column names are separated by commas in the select
clause.
C) Using the MariaDB select statement to query data from all columns of a table example
To select data from all columns of a table, you specify all the column names in the select
clause:
select country_id, name, area, national_day, country_code2, country_code3, region_id from countries;
To make it more convenient, MariaDB provides the star (*) shorthand:
select * from countries;
In this example, the star ( *
) is the shorthand for all columns of the countries
table.
The select *
is called select star
or select all
.
Note that it is a good practice to use the select *
only for adhoc queries. If you embed the select statement in the application’s code, you should explicitly specify the names of columns from which you want to retrieve data.
D) Using the MariaDB select statement with expression only example
In MariaDB, the from
clause is optional. It is convenient if you want to call a function or evaluate an expression.
The following select statement uses the now()
function to return the current timestamp of the server:
select now();
In this tutorial, you have learned how to the simple form of the MariaDB select statement to query data from a table.