The MySQL SELECT statement reads and retrieves data rows from one or more database tables. It forms the foundation of all database querying operations.
SELECT column1, column2, ...
FROM table_name;
To select all columns from a table, use the wildcard operator (*). However, explicit column lists are preferred in production applications.
-- Create sample customers table
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
city VARCHAR(50)
);
-- Insert sample records
INSERT INTO customers (first_name, last_name, email, city) VALUES
('Jane', 'Doe', '[email protected]', 'New York'),
('John', 'Smith', '[email protected]', 'Chicago'),
('Alice', 'Johnson', '[email protected]', 'Austin');
-- Retrieve specific columns
SELECT first_name, email, city
FROM customers;
| first_name | city | |
|---|---|---|
| Jane | [email protected] | New York |
| John | [email protected] | Chicago |
| Alice | [email protected] | Austin |
SELECT * wastes memory, network bandwidth, and prevents database index-only scans.AS to make result sets cleaner and easier to consume in code.Why is explicitly listing column names safer and more performant than using SELECT * in production APIs?
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With