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 8610747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:12:06+00:00 2026-06-12T04:12:06+00:00

In a recent programming interview, I was asked an SQL question to which I

  • 0

In a recent programming interview, I was asked an SQL question to which I gave what I thought was a reasonable answer, but my answer elicited strong disapproval from the dba, and I wasn’t able to figure out why.

Since then, I have thought about the problem some more, and I was unable to figure out what was so horrible about my answer, so I am seeking enlightenment here to find out the right way, or failing that, better ways of producing a report of libraries and the number of books in them from a database containing a table of libraries and a table of books.

I should note that I have changed the scenario a bit so that the wording is not identical to the interview question, but the task is the same.

Here is a minimal schema for the problem:

create table library (
  id integer primary key,
  name char(8)
);

create table book (
  id integer primary key,
  name char(8),
  library_id integer,
  foreign key (library_id) references library(id)
);

The task is to list names of libraries and the number of books in them for libraries with two or more books.

And, here is my proposed solution:

select
  a.name as name,
  b.nbooks as nbooks
from
    library as a,
    (
        select
            min(library_id) as library,
            count(id) as nbooks
        from
            book
        group by 
            library_id
    ) as b
where
    ( nbooks > 1 ) and (a.id = b.library)
;

On second thought, using an explicit inner join might have been better. Other than that, could you please point out to me the potential pitfalls (either in general or in relation to a particular database) and the correct way to generate this report?

  • 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-12T04:12:07+00:00Added an answer on June 12, 2026 at 4:12 am

    Here is a simple way of doing this:

    select l.name, count(*) as numbooks
    from library l join
         books b
         on l.id = b.library_id
    group by l.name
    having count(*) > 1
    

    Your answer is technically ok. The DBA probably doesn’t care about certain stylistic things that others might (such as using “a” as the alias for library rather than “l”). The subquery is unnecessary, and the min(library_id) sticks out as unnecessary. You can apply aggregate functions to the group by columns, but that is typically not done.

    The biggest problem — which the DBA may be responding to — is having the join condition in the WHERE clause rather than in an ON clause. This is dangerous, because if you leave it out or make what seems like an innocent modification, the query can become a CROSS JOIN instead of an INNER JOIN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recenty asked a question about parallel programming algorithms which was closed quite fast
In a recent interview, I was asked a really strange question. The interviewer asked
This was a question I was asked at my recent interview and I want
I've written a simple linked list because a recent interview programming challenge showed me
In a recent question I asked about the fastest way to convert a large
This is a question from the most recent version of Stroustrup's The C++ Programming
I recently got asked this during an interview. As a recent graduate, and only
this is a follow up to my recent question ( Code for identifying programming
I recently asked a question about functional programming, and received (good!) answers that prompted
The recent upgrade to Dreamweaver CS5.5 finally installs the Android SDK correctly, but when

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.