0

I'm trying to do something like:

select fred.name
  , fred.emp_num
from fred
  , joe
  , (select * from
      (select yr_bonus
        , 1 grade
      from mark 
      where mark.department = fred.department)
      union
      (select
        0 yr_bonus
        , 2 grade
      from dual)
    ) pay
where fred.division = joe.division
and fred.grade = pay.grade;

This doesn't work. I can't seem to figure out how to tie mark.department to fred.department from 2 levels deep in selects. How do I accomplish this?

2 Answers 2

2

Because in the FROM clause you can't access another table, you must use either JOINS or join tables in where clause. Besides, your query doesn't seem right, describe what are you trying to do?

If I understood correctly, your query can be rewritten like this:

select fred.name
  , fred.emp_num
from fred
  , joe
where fred.division = joe.division
and ((fred.grade = 1 and fred.department in (select department from mark) 
or (fred.grade = 2))
Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense. What I'm actually trying to do involves significantly more complex queries, in which I have to integrate a few existing queries as subqueries of a new one. This answers my question though - you can't, use joins.
1
SELECT  fred.name
,       fred.emp_num
FROM    fred
JOIN    joe
ON      fred.division = joe.division
JOIN
(
        SELECT  *
        FROM
        (
            SELECT  yr_bonus
            ,       1 grade
            FROM    mark
            UNION
            SELECT  0 yr_bonus
            ,       2 grade
            FROM    dual
        ) mark
        JOIN    fred
        ON      mark.department = fred.department
) pay
ON      fred.grade = pay.grade;

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.