Junior — Middle
How is a clustered index defined and used in SQL, and how does it differ from a non-clustered index?
sobes.tech AI
Answer from AI
A clustered index in SQL determines the physical order of data storage in a table. This means that the table rows are ordered on disk according to the clustered index key. Usually, a table can have only one clustered index, as data can be sorted in only one way.
A non-clustered index is a separate structure that contains keys and pointers to the physical data rows but does not change their physical order. This index is similar to a table of contents in a book: it helps quickly find the needed record, but the data itself remains in the original order.
Example:
- Clustered index on the "ID" column — data is physically sorted by ID.
- Non-clustered index on the "Name" column — a separate structure is created for fast search by name, but the data is not rearranged.
Important differences:
- The clustered index affects the physical order of data.
- The non-clustered index is a separate structure that does not change the data order.