user14288342
user14288342

Reputation: 27

SQL query to check based on if exists condition

I need a help on sql query where I want to fetch the data if the condition exists or not.

select employee.*, stream.*, priority.*
from employee emp,
     stream st,
     priority pr
where emp.st.id = st.id
  and str.pr.id = pr.id

So here priority is not mandatory. A value may or may not exists. So If priority does not exist the data should be fetched for emp.st.id = st.id. If it exists then ofcourse emp.pr.id = pr.id should be included.

Thanks in advance

Upvotes: 0

Views: 215

Answers (2)

Prabhu
Prabhu

Reputation: 1

I hope following suggestion will help you,

select employee.*, stream.*, priority.*
from employee emp,
     stream st,
     priority pr
where emp.st.id = st.id
  and str.pr.id = pr.id(+)

for more details about left/right outer join, you can check below link. https://docs.oracle.com/cd/B28359_01/server.111/b28286/queries006.htm#SQLRF52331

Upvotes: 0

Littlefoot
Littlefoot

Reputation: 142713

This is what you have & said:

select employee.*, stream.*, priority.*
from employee emp,
     stream st,
     priority pr                     --> priority is not mandatory
where emp.st.id = st.id
  and str.pr.id = pr.id

Translated to from English to SQL, it reads as "outer join" which means that something like this might do what you want:

select e.*, 
       s.*
       p.*
from employee e join stream s   on s.id = e.st_id
           left join priority p on p.id = s.pr_id         --> this

Upvotes: 1

Related Questions