0

Problem

When doing any sort of Join all common attributes are listed under one column in one way or another. However, I have not been able to find a source that deal with columns of the same name, but with different domains.

Put in a visual perspective, I am taking about columns like the case below:

enter image description here enter image description here

The domain is different because one B column is numbers but the other is letters.

Question

Is a Join possible with attributes of the same name but different domains?

  • 1
    Yes - as long as you're careful to qualify which table you're referring to! – Vérace Jan 27 '18 at 16:25
  • But in the resulting relation of a Join, attributes of the same name are listed under the same column. Yes you can rename the column, but I'm talking about in the case where you are not supposed to rename either column. – isakbob Jan 27 '18 at 16:36
  • Could you show us what you want with data? – Evan Carroll Jan 9 '19 at 6:47
1

I think you are being misled by your terminology.

SQL is a standard that has the following schematics

  • Schema
  • Table
  • Column
  • Attribute

*Oracle does not have separate databases in an Instance. schemas are tied to a user.

SQL is a 4th generation language that separates the need to know where to query an object to high, almost human language-style programming

When you write a Query in SQL, the compiler (called the SQL Optimizer) translates this into machine-level language and retrieves your data. It is therefore highly dependent on the Optimizer to correctly retrieve the data how it thinks is best.

What this means is that your SQL is nothing more than meaningful suggestions to the Optimizer. All you need to know is what the Structures’ names are, not where or how to grab it.

SQL has hints and Operators that help the Optimizer to pick the right datasets, but it is the Optimizer that chooses the ORDER of the Query to parse first.

Thus, you are writing Logical queries. If two tables are joined, it is your responsibility to help the Optimizer by specifically calling out the columns that can be joined.

Even if the attributes are not the same, the Optimizer is fully agile enough to know how to translate on the fly these values. For example, a VARCHAR type value of ‘13’ is still rather similar to a tinyint, integer, numeric Value, so the Optimizer will Implicitly convert these columns into the same data type!

The following example is in TSQL:

CREATE TABLE TableA (col_A int, col_B varchar(24) )
CREATE TABLE TableB (col_A varchar(24), col_B int)
INSERT INTO TableA (col_A, col_B)
VALUES (1, ‘My Value’)
INSERT INTO TableB (col_A, col_B)
VALUES (‘Not my Value’, 1)

SELECT A.Col_B
FROM TableA AS A
INNER JOIN TableB AS B ON A.Col_A = B.col_B

Notice we used an alias to help the optimizer know what we mean by the join and what we mean by which column to return.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.