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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:16:41+00:00 2026-06-06T06:16:41+00:00

I have a table say table1 which has 3 columns column1, column2 and column3

  • 0

I have a table say table1 which has 3 columns column1, column2 and column3.

The column1 and column2 are a FOREIGN KEY with 2 other tables. However the data in column3 is from n number of tables.

For e.g. Let us consider Facebook. To display the activities it might maintain a table which could have user1 photoliked photo1 or user1 statusliked status1. So in this case column3 cannot be a FOREIGN KEY with a specific table.

Now there are 2 ways of getting real data –

1st way –

SELECT user_id,
       verb_id,
       CASE WHEN verb_id = photoliked THEN
            (SELECT photo_name FROM photos WHERE photo_id = column3) -- getting the desired data from the third column
         WHEN verb_id = statusliked THEN
            (SELECT status FROM statustable WHERE status_id = column3) 
         ELSE '' END AS performedon
FROM table1
     JOIN table2 ON user_id = user_id  -- joining the first column
     JOIN table3 ON verb_id = verb_id  -- joining the second column

2nd way –

SELECT user_id,
       verb_id,
       CASE WHEN verb_id = photoliked THEN
            p.photo_name
         WHEN verb_id = statusliked THEN
            s.status
         ELSE '' END AS performedon
FROM table1
     JOIN table2 ON user_id = user_id  -- joining the first column
     JOIN table3 ON verb_id = verb_id  -- joining the second column
     LEFT JOIN photos p ON p.photo_id = column3  -- joining the column3 with specific table 
     LEFT JOIN statustable s ON s.status_id = column3

Question

Which of the 2 ways is better to retrieve data?
and which of the 2 queries is less expensive?

  • 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-06-06T06:16:42+00:00Added an answer on June 6, 2026 at 6:16 am

    The second would be faster and the reason is the first one contains what is called correlated subqueries. The subqueries have a correlation with records from the master query. So the subqueries need to be run once for every matching record in the master query. In your case it can’t run the subquery until it determines the value of verb_id in the master query. That is a lot of queries to run.

    An EXPLAIN on the first query should indicate this issue. It’s is usually a red flag when you see that in an EXPLAIN.

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

Sidebar

Related Questions

Lets say I have two tables: table english which has two columns, id and
Let's say I have a table, category , which has 3 columns, id ,
I have a table called say Table1 which has a column that is a
Let's say I have two tables: Table 1 has the columns NOTE_ID (a unique
Let's say I have a Customer table which has a PrimaryContactId field and a
Let's say I have an Order table which has a FirstSalesPersonId field and a
I have a Firebird database, which let's say has Tables A and B which
I have a view table which is a union of two separate tables (say
I have an HTML table which has equally divided rows and columns. I would
I have a table which is randomly generated. It has 3 columns : P_Id,

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.