Jump to content

Need Help with few queries please


ballouta

Recommended Posts

consider these four relations
        BookAuthor (book, author, earnings)
        BookReference (book, referenceBook, times)
        BookReview (book, reviewer, score)
        BookPublish (book, year, price, num)
 

*an author can review his book

 

1) I need a sql query that finds all the authors who reviewed more than two books written by 'ballouta' USING aggregators

2) I need a sql query that finds all the authors who reviewed more thna two books written by 'ballouta' with subqueries

3) Find all authors who have written exactly one book and reviewed more than one book

4) Find all reviewers who have reviewed everybook by 'ballouta' using Except'

 

thank you so much

Link to comment
https://forums.phpfreaks.com/topic/286470-need-help-with-few-queries-please/
Share on other sites

You have not specified the join condition between BookAuthor and BookReviewer (ie BA.col_x = BR.coly_y)

 

It more efficient to use an explicit join syntax.

 

EG

... 
FROM BookAuthor BA
    INNER JOIN BookReview BR ON BA.col_x = BR.col_y
 ...

so for question 1, would this query be correcy?

 

Select author, count(reviewers)
From BookAuthor BA
INNER JOIN BookReview BR ON
BA.book = BR.author
where BA.author = 'Charles Dickens'
Group by author
Having count(reviewers) > 2
 

May someone help me with Question no 2 please

so I have the correct query for question # 1

which is

Select reviewer, count(reviewer)
From BookAuthor BA
INNER JOIN BookReview BR ON
BA.book = BR.book
where BA.author = 'Charles Dickens'
Group by reviewer
Having count(reviewer) > 2

I need to convert it to subqueries in the where clause, thank you

I set up some test data.

 

Running this query

SELECT reviewer, COUNT(*)
FROM BookAuthor ba
INNER JOIN BookReview br ON ba.book = br.book
WHERE ba.author = 'ggg'
GROUP BY reviewer
HAVING COUNT(*) >= 2;

+----------+----------+
| reviewer | COUNT(*) |
+----------+----------+
| bbb      |        3 |
| ddd      |        2 |
| eee      |        2 |
| m        |        2 |
| o        |        2 |
| p        |        2 |
+----------+----------+

In my data m, o and p are reviewers but not authors and the question asked for authors so I then used a subquery to select only those reviewers that are also authors

SELECT reviewer, COUNT(*)
FROM BookAuthor ba
INNER JOIN BookReview br ON ba.book = br.book
INNER JOIN (
    SELECT DISTINCT author
    FROM bookauthor
    ) as ba2
    ON br.reviewer = ba2.author
WHERE ba.author = 'ggg'
GROUP BY reviewer
HAVING COUNT(*) >= 2;

+----------+----------+
| reviewer | COUNT(*) |
+----------+----------+
| bbb      |        3 |
| ddd      |        2 |
| eee      |        2 |
+----------+----------+

I could also have used

SELECT reviewer, COUNT(*)
FROM BookAuthor ba
INNER JOIN BookReview br ON ba.book = br.book
WHERE ba.author = 'ggg'
AND br.reviewer IN (SELECT author FROM bookauthor)
GROUP BY reviewer
HAVING COUNT(*) >= 2;

My data

 

  Reveal hidden contents

 

I just finished studying your queries, thanks again

I am thinking of how to wrie the same query without aggregators but with subqueries in the where clause

 

I have these idea, If it is true, I am not sure yet hot to translate it to a query

so the same query above without aggregator

SELECT reviewer, COUNT(*)
FROM BookAuthor ba
INNER JOIN BookReview br ON ba.book = br.book
WHERE ba.author = 'ggg'
AND br.reviewer IN (SELECT author FROM bookauthor)
GROUP BY reviewer
HAVING COUNT(*) >= 2;

I say:

after joining the two tables on the same criteria above

get a Distinct list of reviewers from the tuples (that are the result of the inner join)

then the "inner join tuples" Except (difference) the "distinct list"

will tell us who had reviewed 2 books or mores

 

what do you think?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.