Add Column to an Existing Table using ALTER TABLE

To add a column to an existing table using ALTER TABLE:

ALTER TABLE table_name
ADD new_column_name data_type

The Example

Suppose that you have a table called products.

The ‘products’ table currently contains the following columns and data:

product_idproduct_name
1Desktop Computer
2Laptop
3Tablet
4Monitor
5Printer

The ultimate goal is to add an additional column called ‘price‘ with the following values (as highlighted in yellow):

product_idproduct_nameprice
1Desktop Computer800
2Laptop1200
3Tablet200
4Monitor350
5Printer150

You can then use the template below in order to add the ‘price‘ column:

ALTER TABLE table_name
ADD new_column_name data_type

For our example:

  • The table_name is: products
  • The new_column_name is: price
  • The data_type is: int

So the complete ALTER TABLE query for our example would look as follows:

ALTER TABLE products
ADD price int

Once you run the above query, the new column would be added:

Commands completed successfully

Run the following SELECT query to see the results:

SELECT * FROM products

Notice that the ‘price‘ column was added at the end of the table, where each value under this column is currently NULL:

product_idproduct_nameprice
1Desktop ComputerNULL
2LaptopNULL
3TabletNULL
4MonitorNULL
5PrinterNULL

You can then use an UPDATE query to replace those NULL values with the actual prices:

UPDATE products
SET price =
CASE WHEN product_id = 1 THEN 800
WHEN product_id = 2 THEN 1200
WHEN product_id = 3 THEN 200
WHEN product_id = 4 THEN 350
WHEN product_id = 5 THEN 150
END

Run the above UPDATE query.

Then, rerun the following SELECT query to see the new values:

SELECT * FROM products

You’ll now see the actual prices:

product_idproduct_nameprice
1Desktop Computer800
2Laptop1200
3Tablet200
4Monitor350
5Printer150

So far, you have seen how to add a column with integer values.

Alternatively, you can add a column with string/text values.

For example, add another column called ‘brand‘ that contains strings. The data type for this column would be nvarchar(50):

ALTER TABLE products
ADD brand nvarchar(50)

Run the above query, and the new ‘brand‘ column would be added:

Commands completed successfully

Rerun the following SELECT query to see the results:

SELECT * FROM products

As you can see, the ‘brand‘ column was added at the end of the table with NULL values:

product_idproduct_namepricebrand
1Desktop Computer800NULL
2Laptop1200NULL
3Tablet200NULL
4Monitor350NULL
5Printer150NULL

Update the values under the ‘brand‘ column (since we are dealing with strings, place the brand values inside quotes):

UPDATE products
SET brand =
CASE WHEN product_id = 1 THEN 'A'
WHEN product_id = 2 THEN 'B'
WHEN product_id = 3 THEN 'C'
WHEN product_id = 4 THEN 'D'
WHEN product_id = 5 THEN 'E'
END

Finally, run the SELECT query below in order to see the results:

SELECT * FROM products

You’ll see the brand values at the end of the table:

product_idproduct_namepricebrand
1Desktop Computer800A
2Laptop1200B
3Tablet200C
4Monitor350D
5Printer150E

Leave a Comment