SQLTeam.com | Weblogs | Forums

Query sql

I just started studying the world of queries and databases and I had a question:

having a color table, how do I solve the following query: "favorite color"

Thank you and good day

It all depends on data definition. What makes a color a favorite color? If you can answer that question you can solve it.

For example:
If I order 2 blue shirts and 1 red, is blue my favorite color? Then you should count the colors and pick the one with the most counts.

Maybe I'm allowed to pick my own favorite color?
So if I order 2 blue shirts and 1 red my favorite color can be yellow?

One question and so many possibilities :slight_smile:

sorry I expressed myself badly.

TABLE COLORS
row1 -> BLUE
row2 -> RED
row3 -> GREEN
row4 -> BLUE

the query must return BLUE because it is the most used data

one simple way



declare @sample table(color varchar(10))
insert into @sample
select 'BLUE' union all
select 'RED' union all
select 'GREEN' union all
select 'BLUE' 

select top 1 count(1), color
from @sample
group by color
order by 1 desc

or

SELECT distinct FIRST_VALUE(color) 
               OVER (ORDER BY COUNT(color) DESC) AS MostCommonColor   
FROM @sample
group by color
1 Like