Skip to main content

How to Read Rows from a MySQL Table with SELECT

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

SELECT is how you read from MySQL. You name the columns and the table, then narrow it down with WHERE, sort it with ORDER BY, and cut it short with LIMIT. This guide runs each of those on a real table in MySQL 9.7, and shows what comes back.

Prerequisites​

  • MySQL, and a way to run SQL. The examples use MySQL 9.7.4 in Docker, and the mysql client from How to Connect to MySQL in a Docker Container.
  • A table with rows. The examples read categories and products, from an inventory database. Use your own table names.

Step 1: Read Every Row and Column​

* means every column:

mysql
SELECT * FROM categories;
Output
+----+---------------+----------------------------+
| Id | Name | CreatedAtUtc |
+----+---------------+----------------------------+
| 1 | Cables | 2026-11-04 10:48:37.000261 |
| 2 | Wall chargers | 2026-11-04 10:48:38.457419 |
| 5 | Adapters | 2026-11-04 10:48:45.591706 |
+----+---------------+----------------------------+
3 rows in set (0.000 sec)

A terminal with the result of SELECT star from categories: three rows with the columns Id, Name and CreatedAtUtc, and 3 rows in set.

It's handy when you're exploring. In code, name the columns instead, so you read only what you use and a new column can't change your results.

Step 2: Choose Columns and Rows​

List the columns, and add WHERE to choose the rows:

mysql
SELECT Id, Name FROM categories WHERE Id = 1;
Output
+----+--------+
| Id | Name |
+----+--------+
| 1 | Cables |
+----+--------+
1 row in set (0.000 sec)

MySQL's SELECT page says the WHERE clause "indicates the condition or conditions that rows must satisfy to be selected."

Step 3: Sort the Results​

ORDER BY sorts, and DESC means largest first. This combines a condition and a sort:

mysql
SELECT Sku, Name, UnitPrice FROM products WHERE UnitPrice < 10 ORDER BY UnitPrice DESC;
Output
+---------------+------------------------+-----------+
| Sku | Name | UnitPrice |
+---------------+------------------------+-----------+
| CBL-USBC-1M | USB-C cable, 1 m | 7.50 |
| ADP-USBC-USBA | USB-C to USB-A adapter | 5.25 |
+---------------+------------------------+-----------+
2 rows in set (0.000 sec)

Without ORDER BY, MySQL doesn't promise any order. If order matters, ask for it.

Step 4: Take Only the First Few​

LIMIT stops after that many rows. With a sort, this gives you the three most expensive products:

mysql
SELECT Sku, UnitPrice FROM products ORDER BY UnitPrice DESC LIMIT 3;
Output
+--------------+-----------+
| Sku | UnitPrice |
+--------------+-----------+
| CHG-USBC-65W | 39.50 |
| CHG-USBC-20W | 19.99 |
| CBL-LTNG-1M | 14.00 |
+--------------+-----------+
3 rows in set (0.000 sec)

How to Page Results in MySQL takes this further.

When Nothing Matches​

It isn't an error. MySQL says so, and returns no rows:

mysql
SELECT Sku FROM products WHERE Sku = 'NOPE';
Output
Empty set (0.000 sec)

Common Mistakes​

MistakeWhat happensDo this instead
SELECT * in application codeYou read columns you don't use, and results change when a column is addedName the columns
LIMIT with no ORDER BYWhich rows you get isn't definedSort first
Double quotes around text in some SQL modesThey can mean a column nameUse single quotes for text
A table name in the wrong case on LinuxTable doesn't existMatch its case

FAQ​

Is SQL case-sensitive?​

Keywords aren't: select works too. Table names can be, depending on the operating system. Comparing text depends on the column's collation, as in How to Make a MySQL Text Comparison Case-Sensitive.

How do I show wide rows so they're readable?​

End the statement with \G, as in How to Show MySQL Query Results Vertically.

Conclusion​

SELECT columns FROM a table, with WHERE to choose rows, ORDER BY to sort them and LIMIT to cut them short. Name your columns in code, and sort whenever the order matters.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.