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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:36:49+00:00 2026-05-25T18:36:49+00:00

UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates.

  • 0

UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates.
UNION also sort the final output.

What I want is the UNION ALL without duplicates and without the sort. Is that possible?

The reason for this is that I want the result of the first query to be on top of the final result, and the second query at the bottom (and each sorted as if they where run individually).

  • 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-25T18:36:50+00:00Added an answer on May 25, 2026 at 6:36 pm

    I notice this question gets quite a lot of views so I’ll first address a question you didn’t ask!

    Regarding the title. To achieve a “Sql Union All with “distinct”” then simply replace UNION ALL with UNION. This has the effect of removing duplicates.

    For your specific question, given the clarification “The first query should have “priority”, so duplicates should be removed from bottom” you can use

    SELECT col1,
           col2,
           MIN(grp) AS source_group
    FROM   (SELECT 1 AS grp,
                   col1,
                   col2
            FROM   t1
            UNION ALL
            SELECT 2 AS grp,
                   col1,
                   col2
            FROM   t2) AS t
    GROUP  BY col1,
              col2
    ORDER  BY MIN(grp),
              col1  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to union the result of two sub-queries (say SUB1 and SUB2). The
I'm trying to UNION the results of two queries. But I'm getting the following
I want to join results of two different store procedures that returns same structure
I have two files where I want to perform union operation based on 1st
I'm trying to get a UNION ALL working with a CTE which I'm using
How do I union or join or CTE two selects, so that I only
I have two SQL queries producing different results when I would expect them to
I have two tables. I want to get the UIDs from table 1 where
Basically, I want have two separate SQL queries, but I want them to be
This is my scenario: I have two different tables: Table 'subscriptions' contains all the

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.