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
How to Enable Intelligent Wealth Growth with Enteros Database Analytics, RevOps Automation, and Gen AI
- 24 June 2026
- Software Engineering
Introduction Wealth management and investment organizations are entering a new era defined by data-driven decision-making, AI-powered advisory systems, and highly automated operational environments. As client expectations grow and financial markets become more dynamic, firms must continuously improve performance, efficiency, and personalization to remain competitive. Modern wealth organizations now operate complex ecosystems that include: Portfolio management … Continue reading “How to Enable Intelligent Wealth Growth with Enteros Database Analytics, RevOps Automation, and Gen AI”
How to Improve Financial Cost Visibility with Enteros Database Management Platform and Cost Attribution Analytics
Introduction The financial services industry is rapidly evolving as banks, insurance companies, fintech platforms, and investment firms modernize their digital infrastructure to support real-time transactions, data-driven decision-making, and highly personalized customer experiences. Modern financial organizations operate complex ecosystems that include: Core banking systems Digital payment platforms Investment and trading systems Risk management applications Fraud detection … Continue reading “How to Improve Financial Cost Visibility with Enteros Database Management Platform and Cost Attribution Analytics”
How AI-Driven Database Monitoring Enhances Business Continuity and Resilience
In today’s always-on digital economy, business continuity and operational resilience have become essential for enterprise success. Organizations depend heavily on digital systems to support customer interactions, financial transactions, supply chain operations, analytics, internal workflows, and real-time decision-making. Any disruption to these systems can lead to significant financial loss, operational inefficiencies, and reputational damage. At the … Continue reading “How AI-Driven Database Monitoring Enhances Business Continuity and Resilience”
Reducing Application Latency with Intelligent Database Performance Management
In today’s digital economy, application speed is directly tied to business success. Whether users are shopping online, using banking applications, streaming content, accessing SaaS platforms, or interacting with enterprise systems, they expect fast and seamless experiences. Even minor delays can impact user satisfaction, engagement, and revenue. Application latency has become one of the most important … Continue reading “Reducing Application Latency with Intelligent Database Performance Management”