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

The Archive Base Latest Questions

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

I’m looking for a better way to do the following query. I have a

  • 0

I’m looking for a better way to do the following query. I have a table that looks like this:

game_id | home_team_id | away_team_id 1       | 100          | 200 2       | 200          | 300 3       | 200          | 400 4       | 300          | 100 5       | 100          | 400 

And I want to write a query that counts the number of home games and away games for each team and outputs the following:

team_id | home_games | away_games 100     | 2          | 1 200     | 2          | 1 300     | 1          | 1 400     | 0          | 2 

Right now, I wrote this monstrosity that works, but it’s slow (I know it’s pulling the entire 2,800 row from the table twice).

SELECT    home_team_id as team_id,   (SELECT count(*) FROM `game` WHERE home_team_id = temp_game.home_team_id) as home_games,   (SELECT count(*) FROM `game` WHERE home_team_id = temp_game.away_team_id) as away_games   FROM (SELECT * FROM `game`) as temp_game   GROUP BY home_team_id 

Can a SQL guru help me knock out a better way? I think my problem is that I don’t understand how to get a distinct list of the team IDs to throw at the count queries. I bet there’s a better way with a better placed, nested SELECT. Thanks in advance!

  • 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. 2026-05-11T01:07:59+00:00Added an answer on May 11, 2026 at 1:07 am

    It’s cleaner if you have another table team with team_id and team_name.

     SELECT team_id, team_name,       sum(team_id = home_team_id) as home_games,       sum(team_id = away_team_id) as away_games  FROM game, team  GROUP BY team_id 

    What’s going on: the no WHERE clause causes a Cartesian Product between the two tables; we group by team_id to get back to one row per team. Now there are all the rows from the game table for each team_id so you need to count them but the SQL count function isn’t quite right (it would count all the rows or all the distinct rows). So we say team_id = home_team_id which resolves to 1 or 0 and we use sum to add up the 1’s.

    The team_name is just because it’s geeky to say that ‘team 200 had 20 home games’ when we ought to say that ‘Mud City Stranglers had 20 home games’.

    PS. this will work even if there are no games (often a problem in SQL where there is a team with 0 games and that row will not show up because the join fails).

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

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This MSDN Magazine article has a lot of the answers… May 11, 2026 at 6:33 am
  • added an answer In general, no, that's not how the pattern(s) work. The… May 11, 2026 at 6:33 am
  • added an answer Set cell.text to 'Apples'. Then create an new UILabel view… May 11, 2026 at 6:33 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.