0

I am working on a generalized problem where I am given only schema definition of multiple tables that i have.

Now i have to retrieve certain columns by joining multiple tables such that number of joins are minimized.

Example: Suppose i have 3 tables and here is the list of columns that they have.

Table 1:(1,2,3,4,5), Table 2:(5,6,7), Table 3:(5,6,7,8)

Now suppose I have a query in which i want all the columns 1,2,3,4,5,6,7,8.

Now i can join either table 1,table 2 and table 3 OR table 1 and table 3.I would get the required information in both the cases but joining table 1 and table 3 would require only 1 join rather than 2 join in other case.

What i was trying was a greedy algorithm in which first i would consider table that has maximum number of required columns then eliminate the common columns between the query and table(from both query and table) and then consider updated required columns and update tables and so on.But i guess it would be slow.

So is there a generalized algorithm or if anyone can give me any hint in this direction?

ayush nigam
  • 177
  • 1
  • 4
  • 15
  • The task as it posted makes no sence in the SQL domain. The same set of column names doesn't ensure the same content. – Serg Sep 27 '16 at 07:00

1 Answers1

0

first of all, I have to mention that it's not "join", but "union". Then I have to mention that if you want to use the greedy algorithm, you have to first join the 2 most short, cause when you join a table 2 times, it would be of o(n), and so you will have 2n operations to do, and so it would be better if n be as smaller as possible. Beside these, the following link may be useful for you: Merging 3 tables/queries using MS Access Union Query

Community
  • 1
  • 1
  • It is a Join only,as i may be using 2 joins(i can join table 1 and table 2 on column 5 and then table 2 and table 3 on column 6) or a single join(table 1 and table 3 on column 5) to get all columns 1,2,3,4,5,6,7,8 – ayush nigam Sep 27 '16 at 06:41