14

I want to add a column in a query that does not exist in a table and return it as a result. So lets say TABLE_TEST has column A, B and I want to return values for A, B and C. I am trying to do

SELECT A, B, C=3 FROM TABLE_TEST

or

SELECT *, C=3 FROM TABLE_TEST

Can this be done in MySQL, Postgresel or MSSQL?

1
  • Further to the answers below: If you need a string instead of an integer, you need single quotes around it. (That is, while select 3 as c from table works, select dummy as c from table or select "dummy" as c from table try to look for a column already named dummy, so you need select 'dummy' as c from table. Might vary with your SQL engine. Via stackoverflow.com/questions/2504163/… and stackoverflow.com/questions/5185743/… Commented Oct 29, 2016 at 23:53

3 Answers 3

34

Yes, sure:

select a, b, 3 as c from table_test

That's it. It works on three db engines you've mentioned.

Sign up to request clarification or add additional context in comments.

1 Comment

You could also do 3 as "My Fake Column" if you want to incorporate spaces in the column name.
11

You should use:

SELECT A,B, 3 AS C FROM TABLE_TEST

1 Comment

You are missing a comma after B, which makes the query invalid.
3

you can use as

Select a,b, 3 as c from table

This is known as alias

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.