-2
select fname,lname,salary from employee where exists
(select essn from dependent where employee.sex = dependent.sex); 

this is the query

philipxy
  • 14,867
  • 6
  • 39
  • 83
  • 1
    This is not clear. Do you mean the algebra has to have exists (which doesn't make sense) or just the SQL or both? PS Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect your research. [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/3404097) [How much research effort is expected?](https://meta.stackoverflow.com/q/261592/3404097) – philipxy Jan 09 '22 at 11:25
  • There are many RAs (relational algebras). They differ in operators & even what a relation is. Give operator definitions & your reference for yours. Eg textbook name, edition & page. Define "convert"--Same result? Same structure? Nested algebra calls form a programming language. So give as much of a [mre] as you can. But--Google 'run relational algebra online'. Please show what parts you are able to do. See [ask], other [Help] links, hits googling 'stackexchange homework' & the voting arrow mouseover texts. – philipxy Jan 09 '22 at 11:27
  • 1
    As @philipxy says, there are many variants of RAs. Few of them include a 'keyword' corresponding to SQL `exists` -- because you don't need it. So I think your question is asking "convert a sql query containing exist keyword to relational algebra"? (but it's not clear). If your variant of RA includes an `exists`, _and_ you are required to use it for this exercise, please give full details of the operators in your variant. – AntC Jan 09 '22 at 21:22
  • Rewriting an SQL query to remove EXISTS is also a faq. – philipxy Jan 09 '22 at 21:33
  • [How to represent "not exists" in relational algebra?](https://stackoverflow.com/q/3738070/3404097) [How to model or query for something that does not exist in relational algebra](https://stackoverflow.com/q/55663848/3404097) – philipxy Jan 09 '22 at 23:31

1 Answers1

0

Nicer SQL, avoiding exists:

select distinct fname,lname,salary from employee
                                 join dependent on employee.sex = dependent.sex;

Nicer nicer SQL, but only works if sex is the only column in common between tables employee, dependent

select distinct fname,lname,salary from employee
                                 natural join dependent;

Relational Algebra Appendix A style (with the same proviso)

(employee JOIN dependent) {fname,lname,salary}

Relational Algebra Codd 1972 style (with the same proviso)

π<fname,lname,salary>(employee ⋈ dependent)
  • (putting the attribute names in < >, because subscripting doesn't work in SO's code.)
AntC
  • 2,623
  • 1
  • 13
  • 20