Skip to content
Sharp

SQL for Java Devs · interview question

Given a one-to-many relationship between 'products' and 'reviews', how can you retrieve all products with their reviews in a single SQL query while ensuring each product appears only once in the result set?

Multiple choiceHardchapter-8-curatedpremiumsql-for-java-devsjoins
Choose one answer
Answer and explanation

Correct answer

C. SELECT products.*, COUNT(reviews.id) FROM products JOIN reviews ON products.id = reviews.product_id GROUP BY products.id

Why

Using COUNT and GROUP BY allows you to retrieve each product only once, while aggregating associated reviews. This technique provides a clean and organized result set.

Test yourself on SQL for Java Devs

A free test with instant, explained results. No account needed.

Start free test
All SQL for Java Devs questions →