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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:10+00:00 2026-05-14T04:56:10+00:00

Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:

  • 0

Suppose you have these tables:

Table Name: Salesman
Fields: S_ID(Primary Key), Name

Table Name: Region_1
Fields: Reg_ID(Primary Key), S_ID(Foreign Key), sales

Table Name: Region_2
Fields: Reg_ID(Primary Key), S_ID(Foreign Key), sales

Table Name: Region_3
Fields: Reg_ID(Primary Key), S_ID(Foreign Key), sales

Table Name: Region_4
Fields: Reg_ID(Primary Key), S_ID(Foreign Key), sales

Query 1: Find out total of sales of each salesman in all the regions.

Query 2: Find out total of sales of a particual salesman in all the regions. (if the first one is solved I think this will be easy. 🙂 )

  • 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-14T04:56:10+00:00Added an answer on May 14, 2026 at 4:56 am

    Query 1: Find out total of sales of each salesman in all the regions.


    Use:

       SELECT s.name,
              COALESCE(SUM(x.sales), 0) 
         FROM SALESMAN s
    LEFT JOIN (SELECT r1.s_id,
                      r1.sales
                 FROM REGION_1 r1
               UNION ALL
               SELECT r2.s_id,
                      r2.sales
                 FROM REGION_2 r2
               UNION ALL
               SELECT r3.s_id,
                      r3.sales
                 FROM REGION_3 r3
               UNION ALL
               SELECT r4.s_id,
                      r4.sales
                 FROM REGION_4 r4) x ON x.s_id = s.s_id
    

    Query 2: Find out total of sales of a particual salesman in all the regions.


    Add the WHERE clause to the query above:

       SELECT s.name,
              COALESCE(SUM(x.sales), 0) 
         FROM SALESMAN s
    LEFT JOIN (SELECT r1.s_id,
                      r1.sales
                 FROM REGION_1 r1
               UNION ALL
               SELECT r2.s_id,
                      r2.sales
                 FROM REGION_2 r2
               UNION ALL
               SELECT r3.s_id,
                      r3.sales
                 FROM REGION_3 r3
               UNION ALL
               SELECT r4.s_id,
                      r4.sales
                 FROM REGION_4 r4) x ON x.s_id = s.s_id
        WHERE s.s_id = ?
    

    …replace the ? with the salesperson’s s_id value.

    The Lesson: UNION vs UNION ALL


    UNION and UNION ALL will allow you to combine two queries, but they have to have the same data types in the column positions. For example, say query one returns data types in the following order:

    1. Integer
    2. String

    Any subsequent query that is union’d to the first has to return the same data types in the same position. Pay special attention to the fact of data types – it doesn’t guarantee the data within the column.

    The second issue to UNIONs is knowing the difference between UNION and UNION ALL. UNION will remove duplicates if they exist (equivalent to using DISTINCT), which is not desired in the given scenario. UNION ALL will not remove duplicates, and is faster because of this.

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

Sidebar

Related Questions

Suppose I have these tables: Fruits - FruitID INT PK - FruitName NVARCHAR(30) -
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
Suppose I have a table called Companies that has a DepartmentID column. There's also
Let's say, we have these SQL tables: [Articles] bill int (pkey) arti int (pkey)
Suppose I have a table of customers and a table of purchases. Each purchase
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a directory /dir inside which there are 3 symlinks to other
Suppose I have a SELECT statement that returns some set of results. Is there
Suppose I have three classes. It is valid to instantiate A, but there are
There's no strongly typed View() method to return an ActionResult. So, suppose I have

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.