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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:58:28+00:00 2026-05-13T10:58:28+00:00

I am looking for help in writing a query of retrieving the values from

  • 0

I am looking for help in writing a query of retrieving the values from 2 tables in MySQL.

The scenario is

Table A

ID    Name    Marks
===================
23    John    67
45    Mark    45
12    Ram     87

Table B has the following Structure

ID    Name    Evaluation Marks
==============================
45    Mark    34
78    Chris   09
98    Nancy   10
23    John    12

I am trying to write a query, where if I execute the following query

Select "SOMETHING" from Table A where Id=45

I should get Marks Column as 45+34=79, which should fetch and sum from the both the Tables A and Table B.

If I execute the query with the Id=12.
Since the Id=12, does not exists in the Table B, I should get the Marks as 87.

What would a query for the above?

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

    I assume that the id occurs only once in your tables table a, but could be missing in both. If it always exists in table a, you can use a LEFT JOIN instead of the UNION.

    SELECT COALESCE(SUM(marks), 0)
    FROM
    (
      SELECT marks FROM a WHERE id = 45
      UNION ALL
      SELECT SUM(evaluation_marks) AS marks FROM b WHERE id = 45
    ) x
    

    Edit

    If you have all users in table a, then use

    SELECT a.marks + COALESCE( SUM( b.evaluation_marks ), 0 )
    FROM a
    LEFT OUTER JOIN b ON ( b.id = a.id )
    WHERE a.id = 45
    GROUP BY a.id, a.marks
    

    You should consider changing your table model though. Why do you store name and id twice? Can’t you do it like that:

    id    name    marks    evaluation marks
    =======================================
    12    Ram     87        0
    23    John    67       12
    45    Mark    45       34
    78    Chris    0        9
    98    Nancy    0       10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for help with my query below. which is never returning anything for
I'm looking for help using sum() in my SQL query: SELECT links.id, count(DISTINCT stats.id)
I'm looking for help to create a query, to solve the following problem: Let's
LINQ gurus, I am looking for help to write a query... I have a
I could use some help writing a SQL query. I'm trying to display some
I need help writing a query. What I'm trying to do, is to check,
Hi I am looking for best method for writing Dynamic LINQ query. I have
Looking for help on writing a Perl program that takes an input file and
I am looking for your help on for writing mod_rewrite URL for the below
New to jQuery/javascript here and looking for a little help. I am writing a

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.