I’m selecting rows from a table
select name,age from tabl1
I need to add another column which should have the same value for the whole result set.
the value is comming from:
select value from config where key = 'company'
How can I combine this into one query ?
The resultset should look like
Name1 | Age1 | SameComapanyName
Name2 | Age2 | SameComapanyName
Name3 | Age3 | SameComapanyName
Name4 | Age4 | SameComapanyName
Added:
It’s maybe more appropriate when there is need to return more than one column at time from specific table.
There is a difference in behaviour between this and subquery as a column. When condition
c.key = 'company'isn’t met the join won’t return records at all, in case on subquery it returnsnullvalue for each row.Added (after @alex comment):
cros joinreturnsNxMrecords that is true but not in this case. Herewherecondition limitsMto specific row/-s only. Sometimes NxM result might be even requirement then subquery won’t fulfil it.Query (subquery version) fails when subquery returns more than one row or more than one column (that’s why other answers limits returned rows in subquery to one by
topormaxwhich is not always sufficient).cross joinwill do it without any problems, it’s is more elastic.Tests done on tables:
Before each query I’ve run:
Query:
Execution plan:
Stats:
Query:
Execution plan:
Stats:
Query:
Execution plan:
Stats:
The test would be better if I had done it many times and calculated the average but I had no time for that.