Search code examples
mysqlsqlsubqueryinner-joinrelational-algebra

Simplify nested queries


select name 
from person, author, article
where name != "John Doe" AND
      person.pid = author.pid AND
      author.aid = article.aid AND
        title = select title
                from author, person, article
                where person.name = "John Doe" AND
                      author.pid = person.pid AND
                      article.aid = author.aid

The above are the nested queries I wrote in sqlite for a relational algebra that outputs the names of the people who coauthored an article with John Doe.

Here's the relational schema:
Article(aid, title, year, confid, numpages) Author(aid, pid) Person(pid, name, affiliation).
My question how can I simplify the nested queries?


Solution

  • I don't see how your question relates to regular expressions at all. If you want the names of the persons that co-authored with John Doe though, I would recommend joins:

    select distinct pe2.name
    from person pe1
    inner join author  au1 on au1.pid = pe1.pid
    inner join author  au2 on au2.aid = au1.aid and au2.pid <> au1.pid
    inner join person  pe2 on pe2.pid = au2.pid
    where pe1.name = 'John Doe'
    

    The query starts from person John Doe, and brings the corresponding rows in author; then, it searches for all co-authors, and finally brings their names.