1

I know this question will be easy for most people. But I really expected that this would work. Why doesn't this query work?

select name, price,  select num from ( select 1 as num) as value 
from products;

I've got this:

error at or near select

3 Answers 3

1

You can do it with single SELECT statement :

select name, price, 1 as value 
from products;

Single outer SELECT statement should contains only columns/expressions, if it has other SELECT statements then it should be in subquery form.

something like that :

select t.col1, t.col2 t.col3,
       (select t1.col from table t1 where . . . ) as col_name
from table t;
Sign up to request clarification or add additional context in comments.

Comments

0

answer depends on what you want to do exactly.

remember that select 1 from table is a correct sql query, which returns "the first column" from the table

it explains why you have a syntax error in your query. the from table is missing in your (select 1 as num)

so let's confirm what your want exactly

Comments

0

Why does this fail?

select name, price,  select num from ( select 1 as num) as value 
from products;

select is a SQL keyword that indicates either a new query or a subquery. When used with a subquery, it must be preceded by its own parentheses. Hence, you get a syntax error.

If you add the parentheses:

select name, price,  (select num from ( select 1 as num)) as value 
from products;

You will get a different error, because the nested subquery is missing a table alias.

In the end, you can fix this to what you presumably intend:

select p.name, p.price, (select num from (select 1 as num) x) as value from products p;

Comments

Your Answer

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