Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:58:57+00:00 2026-06-10T01:58:57+00:00

Say I have two tables, author and book. Each author can have multiple books,

  • 0

Say I have two tables, author and book. Each author can have multiple books, identified by the authorId column of the book table which is linked to the id field of author table.

I want to fetch all the books with a certain category Id, and for each book I want to get the author’s name and photo url to be displayed next to the book title.

So I do a query like this:

SELECT author.name, author.photoUrl, book.title
    FROM author, book
    WHERE book.categoryId = '3' AND author.id = book.authorId

The problem is, what if an author has multiple books in the category? (E.g a fiction writer writes multiple fiction books). In that case, will the author’s info be fetched separately for each row, hence fetching duplicate info, or is there any way such as using DISTINCT so an author’s info is only fetched once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:58:58+00:00Added an answer on June 10, 2026 at 1:58 am

    Yes, it would return duplicates on the author but I dont think that is a problem.
    An alternative could be to have two queries instead, one for the books and one for the authors, and than in code set up a relationship (in a DataSet or using a Dictionary<int, Author>) between the results on AuthorId.

    Books

    SELECT 
      book.title,
      book.authorId
    FROM 
      book
    WHERE 
      book.categoryId = '3'
    

    Authors

    SELECT 
      author.Id,
      author.name, 
      author.photoUrl
    FROM
      author
    WHERE
      EXISTS(
         SELECT NULL
         FROM book
         WHERE 
           book.categoryId = '3' AND
           author.id = book.authorId
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. Lets say they look like this Table Sports: Column 1:
Say I have two tables, 'cars' and 'properties', linked by table 'car_properties'. I can
Lets say I have two tables: table english which has two columns, id and
Let's say I have two tables, Books and Reviews. Reviews has a column, stars,
Say I have two tables, users and groups each of which has an auto-incrementing
Say I have two tables, user and comment . They have table definitions that
Lets say I have two tables, one for transactions, and another table who's primary
Let's say I have two tables with several fields and in every table there
Let's say we have two tables, ORDERS and OFFERS Order POJO @Entity @Table(name =
Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.