SELECT Statement
In this page:
SELECT column1, column2 FROM table_name;
SELECT * FROM table_name;
Introduction to SELECT
SELECT retrieves rows from a table and is the single most frequently used SQL statement, forming the foundation of nearly every read operation an application performs against its database.
Example: Introduction to SELECT
SELECT * FROM users;
Selecting Specific Columns
Naming specific columns instead of retrieving every column reduces the amount of data MySQL has to read and transmit, which measurably speeds up queries on wide tables or over slow network links.
Example: Selecting Specific Columns
SELECT name, email FROM users;
Basic Math in SELECT
You can perform arithmetic directly inside a SELECT's column list — like multiplying price by quantity — to compute derived values on the fly without altering anything stored in the table.
Example: Basic Math in SELECT
SELECT name, price * quantity AS total FROM orders;
System Information Queries
SELECT can also return values with no table involved at all, such as the server's current time or version, which is a handy way to sanity-check a connection is alive.
Example: System Information Queries
SELECT VERSION();
Best Practices for SELECT
Avoiding SELECT * in production code is a widely recommended practice: fetching only the columns you actually need avoids wasted memory, network traffic, and breakage if the table's columns change later.
Example: Best Practices for SELECT
-- Avoid SELECT * in production; name only the columns you need
SELECT name, email FROM users;
- Using
SELECT *on large tables when only a few columns are needed, which transfers unnecessary data. - Misspelling a column name, which gives an
Unknown columnerror. - Expecting rows in a specific order without
ORDER BY.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: