Sqlite: Setting default value for a max sub-query if result is null
In these situations, function COALESCE()
comes very handy:
UPDATE orderSET class = 5, sequence = coalesce( (SELECT max(sequence) FROM order WHERE class=5), 0 ) + 1WHERE order_id = 104
Another good thing about COALESCE
that it is supported by most other SQL engines - MySQL, Postgres, etc...