MySQL SELECT

Summary: in this tutorial, you’ll learn how to use the MySQL SELECT statement without referencing any table.

Typically, you use a SELECT statement to select data from a table in the database:

SELECT select_list
FROM table_name;Code language: SQL (Structured Query Language) (sql)

In MySQL, the SELECT statement doesn’t require the FROM clause. It means that you can have a SELECT statement without the FROM clause like this:

SELECT select_list;Code language: SQL (Structured Query Language) (sql)

The following example uses the SELECT statement to perform a simple calculation:

SELECT 1 + 1;    Code language: SQL (Structured Query Language) (sql)

Output:

+-------+
| 1 + 1 |
+-------+
|     2 |
+-------+
1 row in set (0.00 sec)    Code language: plaintext (plaintext)

MySQL has many built-in functions like string functions, date functions, and math functions. You can use the SELECT statement to execute one of these functions.

For example, the following statement uses the NOW() function in the SELECT statement to return the current date and time of the server where MySQL server is running:

SELECT NOW();Code language: SQL (Structured Query Language) (sql)

Output:

+---------------------+
| NOW()               |
+---------------------+
| 2021-07-26 08:08:02 |
+---------------------+
1 row in set (0.00 sec)Code language: plaintext (plaintext)

The NOW() function doesn’t have any parameters. To call it, you place the parentheses () after the function name.

If a function has parameters, you need to pass arguments into it. For example, to concatenate multiple strings into a single string, you can use the CONCAT() function:

SELECT CONCAT('John',' ','Doe');Code language: SQL (Structured Query Language) (sql)

Output:

+--------------------------+
| CONCAT('John',' ','Doe') |
+--------------------------+
| John Doe                 |
+--------------------------+
1 row in set (0.00 sec)Code language: plaintext (plaintext)

The CONCAT() function accepts one or more strings and concatenates them into a single string.

Column alias

By default, MySQL uses the expression specified in the SELECT clause as the column name of the result set. To change a column name of the result set, you can use a column alias:

SELECT expression AS column_alias;Code language: SQL (Structured Query Language) (sql)

To assign an alias to a column, you place the AS keyword after the expression followed by a column alias. The AS keyword is optional, so you can skip it like this:

SELECT expression column_alias;Code language: SQL (Structured Query Language) (sql)

For example:

SELECT CONCAT('John',' ','Doe') AS name;Code language: SQL (Structured Query Language) (sql)

Output:

+----------+
| name     |
+----------+
| John Doe |
+----------+
1 row in set (0.00 sec)Code language: plaintext (plaintext)

If the column alias contains spaces, you need to place it inside quotes like this:

SELECT CONCAT('Jane',' ','Doe') AS 'Full name';Code language: SQL (Structured Query Language) (sql)

Output:

+-----------+
| Full name |
+-----------+
| John Doe  |
+-----------+
1 row in set (0.00 sec)Code language: plaintext (plaintext)

Summary

  • MySQL SELECT statement doesn’t require the FROM clause.
  • Assign an alias to a column to make it more readable.
Was this tutorial helpful?