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

  • Home
  • SEARCH
  • 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 7792361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:09:36+00:00 2026-06-01T22:09:36+00:00

I’m making a program and I need to make a query to the database

  • 0

I’m making a program and I need to make a query to the database asking for the string that appears most often in a given column. In this example, its “stringONE”.

----------------------------
|  ID  |  Column (string)  |
----------------------------
|  1   |     stringONE     |
----------------------------
|  2   |     stringTWO     |
----------------------------
|  3   |     stringONE     |
----------------------------
|  4   |     stringONE     |
----------------------------

Now I need to take the name of the string that appears the most and put it into a variable string, for example:

string most_appeared_string = sql.ExecuteScalar();

Also, what happens if there is no string that appears the most, rather 2 or more strings that appear the same amount of times, like this:

----------------------------
|  ID  |  Column (string)  |
----------------------------
|  1   |     stringONE     |
----------------------------
|  2   |     stringTWO     |
----------------------------
|  3   |     stringTWO     |
----------------------------
|  4   |     stringONE     |
----------------------------

Thanks ahead.


@KeithS

Do you have an sql-server version of the query because I’m getting some errors when trying it there. Here’s a table example of what I’d like to do precisely.

------------------------------------------------
|  ID  |  column1 (string) |  author (string)  |
------------------------------------------------
|  1   |     string-ONE    |       John        |
------------------------------------------------
|  2   |     string-TWO    |       John        |
------------------------------------------------
|  3   |     string-ONE    |      Martin       |
------------------------------------------------
|  4   |     string-ONE    |       John        |
------------------------------------------------

SELECT TOP (1) column1, COUNT(*) FROM table WHERE author='John' ORDER BY ID

It should return “string-ONE” since it appears the most (2) times for the author John. When trying the query in MS-SQL Management Studio though, this is the error I’m getting:

Column 'table.column1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Nevermind the edit. 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-06-01T22:09:38+00:00Added an answer on June 1, 2026 at 10:09 pm

    This is a pretty easy query (in T-SQL at least):

    select top 1 Column, Count(*) from Table group by Column order by Count(*) desc
    

    ExecuteScalar, by an implementation detail, will return the string value because it’s the first column of the only row in the result set, even though there are two columns. You could also use ExecuteReader to access the number of times that string occurs.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And

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.