Why? Sometimes the id isn't a sequence. Sometimes you might not know the name of the sequence. Though rare, sequences can be renamed, meaning you'd have to change all your queries referencing it. Sometimes the id is passed in from the user (common for UUIDs and custom id types like ULID). Sometimes it's an autogenerated UUID instead of a int/bigint.
With this variation you can handle any id type, not just ones from sequences, AND it can be included in either a separate transaction or the current one due to no longer needing explicit BEGIN/COMMIT (CTEs are considered a single statement), AND it allows you insert multiple employees at the same time rather than just one at a time!
As usual there is no MySQL equivalent, because MySQL doesn't support the RETURNING clause and therefore does not support this level of flexibility.
There's a better option for Postgres.
Why? Sometimes the id isn't a sequence. Sometimes you might not know the name of the sequence. Though rare, sequences can be renamed, meaning you'd have to change all your queries referencing it. Sometimes the id is passed in from the user (common for UUIDs and custom id types like ULID). Sometimes it's an autogenerated UUID instead of a int/bigint.
With this variation you can handle any id type, not just ones from sequences, AND it can be included in either a separate transaction or the current one due to no longer needing explicit BEGIN/COMMIT (CTEs are considered a single statement), AND it allows you insert multiple employees at the same time rather than just one at a time!
As usual there is no MySQL equivalent, because MySQL doesn't support the RETURNING clause and therefore does not support this level of flexibility.
thanks for sharing!