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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:59:12+00:00 2026-05-14T06:59:12+00:00

I have a table with three filled columns named Name, City and Occupation. I

  • 0

I have a table with three filled columns named “Name”, “City” and “Occupation”.
I want to create a new column in the same table that contains the number of people who have the same occupation.

"Name" | "City" | "Occupation"
------------------------------
Amy    | Berlin | Plumber
Bob    | Berlin | Plumber
Carol  | Berlin | Lawyer
David  | London | Plumber

I want to have a table that contains:

"Name" | "City" | "Occupation" | "Number"
---------------------------------------
Amy    | Berlin | Plumber      | 2
Bob    | Berlin | Plumber      | 2
Carol  | Berlin | Lawyer       | 1
David  | London | Plumber      | 1

How does the SQL Query that creates the new columns have to look like? I want to actually create a new column in the database that I can access later.

  • 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-14T06:59:12+00:00Added an answer on May 14, 2026 at 6:59 am

    Simple self-join:

    SELECT t0.Name, t0.City, t0.Occupation, COUNT(*) AS Number
    FROM sometable AS t0
    JOIN sometable AS t1 ON t1.Occupation=t0.Occupation
    GROUP BY t0.Name, t0.City, t0.Occupation
    

    If Name is a primary key you can just group by that alone instead, since the other columns would have a functional dependency on it. Of course Name wouldn’t normally be a very good primary key.

    (You might need COUNT(*)-1 if you want the number of other people doing the job rather than the total. It’s unclear; the numbers in your example don’t add up either way.)

    If you must change your schema (and I wouldn’t recommend this kind of denormalisation unless you are absolutely certain you need it; see comments), you can do it with an UPDATE JOINed to Michael’s subselect:

    ALTER TABLE sometable ADD COLUMN Number INTEGER NOT NULL;
    UPDATE sometable AS t0 JOIN (
        SELECT Occupation, COUNT(*) AS Number
        FROM sometable
        GROUP BY Occupation
    ) AS t1 ON t1.Occupation=t0.Occupation
    SET t0.Number= t1.Number;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has three different date columns, so I set each
I have a table named Grades which contains 2 columns: Name and Grade. I
I have a table with three columns, and two cells contain select boxes with
I have a table with three fields, User , City and Country , where
I'm using SQL Server 2008 and I have a table with three columns: Length
I have a table in database in which there is a column named roll_no,
I currently have a site with a table that has Lat/Long float columns, and
I have three table views inside a view controller (going to show/hide table views
I have three table I need to query table 1,2 and 3 and select
I have a table with three fields, FirstName, LastName and Email. Here's some dummy

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.