Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Oracle Implicit vs Explicit Joins and MySQL One-to-Many Aggregation

Tech 1

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 DISTINCT to 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.