SQL Server Interview Question #30

What is the difference between NEWID() and NEWSEQUENTIALID()?

Data Types, Keys & Constraints Junior Beginner

Detailed Explanation

NEWID() generates a GUID with values that are effectively non-sequential for indexing purposes. NEWSEQUENTIALID() generates GUID values that are more sequential on a given machine, which can reduce page splits when the GUID column is indexed.

NEWSEQUENTIALID() has usage restrictions: it is typically used as a DEFAULT constraint for a UNIQUEIDENTIFIER column and cannot simply be called anywhere like NEWID().

The choice depends on requirements such as distribution, predictability, indexing behavior, and identifier exposure.

Code Example

CREATE TABLE Orders
(
    Id UNIQUEIDENTIFIER NOT NULL
        DEFAULT NEWSEQUENTIALID(),
    OrderNumber NVARCHAR(50) NOT NULL,
    CONSTRAINT PK_Orders PRIMARY KEY (Id)
);