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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:11:07+00:00 2026-05-24T18:11:07+00:00

I’m new to MySQL and am trying to learn more about the JOIN function.

  • 0

I’m new to MySQL and am trying to learn more about the JOIN function.

Let’s say I have the following code and I want to find the count for each level of
the variable and then join the two column.
So the data looks like:

in_clicks:
one
one
one
two 
three
four
four 
four 
four
four
five 
five
six
seven
seven
seven
eight

and I’m trying to generate the following output:

four    5
one     3
seven   3
five    2
two     1
three   1
six     1
eight   1

Not knowing anything about the JOIN function, I can use something similar to the following code to get my desired result. (it doesn’t work)

SELECT(SELECT * FROM in_clicks) AS keys,
     (SELECT COUNT(*) FROM in_clicks) AS KeysNum;

However, how can I use the JOIN function to optimize my sql code?

  • 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-24T18:11:08+00:00Added an answer on May 24, 2026 at 6:11 pm

    You don’t need a join here at all. Assuming that number is the column you have listed:

    SELECT number, COUNT(number)
    FROM in_clicks
    GROUP BY number
    ORDER BY COUNT(number) DESC
    

    The COUNT() aggregate function basically counts the number of rows. (COUNT(*) would have worked just as well.) The magic here is GROUP BY number, which instructs the database to group the results by distinct values of number; the aggregate functions will evaluate each group separately. Since there are five rows in the “four” group, COUNT() over that group will yield 5.

    Selecting the number column is possible because we have grouped by it; without GROUP BY number we could not* select number alongside of an aggregate, since that wouldn’t make sense. Selecting this column allows you to see which group the corresponding aggregate result belongs to.

    And the ORDER BY clause should be self-explanatory… sort the results descending by the count of rows in that group. Note that the order of rows whose counts are the same is unspecified; you can do ORDER BY COUNT(number) DESC, number ASC if you want to order first by the count descending, then the number column ascending… but note that it will be a text comparison, so the order would be four, one, seven, five, eight, six, three, two. If you order only by the count then the relative order of the one/seven and two/three/six/eight rows will not be reliable, since they have equal counts.


    * Actually it is possible in MySQL to select non-aggregated columns that are not included in GROUP BY, but this is non-standard and should be used with utmost caution. Read more about it here.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I want to construct a data frame in an Rcpp function, but when I
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites 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.