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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:18:29+00:00 2026-05-13T12:18:29+00:00

Let’s imagine that we are having cars table with such a simple structure… car_id

  • 0

Let’s imagine that we are having “cars” table with such a simple structure…

car_id INT
color ENUM('black','white','blue')
weight ENUM('light','medium','heavy')
type ENUM('van','sedan','limo')

Fist, I’m selecting car (1, black, heavy, limo), then I’d like to get list of related cars sorted by number of matching columns (without any column weight). So, first I’m expecting to see (black, heavy, limo) cars, then I’m expecting to see cars with only 2 matching fields etc.

Is it possible to execute this kind of sorting using SQL?

Sorry for my English, but I really hope that my question is clear for you.

Thank you.

  • 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-05-13T12:18:29+00:00Added an answer on May 13, 2026 at 12:18 pm

    There are probably a few ways to optimise the sub-queries, but without using case statements or sub-optimal join clauses:

    select
            *
        from
            (
                select
                        selection.CarId,
                        selection.Colour,
                        selection.Weight,
                        selection.Type,
                        3 as Relevance
                    from
                        tblCars as selection
                    where
                        selection.Colour = 'black' and selection.Weight = 'light' and selection.Type = 'van'
                union all
                select
                        cars.CarId,
                        cars.Colour,
                        cars.Weight,
                        cars.Type,
                        count(*) as Relevance
                    from
                        tblCars as cars
                    inner join
                        (
                            select
                                    byColour.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byColour
                                on
                                    cars.Colour = byColour.Colour
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byColour.CarId <> cars.CarId
                            union all
                            select
                                    byWeight.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byWeight
                                on
                                    cars.Weight = byWeight.Weight
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byWeight.CarId <> cars.CarId
                            union all
                            select
                                    byType.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byType
                                on
                                    cars.Type = byType.Type
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byType.CarId <> cars.CarId
                        ) as matches
                    on
                        cars.CarId = matches.CarId
                    group by
                        cars.CarId,
                        cars.Colour,
                        cars.Weight,
                        cars.Type
            ) as results
        order by
            Relevance desc
    

    Output:

    CarId   Colour  Weight  Type    Relevance
    1       black   light   van     3
    3       white   light   van     2
    4       blue    light   van     2
    5       black   medium  van     2
    6       white   medium  van     1
    7       blue    medium  van     1
    8       black   heavy   limo    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I have an int that represents the number of rows in a
Let's say I have the following, declare @A table (a int) insert into @A
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that I have an arbitrary string like `A man + a plan
Let's say that I'm currently designing an application where I will need to use

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.