SELECT SUM and remove similar rows
You need a join or a correlated subquery (with `EXISTS), in order to compare rows:

SELECT 
    SUM(c.available_supply * h.price) AS market_cap_usd_all 
FROM coins AS c
    JOIN hist_all AS h
    ON c.id = h.coins_id
WHERE h.price &lt;&gt; 0
  AND ( h.second_coin = 'USD' 
     OR 
        h.second_coin = 'USDT'
        AND NOT EXISTS
            ( SELECT 1
              FROM hist_all AS hi
              WHERE hi.first_coin = h.first_coin
                AND hi.second_coin = 'USD'
            )
      ) ;