1

I want to ask whether we can use subqueries inside a sql function (consider both group and single functions). I've been trying out this simple query -

select count(select empno from emp where sal<3000) from emp;

It's giving error "missing expression". This is just a simple example.

I just want to know if this query logic is wrong or if it is not allowed in sql and if we can use subqueries inside sql function please care to mention an example.

Thanks.

3 Answers 3

1

Your query simply does not make sense. Why wouldn't you just do this?

select count(empno) from emp where sal<3000;
Sign up to request clarification or add additional context in comments.

2 Comments

While the query is silly, I think he's trying to ask if the concept of select count(subquery) is possible.
I know that I can use this query rather than that one. But I used this example to make people understand what my question is about. In nutshell, I want to know whether we can use subqueries inside functions or not.
0

I think it is possible. See http://www.akadia.com/services/sqlsrv_subqueries.html for an example (find "DATEDIFF" on the page to see it). However, I'm not sure it is possible in all flavors of SQL; for example, I use SAS, which has its own fairly bare-bones SQL implementation, and it does not seem to allow it.

Comments

0

Even if you are able to use a subquery it will have to essentially return an expression or column name which can be evaluated and counted by looping through the records. Hence you should try to look at using a subquery result as a column in your query or any other combination of joins to do your job.

1 Comment

This is the right answer, but it's confusingly stated. A subquery returns a result set - essentially another relation. In general, functions take values or columns, not relations. If you need to use a subquery, use it as a relation and apply your function to the proper parts of that relation.

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.