Advertisement
Intermediate SQL Server, Data Types & Schema Design

How do computed columns work in SQL Server, and what are some practical use cases for them?

Short answer

A computed column automatically calculates its value based on an expression involving other columns in the same table, rather than storing a value that must be manually kept up to date, which is useful for things like calculating a full name from first and last name columns or a…

Save SQL Server mock interview
Advertisement
AModel answer
A computed column automatically calculates its value based on an expression involving other columns in the same table, rather than storing a value that must be manually kept up to date, which is useful for things like calculating a full name from first and last name columns or a total price from quantity and unit price.
CREATE TABLE OrderItems (
  Quantity INT,
  UnitPrice DECIMAL(10,2),
  TotalPrice AS (Quantity * UnitPrice)
);
Real-world example
An order line item table automatically calculates the total price for each item using a computed column, ensuring that value always stays accurate and consistent without requiring any application code to calculate and maintain it separately.
Likely follow-ups
  • Can a computed column be persisted and indexed for better performance?
  • What happens if one of the columns used in the computed column's formula changes?
Related topics