How to Disable ONLY_FULL_GROUP_BY in MySQL?
Last Updated :
23 Jul, 2025
In MySQL, the 'ONLY_FULL_GROUP_BY' mode ensures that queries with GROUP BY clauses are written according to SQL standards. This mode requires all columns in the SELECT statement that are not aggregated to be included in the GROUP BY clause. While this is helpful for ensuring data integrity, there are scenarios where you might need to disable this mode for more flexible query writing.
This article will guide you through the process of disabling 'ONLY_FULL_GROUP_BY' and provide examples with table creation, queries, and explanations.
ONLY_FULL_GROUP_BY
In simple terms, 'ONLY_FULL_GROUP_BY' is a setting in MySQL that ensures you write your GROUP BY queries correctly. It requires that every column in your SELECT statement, that isn't used with an aggregate function (like SUM, COUNT, AVG, etc.), must also appear in the GROUP BY clause. This rule helps prevent ambiguous and potentially incorrect results by making sure that you specify exactly how to group and summarize your data.
Steps to Disable ONLY_FULL_GROUP_BY
- Check Current SQL Mode: First, check the current SQL modes enabled.
- Disable
ONLY_FULL_GROUP_BY
: Modify the SQL mode to exclude ONLY_FULL_GROUP_BY
.
- Verify the Change: Ensure that the mode has been disabled.
Step-by-Step Guide to Disabling ONLY_FULL_GROUP_BY in MySQL
1. Check Current SQL Mode
To check the current SQL mode, run the following query:
SELECT @@sql_mode;
2. Disable ONLY_FULL_GROUP_BY
You can disable 'ONLY_FULL_GROUP_BY
'
by modifying the SQL mode configuration. This can be done temporarily for the current session or globally for all sessions.
Temporarily Disable for Current Session:
SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Permanently Disable for All Sessions: To disable it permanently, you need to edit the MySQL configuration file (my.cnf or my.ini) and remove 'ONLY_FULL_GROUP_BY
'
from the SQL mode line. Add or modify the following line under the [mysqld]
section:
[mysqld]
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Then restart the MySQL service for the changes to take effect.
3. Verify the Change
To ensure that 'ONLY_FULL_GROUP_BY
'
has been disabled, run:
SELECT @@sql_mode;
Ensure that 'ONLY_FULL_GROUP_BY
'
is not in the result.
Example of MySQL Only_Full_Group_By
Example 1: Without ONLY_FULL_GROUP_BY
Create a Sample Table
Let's create a sample table sales
to demonstrate the effect of disabling 'ONLY_FULL_GROUP_BY
'
.
CREATE TABLE sales (
id INT AUTO_INCREMENT PRIMARY KEY,
product VARCHAR(50),
amount DECIMAL(10, 2),
sale_date DATE
);
INSERT INTO sales (product, amount, sale_date) VALUES
('Product A', 100.00, '2024-06-01'),
('Product A', 150.00, '2024-06-02'),
('Product B', 200.00, '2024-06-01'),
('Product B', 250.00, '2024-06-03');
Query Without ONLY_FULL_GROUP_BY
Now, execute a query that selects non-aggregated columns not included in the GROUP BY
clause:
SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SELECT product, amount FROM sales GROUP BY product;
Output:
+----------+--------+
| product | amount |
+----------+--------+
| Product A| 100.00 |
| Product B| 200.00 |
+----------+--------+
Explanation: Without 'ONLY_FULL_GROUP_BY', MySQL returns one row per product with arbitrary values for amount (e.g., 100.00 for 'Product A' and 200.00 for 'Product B'), as the amount is not aggregated.
Example 2: With Aggregated Functions
Query with Aggregated Functions
Let's run a query that makes sense both with and without 'ONLY_FULL_GROUP_BY
'
.
SELECT product, SUM(amount) AS total_amount FROM sales GROUP BY product;
Output:
+----------+-------------+
| product | total_amount|
+----------+-------------+
| Product A| 250.00 |
| Product B| 450.00 |
+----------+-------------+
Explanation: The query uses SUM(amount) to aggregate the amount for each product. Product A totals 250.00 (100.00 + 150.00), and Product B totals 450.00 (200.00 + 250.00), providing clear and accurate grouped results.
Conclusion
In Conclusion, the "only_full_group_by" error in MySQL acts as a protective measure to ensure that SQL standards are followed when using the GROUP BY clause. Although it may feel limiting, this setting guarantees precise and reliable query outcomes. When developers come across this error, they should carefully examine their queries to confirm that all non-aggregated columns in the SELECT list are also included in the GROUP BY clause. By doing this, they not only fix the error but also improve the clarity and correctness of their SQL queries, which contributes to creating a strong and compliant database environment.
Similar Reads
How to Query as GROUP BY in Django? In Django, the powerful ORM (Object-Relational Mapping) allows developers to interact with databases using Python code. One common database operation is the GROUP BY query, which groups rows sharing a property so that aggregate functions can be applied to each group. This article will guide you thro
3 min read
SELECT DISTINCT vs GROUP BY in MySQL In MySQL, the two most common ways of managing and retrieving unique values are with SELECT and GROUP BY. However, they are used for different reasons. With SELECT, we can get different values from the same column, so we donât have to worry about duplicates. With GROUP BY, we can aggregate data and
5 min read
GROUP BY vs PARTITION BY in MySQL MySQL, like many relational database management systems, provides powerful tools for manipulating and analyzing data through the use of SQL (Structured Query Language). Two commonly used clauses, GROUP BY and PARTITION BY, play essential roles in aggregate queries and window functions, respectively.
5 min read
How to Temporarily Disable a Foreign Key Constraint in MySQL? MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
4 min read
How to Delete Duplicate Rows in MySQL? Duplicate rows can cause problems with data accuracy and integrity. They can also make it difficult to query and analyze data. In this article, we will explain several methods to remove duplicate rows from your MySQL tables, ensuring your data stays clean and accurate.Note: Some features are only su
4 min read
How To Get Last Record In Each Group In MySQL? In MySQL, we group the data using the GROUP BY clause. There can be a need to access these groups and get the latest record in each group, for example, tracking the last login timestamp for each user. Knowing how to retrieve the latest record from a group is essential, as it is used in many practica
4 min read