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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:05:19+00:00 2026-06-18T03:05:19+00:00

I got an interview question where there’s a Car sale modeled in a DB.

  • 0

I got an interview question where there’s a Car sale modeled in a DB. Each Car represents a physical car in a Car sale which refers to a Make and a Model table. A Sale table keeps track of each Car that is sold. A Sale only consists of one Car, so there’s a record in Sale per every unique Car that had been sold.

The question was to find-out the name of the most sold Model in the car sale. I answered with a 3-level nested query. The interviewer specifically asked for a solution using joins where I only succeeded in just joining the tables without the aggregates.

How would you join 3 tables as below (Car, Make, Sale) while using two other aggregates?

Here’s a rough sketch of the schema. The most sold Model here should return ‘Corolla’

Car

| carid|  modid | etc...
_________________
|  1   |    1   |
|  2   |    1   |
|  3   |    1   |
|  4   |    2   |
|  5   |    2   |

Make

| mkid |  name  |
_________________
|  1   | Toyota |
|  2   | Nissan |
|  3   |  Chevy |
|  4   |  Merc  |
|  5   |  Ford  |

Model

| modid|  name   | mkid |
________________________
|  1   |  Corolla|  1
|  2   |  Sunny  |  2
|  3   |  Carina |  1
|  4   |  Skyline|  2
|  5   |  Focus  |  5

Sale

|  sid |  carid | etc...
_________________
|  1   |    1   |
|  2   |    2   |
|  3   |    3   |
|  4   |    4   |
|  5   |    5   |

Edit:

Using MS SQL Server 2008

Output needed:

Model Name |  Count
_____________________
Corolla    |    3

i.e. The model of the Car that has been sold the most.
Notice only 3 Corollas and 2 Sunnys are in the Car table while Sale table corresponds to each of those with other sales detail. The 5 Sale records are actually Corolla, Corolla, Corolla, Sunnnu and Sunny.

  • 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-18T03:05:20+00:00Added an answer on June 18, 2026 at 3:05 am

    Since you are using SQL Server 2008, make use of Common Table Expression and Window Function.

    WITH recordList
    AS
    (
        SELECT  c.name, COUNT(*) [Count],
                DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) rn
        FROM    Sale a
                INNER JOIN Car b
                    ON a.carid = b.carID
                INNER JOIN Model c
                    ON b.modID = c.modID
        GROUP   BY c.Name
    )
    SELECT name, [Count]
    FROM recordList
    WHERE rn = 1
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this as an interview question ... infinite array which is sorted and
Got asked this interesting interview question today. Explain in detail the process by which
It's an interview question: There are 1 billion cell-phone numbers which has 11 digits,
I got this question at an interview and at the end was told there
Got this interview question which i'm wondering about: A software company designed an app
I got this in an interview question -- the question was more about what
I got asked the following question in an interview while talking about concurrency in
I just got this question on an interview and had no idea how to
My girlfriend got this question in an interview, and I liked it so much
Unusual interview question I got. Being completely agnostic about the platform, what are some

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.