Java EE 7 Application Developer Exam: JPQL Query Example

Books with 500-750 Pages - Answer

Question

Given the JPQL code fragment: Select pub.title, pub.author, pub.pages FROM Publisher pub Which two clauses do you add to this JPQL query to retrieve only those books with between 500 and 750 total pages? (Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

AB.

The given JPQL code fragment is:

rust
SELECT pub.title, pub.author, pub.pages FROM Publisher pub

This query retrieves the title, author, and pages of all the books published by all the publishers. To retrieve only those books with between 500 and 750 total pages, we need to add two clauses to the query using the WHERE keyword.

Let's examine the given answer choices:

A. WHERE MIN(pages) >= 500 AND MAX(pages) <= 750

This clause uses the aggregate functions MIN and MAX to find the minimum and maximum number of pages in all the books published by a publisher, respectively. Then it checks whether the minimum number of pages is greater than or equal to 500 and the maximum number of pages is less than or equal to 750. However, this clause is incorrect because it is not possible to use aggregate functions directly in the WHERE clause.

B. WHERE pub.pages <= 500 OR pub.pages >= 750

This clause uses the OR operator to retrieve books with either less than or equal to 500 pages or greater than or equal to 750 pages. This clause is incorrect because it retrieves books with pages outside of the range of 500 to 750.

C. WHERE pub.pages BETWEEN 500 AND 750

This clause uses the BETWEEN keyword to retrieve books with pages between 500 and 750, inclusive. This is the correct clause to retrieve books with between 500 and 750 total pages.

D. WHERE pub.pages <= 500 AND pub.pages >=750

This clause uses the AND operator to retrieve books with less than or equal to 500 pages and greater than or equal to 750 pages. This clause is incorrect because it is not possible for a book to have less than or equal to 500 pages and greater than or equal to 750 pages at the same time.

Therefore, the correct clauses to add to the given JPQL query to retrieve only those books with between 500 and 750 total pages are:

  • WHERE pub.pages BETWEEN 500 AND 750 (option C)
  • There is no second correct answer.