HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL SELECT Statement

The SELECT statement is the most frequently used command in MySQL. It is the R in CRUD — Read. Every time you want to retrieve data from a table — whether one row or millions — you use SELECT. Everything from simple lookups to complex multi-table reports is built on this single statement.


Syntax

SELECT column1, column2, ...
FROM table_name;

The clauses that can follow FROM — like WHERE, ORDER BY, and LIMIT — are all optional. They each get their own dedicated lesson later in this series.


Sample Table

All examples on this page use the following students table:

id name city age
1Rahim UddinDhaka22
2Sara BegumChittagong20
3Karim AliSylhet23
4Nila AkterDhaka21
5Rafi HossainRajshahi24

Select All Columns

The asterisk * is a wildcard that means "all columns". Use it when you want every column returned:

SELECT * FROM students;

This returns all 5 rows with all 4 columns exactly as shown in the table above.

Note: While SELECT * is convenient for quick exploration, avoid it in production code. Listing columns explicitly makes queries faster, more predictable, and easier to maintain.

Select Specific Columns

List only the columns you need, separated by commas:

SELECT name, city FROM students;
namecity
Rahim UddinDhaka
Sara BegumChittagong
Karim AliSylhet
Nila AkterDhaka
Rafi HossainRajshahi

Column Aliases with AS

You can rename a column in the result set using AS. The alias only affects the output — the actual column name in the table is unchanged:

SELECT name AS student_name, city AS hometown
FROM students;
student_namehometown
Rahim UddinDhaka
Sara BegumChittagong
Karim AliSylhet
Nila AkterDhaka
Rafi HossainRajshahi

Calculated Expressions in SELECT

You are not limited to column names — you can include any expression or calculation:

-- Add 1 to every student's age in the result
SELECT name, age, age + 1 AS next_year_age
FROM students;
-- Combine first and last name columns into one
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;

The calculation happens at query time and does not change any data stored in the table.


SELECT with WHERE

Use WHERE to filter results and return only rows that match a condition. The WHERE clause is covered in full detail in its own lesson, but here is a quick preview:

-- Return only students from Dhaka
SELECT name, age FROM students
WHERE city = 'Dhaka';
nameage
Rahim Uddin22
Nila Akter21

SELECT with ORDER BY

Use ORDER BY to sort results. ASC (ascending) is the default; use DESC for descending:

-- Sort students by age, youngest first
SELECT name, age FROM students
ORDER BY age ASC;

SELECT with LIMIT

LIMIT restricts how many rows are returned — useful for pagination or previewing data:

-- Return the first 3 students only
SELECT name, city FROM students
LIMIT 3;

Combining Clauses

All clauses can be combined in a single query. The order must always follow this structure:

SELECT name, city, age
FROM students
WHERE age > 20
ORDER BY age DESC
LIMIT 3;
Clause order: SELECTFROMWHEREORDER BYLIMIT. MySQL will return an error if the clauses appear in the wrong order.

Key Points to Remember

  • SELECT * returns all columns — use it for exploration, not for production queries.
  • List only the columns you actually need for better performance and clarity.
  • Use AS to give columns a friendlier name in the result — the table itself is not changed.
  • You can include expressions and function calls directly in a SELECT list.
  • Add WHERE to filter, ORDER BY to sort, and LIMIT to cap the number of results.
  • Clause order matters: SELECT → FROM → WHERE → ORDER BY → LIMIT.
What's next? The next lesson covers UPDATE — how to modify data that already exists in a table.