-- Clean tables
DROP TABLE IF EXISTS utxos;
DROP TABLE IF EXISTS number_of_utxos;
DROP TABLE IF EXISTS id_of_max_utxo;
CREATE TABLE utxos (output_id int, value bigint); 
CREATE TABLE number_of_utxos (utxo_count int); 
CREATE TABLE id_of_max_utxo (max_utxo int);

-- Get all utxos

INSERT INTO utxos
SELECT output_id, value 
FROM outputs
WHERE NOT EXISTS (
    SELECT * FROM inputs
    WHERE inputs.output_id = outputs.output_id
    );

-- Get number of utxos

INSERT INTO number_of_utxos
SELECT COUNT(output_id) FROM utxos;

-- Get highest valued utxo

INSERT INTO id_of_max_utxo
SELECT output_id FROM utxos
ORDER BY value DESC
LIMIT 1;

