O erro é: Isso vai resolver: SELECT cr.plate, car_id_seq.nextval FROM (SELECT DISTINCT plate FROM cars_rentals) cr