Oracle Implicit vs Explicit Joins and MySQL One-to-Many Aggregation
In Oracle, the queries SELECT * FROM a, b WHERE a.id = b.id and SELECT * FROM a INNER JOIN b ON a.id = b.id are functionally equivalent. Both produce the same result set by matching rows from tables a and b where the id values are equal. However, they differ in syntax style and clarity.
The comma-based syntax (FROM a, b) represents a implicit join. Without a WHERE clause, it results in a Cartesian product—every row of a paired with every row of b. Adding WHERE a.id = b.id filters this product to only matching pairs, effectively turning it into an inner join. In contrast, the explicit INNER JOIN ... ON syntax clearly separates the join logic from filtering conditions, improving readability and maintainability.
While both forms yield identical execution plans in modern Oracle versions (assuming equivalent predicates), the explicit JOIN syntax is strongly preferred as it adheres to SQL standards and avoids ambiguity, especially in complex multi-table queries.
In MySQL, handling one-to-many relationships often requires aggregating multiple related rows into a single field. For example, a product may have multiple models, and the goal is to list all models in one column per product.
This can be achieved using the GROUP_CONCAT() function:
SELECT
p.id AS product_id,
p.product_name,
p.type,
(SELECT GROUP_CONCAT(pm.model)
FROM product_model pm
WHERE pm.product_id = p.id) AS models
FROM product p;
GROUP_CONCAT() supports several options:
-
Deduplication: Use
DISTINCTto remove duplicates:GROUP_CONCAT(DISTINCT pm.model) -
Ordering: Sort values within the concatenated string:
GROUP_CONCAT(pm.model ORDER BY pm.model ASC) -
Custom delimiter: Replace the default comma with another character:
GROUP_CONCAT(pm.model SEPARATOR ';')
A complete example on a table testgroup with columns id and score:
-- Basic aggregation
SELECT id, GROUP_CONCAT(score) FROM testgroup GROUP BY id;
-- With deduplication
SELECT id, GROUP_CONCAT(DISTINCT score) FROM testgroup GROUP BY id;
-- With sorting
SELECT id, GROUP_CONCAT(score ORDER BY score DESC) FROM testgroup GROUP BY id;
-- With custom separator
SELECT id, GROUP_CONCAT(score SEPARATOR '|') FROM testgroup GROUP BY id;
Note that GROUP_CONCAT() has a default maximum length (controleld by group_concat_max_len), which may truncate large results if not adjusted.