logoalt Hacker News

Joker_vDyesterday at 5:16 PM2 repliesview on HN

> Then query it like so:

    SELECT DISTINCT pokemon, special_attack
    FROM pokemon as p
    WHERE
      p.special_attack > 120
      AND EXISTS (
        SELECT 1
        FROM pokemon_moves as pm
        WHERE p.pokemon_name = pm.pokemon_name AND move = 'freezedry'
      )
      AND EXISTS (
        SELECT 1
        FROM pokemon_types as pt
        WHERE p.pokemon_name = pt.pokemon_name AND type = 'ice'
      );
Hmm. I wonder if this

    SELECT DISTINCT pokemon, special_attack
    FROM pokemon as p
      NATURAL JOIN pokemon_moves as pm
      NATURAL JOIN pokemon_types as pt
    WHERE
      p.special_attack > 120 AND
      pm.move = 'freezedry' AND
      pt.type = 'ice'
    ;
would work instead.

Replies

sgarlandyesterday at 5:42 PM

It would, but it forces the requirement of DISTINCT. With the original, if there were declared PKs (pokemon_name is fine for the main table, with a composite for others), the semi-join (EXISTS) would eliminate the need for DISTINCT entirely.

I think. Doing this in my head, but you could verify it trivially with SQLite or any other RDBMS.