MariaDB: Delete Rows Not Containing the Maximum Value in a Column (Grouped by Another Column)
-
JOIN: This clause is key for identifying the rows to delete. It's not used for joining tables in this case.
- We create a temporary table by joining the original table with itself. This temporary table is used for comparison.
- We use the
ON
clause to specify the join condition. In this case, it ensures both rows have the same value in thegroup_column
.
-
value_column, MAX(value_column) AS max_value:
DELETE t1
FROM your_table t1
INNER JOIN (
SELECT group_column, MAX(value_column) AS max_value
FROM your_table
GROUP BY group_column
) t2
ON t1.group_column = t2.group_column
WHERE t1.value_column <> t2.max_value;
Explanation:
- DELETE t1: This specifies we want to delete rows from the table named
your_table
, aliased ast1
. - INNER JOIN: This joins
your_table
with a temporary result set created by the subquery. - Subquery:
SELECT group_column, MAX(value_column) AS max_value
: This part calculates the maximum value ofvalue_column
for each group defined bygroup_column
.FROM your_table
: The subquery selects data from the same table (your_table
).GROUP BY group_column
: This groups the rows by thegroup_column
.
- ON clause: This defines the join condition. It ensures rows in
t1
and the subquery result (t2
) have the same value ingroup_column
. - WHERE clause: This filters the joined data. It keeps only rows where
value_column
int1
is not equal to themax_value
(maximum value within the group) fromt2
.
This method uses a subquery with NOT EXISTS
to check if a row's value exists as the maximum value within its group.
Here's the code:
DELETE FROM your_table t1
WHERE NOT EXISTS (
SELECT 1
FROM your_table t2
WHERE t1.group_column = t2.group_column
AND t2.value_column > t1.value_column
);
- DELETE: Similar to the previous example, it specifies deletion from
your_table
. - WHERE NOT EXISTS: This clause checks if a subquery doesn't return any rows.
- Subquery:
- It selects a constant value (1) to simply check for existence, not retrieve data.
- It searches for rows in
your_table
(aliased ast2
) where:group_column
matches the current row (t1
).value_column
int2
is greater than the current row'svalue_column
(t1
).
- Essentially, if a row in the subquery exists (meaning another value is greater within the group), the row from the main table (t1) isn't deleted.
Using EXISTS with a correlated subquery:
This method utilizes a correlated subquery with EXISTS
to compare the current row's value to the maximum within its group.
DELETE FROM your_table
WHERE NOT EXISTS (
SELECT *
FROM your_table AS t2
WHERE t1.group_column = t2.group_column
AND t2.value_column > t1.value_column
);
- DELETE: Similar to previous examples.
- WHERE NOT EXISTS: This clause checks for the absence of rows in the subquery.
- Subquery:
- It selects all columns (
*
) fromyour_table
aliased ast2
.
- It selects all columns (
- The logic is similar to the previous approach. If a row exists with a higher value within the group, the current row isn't deleted.
Choosing the right method:
- The method using
JOIN
might be more efficient for complex filtering conditions within the subquery. - The methods using
NOT EXISTS
can be simpler to write for basic scenarios. - Consider the complexity of your specific query and choose the method that best suits your needs.
mariadb