With your original SQL as base (if you want to sort first for id and then for sub_id):
$sql="select * from gallery group by sub_id order by id desc, subid desc limit 0,4";
If it is not intended to eliminate the "duplicates", then the following sql could be used instead of the above one:
$sql="select * from gallery order by id desc, subid desc limit 0,4";