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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:31:04+00:00 2026-05-30T09:31:04+00:00

Which version of the query is faster / best practice? (Curiousity thing.) More importantly,

  • 0

Which version of the query is faster / best practice? (Curiousity thing.)

More importantly, are they equivalent?

Do these queries accomplish the same thing in this example?

1) INNER JOIN with two OR conditions:

SELECT
  DISTINCT (cat.id) as 'Cat ID:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2) Query each column separately and UNION ALL:

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
    FROM
    cuteness_showdown) AS table_1;

Now, which version is faster / best practice if I need a column from another table?

1) INNER JOIN with two OR conditions (no change):

SELECT
  DISTINCT (cat.id) as 'Cat ID:',
  cat.name as 'Cat Name:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2) Query each column separately and UNION ALL (needed to INNER JOIN cat table):

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
  cat.name as 'Cat Name:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
  FROM
    cuteness_showdown) AS table_1
INNER JOIN cat on
  (table_1.id = cat.id);
  • 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-30T09:31:05+00:00Added an answer on May 30, 2026 at 9:31 am

    To find out which is faster, break out a terminal, write a script that runs each 1000 times and compare the results 🙂

    As for whether they are equivalent, the query optimiser will very often come up with the exact same execution plan for several SQL queries that do the same thing, so they may well be. I can’t tell you whether these ones will get that treatment, but you can use EXPLAIN to see the execution plans for yourself and compare them, assuming you have some data.

    If the execution plans are indeed the same, best practice is about choosing the more readable statement so that anyone else who comes along to maintain the code can do so easily. Alternatively, if they are not the same, then you have to decide whether a harder-to read statement is worth the extra performance gain, which depends on how big a deal performance is in your project. I’d argue that if you have a relatively small DB which is unlikely to scale much and sub 10ms response times, then performance isn’t an issue so just make it easy to maintain.

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

Sidebar

Related Questions

I'm using Oracle SQL Developer to query an Oracle DB (not sure which version
Which version of Linux would be my best choice if I want to use
I'm running NHibernate 2.1.2.4000 but there's no Query() method off my session... Which version
Quick version: Which is the best of the following and why? (or is there
The following SQLite query is accepted by the SQLite shell (version 3.7.5, which is
I have the following query which I found a modified version of it online.
Which version of JavaScript does Google Chrome support in relation to Mozilla Firefox? In
Which version would you prefer? #!/usr/bin/env perl use warnings; use strict; use 5.010; my
With which version(s) of VS2008/VSTS can I work with .dbproj files?
How do I determine which version of comctl32.dll is being used by a C#

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.