MySQL SELECT Statement

The SELECT statement is used to retrieve data from a database. It is the most frequently used SQL command. Data can be fetched from one or more tables, all columns or specific ones, and the results can be filtered, sorted, or limited as needed.

Basic Syntax

SELECT column1, column2, ...
FROM table_name;

To retrieve all columns from a table, use the asterisk (*) wildcard:

SELECT * FROM table_name;

Sample Table: students

The examples in this topic use the following data:

+------------+------------+-----------+-----+-------------------+
| student_id | first_name | last_name | age | email             |
+------------+------------+-----------+-----+-------------------+
|          1 | Alice      | Johnson   |  20 | alice@example.com |
|          2 | Bob        | Smith     |  22 | bob@example.com   |
|          3 | Carol      | White     |  19 | carol@example.com |
|          4 | David      | Brown     |  21 | david@example.com |
+------------+------------+-----------+-----+-------------------+

Selecting All Columns

SELECT * FROM students;

This retrieves every row and every column from the students table.

Selecting Specific Columns

To retrieve only the first name and email of all students:

SELECT first_name, email FROM students;

Output:

+------------+-------------------+
| first_name | email             |
+------------+-------------------+
| Alice      | alice@example.com |
| Bob        | bob@example.com   |
| Carol      | carol@example.com |
| David      | david@example.com |
+------------+-------------------+

Using Column Aliases

An alias gives a column a temporary display name in the result set. It does not change the actual column name in the table.

SELECT first_name AS 'First Name', last_name AS 'Last Name'
FROM students;

Output:

+------------+-----------+
| First Name | Last Name |
+------------+-----------+
| Alice      | Johnson   |
| Bob        | Smith     |
| Carol      | White     |
| David      | Brown     |
+------------+-----------+

Performing Calculations in SELECT

Expressions and calculations can be included directly in a SELECT statement:

SELECT first_name, age, age + 5 AS age_in_5_years
FROM students;

Output:

+------------+-----+----------------+
| first_name | age | age_in_5_years |
+------------+-----+----------------+
| Alice      |  20 |             25 |
| Bob        |  22 |             27 |
| Carol      |  19 |             24 |
| David      |  21 |             26 |
+------------+-----+----------------+

Selecting Literal Values

A fixed text or number can be added as a column in the result:

SELECT first_name, 'Active' AS status
FROM students;

Every row shows "Active" in the status column regardless of stored data.

Selecting Data from Multiple Columns with Concatenation

The CONCAT() function joins multiple column values into one string:

SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM students;

Output:

+---------------+
| full_name     |
+---------------+
| Alice Johnson |
| Bob Smith     |
| Carol White   |
| David Brown   |
+---------------+

SELECT with WHERE (Preview)

The WHERE clause filters which rows are returned. This is covered in detail in the next topic, but here is a quick preview:

SELECT first_name, age
FROM students
WHERE age >= 21;

Only students aged 21 or older are returned.

Key Points

  • SELECT * FROM table_name; retrieves all columns and rows.
  • Specify column names to retrieve only the needed data.
  • Column aliases (AS) provide readable labels in the output.
  • Calculations and functions can be used directly inside SELECT.
  • The WHERE clause can be added to filter results based on conditions.

Leave a Comment

Your email address will not be published. Required fields are marked *