T-SQL reference › uniqueidentifier

uniqueidentifier (GUID) in SQL Server

A uniqueidentifier is a 16-byte GUID. Systems use them when ids must be unique across machines. They are painful to type and make a clustered index jump around if you use NEWID().

Make one

SELECT NEWID() AS random_guid;

At work, if the table already has an int identity, keep using that for joins. Join on the GUID only when that is the real key.

WHERE id = 'the-guid-without-quotes'

It is a literal. Quote it. And do not SELECT * and paste a 36-character id into Slack if a customer number exists.

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Can I get a customer contact list? Just first name, last name and email.” Write the query, run it, send the results. Free.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets