Preamble
MySQL statement SELECT is used to extract records from one or more tables to MySQL.
Simple syntax for SELECT statement in MySQL
SELECT expressions
FROM tables
[WHERE conditions];
The full syntax for the SELECT statement in MySQL
SELECT [ ALL | DISTINCT | DISTINCTROW ]
[ HIGH_PRIORITY ]
[ STRAIGHT_JOIN ]
[ SQL_SMALL_RESULT | SQL_BIG_RESULT ] [ SQL_BUFFER_RESULT ].
[ SQL_CACHE | SQL_NO_CACHE ]
[ SQL_CALC_FOUND_ROWS ]
expressions
FROM tables
[WHERE conditions]
[GROUP BY]
[HAVING condition]
[ORDER BY expression [ ASC | DESC ]].
[LIMIT [offset_value] number_rows | LIMIT number_rows OFFSET offset_value].
[PROCEDURE procedure_name]
[INTO [ OUTFILE 'file_name' options
| DUMPFILE 'file_name'
| @variable1, @variable2, ... @variable_n].
[FOR UPDATE | LOCK IN SHARE MODE];
Parameters and arguments of the statement
- ALL is optional. Returns all matching strings
- DISTINCT is optional. Removes duplicates from the result set. Learn more about DISTINCT.
- DISTINCTROW is optional. Synonym for DISTINCT. Removes duplicates from the results set.
- HIGH_PRIORITY – optional. It tells MySQL that it starts SELECT before any UPDATE operators waiting for the same resource. It can be used with MyISAM, MEMORY, and MERGE tables that use table-level locks.
- STRAIGHT_JOIN is optional. It tells MySQL to connect the tables in the order they are listed in the FROM sentence.
- SQL_SMALL_RESULT is optional. It uses fast temporary tables to store results (used with DISTINCT and GROUP BY).
- SQL_BIG_RESULT is optional. Prefers sorting rather than a temporary table to store results (used with DISTINCT and GROUP BY).
- SQL_BUFFER_RESULT is optional. Uses temporary tables to store results (cannot be used with subqueries).
- SQL_CACHE is optional. Saves results in a query cache.
- SQL_NO_CACHE is optional. It does not save results in the query cache.
- SQL_CALC_FOUND_ROWS is optional. Calculates how many records are in the result set (without taking into account the LIMIT attribute), which can then be obtained with FOUND_ROWS.
- expressions – The columns or calculations that you want to get. Use * if you want to select all columns.
- tables – the tables from which you want to get the records. There must be at least one table listed in the FROM sentence.
- WHERE conditions are optional. The conditions that must be met for the selected records.
- GROUP BY – optional. It collects data by several records and groups results by one or several columns. Learn more about GROUP BY.
- HAVING condition is optional. It is used in combination with GROUP BY to limit groups of returned rows to only those whose TRUE condition. Read more about HAVING.
- ORDER BY is optional. It is used to sort the entries in your result set. Learn more about ORDER BY.
- LIMIT is optional. If LIMIT is specified, it controls the maximum number of records to be retrieved. The maximum number of records specified by number_rows will be returned in the resulting set. The first line returned by LIMIT will be determined by the value offset_value.
- PROCEDURE is optional. If specified, it is the name of the procedure which should process the data in the resulting set.
- INTO is optional. If specified, this allows you to write the resulting set to a file or variable.
Meaning
|
Explanation
|
---|---|
INTO OUTFILE
‘filename’ options |
“Writes the resulting set to a file named filename on the server host. For parameters you can specify:
FIELDS ESCAPED BY ‘character’ FIELDS TERMINATED BY ‘character’ [ OPTIONALLY ENCLOSED BY ‘character’ ] LINES TERMINATED BY ‘character’ where a character is a character displayed as ESCAPE, ENCLOSED or TERMINATED. For example: SELECT supplier_id, supplier_name FROM suppliers INTO OUTFILE ‘results.txt’ FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ”” LINES TERMINATED BY ‘\n’;” |
INTO DUMPFILE
‘filename’ |
Writes one line of the result set to a file named filename on the server host. This method does not interrupt a column, break a line, or process a transition.
|
INTO @variable1,
@variable2, … @variable_n |
Writes a set of results in one or more variables as specified in the @ v parameters
|
- FOR UPDATE – optional. Records affected by the query are blocked until the transaction is completed.
- LOCK IN SHARE MODE – optional. Records affected by the query may be used by other transactions, but cannot be updated or deleted by these and other transactions.
SELECTION OF ALL FIELDS FROM ONE TABLE
Let’s see how to use MySQL statement SELECT to select all fields from a table.
SELECT *
FROM order_details
WHERE quantity >= 100
ORDER BY DESC;
In this MySQLSELECT example, we used * to indicate that we want to select all fields from the order_details table where the number is greater than or equal to 100. The resulting set is sorted by quantity in descending order.
SELECTION OF INDIVIDUAL FIELDS FROM THE SAME TABLE
You can also use MySQL statement SELECT to select individual fields from a table.
For example:
SELECT order_id, quantity, unit_price
FROM order_details
WHERE quantity < 300
ORDER BY quantity ASC, unit_price DESC;
In this MySQL example, SELECT returns only order_id, quantity, and unit_price fields from the order_details table where the number is less than 300. The results are sorted by quantity in ascending order and then unit_price in descending order.
SELECTION OF FIELDS FROM SEVERAL TABLES
You can also use MySQL statement SELECT to extract fields from multiple tables.
SELECT order_details.order_id, customers.customer_name
FROM customers
INNER JOIN order_details
ON customers.customer_id = order_details.customer_id
ORDER BY order_details.order_id;
In this MySQL example, SELECT connects two tables together to produce a result set that displays the order_id and customer_name fields where the customer_id value matches both the order_details and the order_details table. The results are sorted by the order_details.order_id the field in ascending order.
Write to file
You can also use MySQL statement SELECT to write a set of results to a file.
For example:
SELECT order_id, quantity, unit_price
FROM order_details
WHERE quantity < 300
ORDER BY quantity
INTO OUTFILE 'result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"
LINES TERMINATED BY '\n';
In this MySQL example, SELECT returns only order_id, quantity, and unit_price fields from the order_details table where the quantity is less than 300. The results are sorted by quantity in ascending order and written to a file named result.txt.
Learning MySQL; SELECT Statements
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Maximizing SaaS Database Performance in the Financial Sector with AIOps and Cloud FinOps—Powered by Enteros
- 17 September 2025
- Database Performance Management
Introduction The financial sector is evolving rapidly in the era of digital-first services. Banks, investment firms, insurance providers, and fintech companies are managing unprecedented volumes of transactions, risk models, customer interactions, and compliance data. At the center of this transformation are SaaS databases, which power real-time trading platforms, digital banking, fraud detection systems, regulatory reporting, … Continue reading “Maximizing SaaS Database Performance in the Financial Sector with AIOps and Cloud FinOps—Powered by Enteros”
Optimizing Real Estate IT with AI SQL, Spot Instances, and Cloud Centers of Excellence—Powered by Enteros
Introduction The real estate sector is undergoing a digital revolution. From property search engines and virtual tours to predictive analytics for investment and AI-driven customer engagement, the industry is increasingly reliant on data-driven platforms. At the core of this transformation are databases—the backbone of property listings, mortgage systems, customer relationship management (CRM), IoT-enabled smart buildings, … Continue reading “Optimizing Real Estate IT with AI SQL, Spot Instances, and Cloud Centers of Excellence—Powered by Enteros”
Airline Check-ins Crashing: Passengers Stuck in Digital Queues
Introduction Air travel depends on speed and efficiency—but increasingly, passengers are delayed not at the gate, but in digital check-in queues. Database performance is at the heart of these failures. This article explains why airline IT systems struggle under pressure, the business risks involved, and how better database monitoring prevents costly meltdowns. Why Check-ins Depend … Continue reading “Airline Check-ins Crashing: Passengers Stuck in Digital Queues”
Smart Grids Crashing: Blackouts from DB Delays
Introduction Smart grids promise efficiency, sustainability, and resilience. But when databases powering them lag, the result isn’t just inconvenience—it’s regional blackouts and massive financial losses. In this article, we explore why database delays threaten energy infrastructure and how providers can protect against them. Why Databases Matter in Smart Grids Read moreMongoDB profiler and database performance … Continue reading “Smart Grids Crashing: Blackouts from DB Delays”