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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:08:10+00:00 2026-05-16T10:08:10+00:00

given the following tables, how would I go about finding the most common ip

  • 0

given the following tables, how would I go about finding the most common ip address across all tables, and ideally, the number of times that ip occurs across all tables.

bad_guys_1         bad_guys_2
| id | ip      |   | id | ip      |
+----+---------+   +----+---------+
| 1  | 1.2.3.4 |   | 1  | 1.2.3.4 |
| 2  | 2.3.4.5 |   | 2  | 4.5.6.7 |
| 3  | 3.4.5.6 |   | 3  | 1.2.3.4 |

bad_guys_3         bad_guys_4
| id | ip      |   | id | ip      |
+----+---------+   +----+---------+
| 1  | 9.8.7.6 |   | 1  | 1.2.3.4 |
| 2  | 8.7.6.5 |   | 2  | 2.3.4.5 |
| 3  | 2.3.4.5 |   | 3  | 3.4.5.6 |

For example, querying the above tables should result in something like:

| ip      | count |
+---------+-------+
| 1.2.3.4 | 4     |
| 2.3.4.5 | 3     |
| 3.4.5.6 | 2     |
| 4.5.6.7 | 1     |
| 9.8.7.6 | 1     |
| 8.7.6.5 | 1     |

The real tables actually contain many additional fields which don’t line up with each other, thus separate tables. I don’t really care about breaking ties between matches, just listing them in descending order by count would be great. My database is PostGreSQL if using any non-standard functions will assist, but for portability would prefer to use standard sql if possible. Thanks and let me know if you need any more detail.

  • 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-16T10:08:10+00:00Added an answer on May 16, 2026 at 10:08 am

    Sorry to say, but the other answers using just union and not union all are wrong. If there is a selected row that appears in more than one table, it will only be counted in the first table if the other tables are included via union and not union all.

    For those queries selecting both the ID and the address, the possibility of a row having the same ID and address in different tables still exists. Using UNION ALL ensures all values are unioned, whether they are duplicates or not – and we want the duplicates so they can be counted. Using UNION ALL is often less work for the database, since it does not need to find duplicates and remove them.

    select ip, count(*) from
    (
    select ip from bad_guys_1
    union ALL
    select ip from bad_guys_2
    union ALL
    select ip from bad_guys_3
    union ALL
    select ip from bad_guys_4
    ) as ranking
    group by ip
    order by count(*) DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about doing the following? Given the tables, Recipe, RecipeItem, RecipeInstruction.
Given the one-table design given below how would the following best be queried The
Given the following tables in ActiveRecord: authors sites articles I don't know how to
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
I would like to confirm that the following analysis is correct: I am building
Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ----
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
Given the following: declare @a table ( pkid int, value int ) declare @b
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME
Given following Ruby statements: (Read input and store each word in array removing spaces

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.