How to add the same thing to every row in a column in SQL?

Viewed 537

Im not sure why I cant find this online but I simply want to tell a query to have a new column which is the same thing for every row.

So something like this:

ID    Pollo  
001    True
002   False
003    True
004   False
005    True

And I wanna make it this where I add a variable called Tag and it is strictly the word "Mark" for every row:

ID    Pollo  Tag  
001    True   Mark
002   False   Mark
003    True   Mark
004   False   Mark
005    True   Mark
2 Answers

You can add a constant expression as a column in a query's select-list, and an expression can be a simple value:

SELECT ID, Pollo, 'Mark' AS Tag
FROM mytable;

Since this value is a constant, it will be the same on every row returned by the query.

You should assign the column an alias by using the AS keyword. This becomes the header for the column.

The data type for the column is implied by the expression. In this case, it's a varchar.

If you want to add the data to the table:

alter table yourmysterytablename add column Tag varchar(desiredmaximumlength);
update yourmysterytablename set Tag='Mark';

If you just want to add it to your select results:

select ID, Pollo, 'Mark' as Tag
from yourmysterytablename
where ...
Related