Add new column to a table

ALTER TABLE (Transact-SQL)

Modifies a table definition by altering, adding, or dropping columns and constraints. ALTER TABLE also reassigns and rebuilds partitions, or disables and enables constraints and triggers.

For more information about the syntax conventions, see Transact-SQL Syntax Conventions.

alter table [dbo].[console_games]
ADD global_sales float

Take a look at this if you want to know more about altering statements in SQL. https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-ver15

If we need to add a new column to our table however such a new table needs to calculate something, we can ask SQL to do it for us. Just like the following example:

update [dbo].[console_games] 
set [global_sales] = [na_sales] + [eu_sales] + [jp_sales] + [other_sales]

select * from [dbo].[console_games]

So we can now calculate the value thanks to SQL.

update [dbo].[console_games]
set [na_sales_percent] = [na_sales] / [global_sales] * 100
where [global_sales] > 0

select * from [dbo].[console_games]
USE heybaldurdb;

SELECT 
bm.Id AS BusinessId,
pro.Id AS ProblemId,
pro.Description AS ProblemDescription,
sol.Description AS SolutionDescription

	FROM businessmodels AS bm
INNER JOIN problems AS pro
ON pro.BusinessModelId = bm.Id
LEFT JOIN solutions AS sol
ON sol.BusinessModelId = bm.Id
WHERE bm.Id = 33 AND bm.ProjectId = 20

Last updated