Toad World® Forums

How do I select different columns from two different tables?

Hello, all. I am very new to the SQL language. Here’s hoping someone can help with my question.

I am trying to select data from two tables using a column that is in both tables. My question requires a bit of setup.

Table1 has a lot of data in it. For the sake of my question, we’ll call the columns I am interested in as IDTag and ColumnA.

Table2 has its own data in it, but also has the identical IDTag column from Table1. The column I am interested in this table is ColumnB.

ColumnA doesn’t exist in Table2, nor does ColumnB exist in Table1.

My question is…what query do I need to use to select the information in ColumnA and ColumnB using the IDTag column?

Thank you in advance for any help.

See if this works for you.

Notes…
Table1 aka Lots of Data
IDTag
ColumnA

Table2 aka Some Data
IDTag
ColumnB

– === The SQL ======
SELECT
T1.IDTag T1_IDTAG,
T1.ColumnA,
T2.IDTag T2_IDTAG,
T2.ColumnB,
FROM
Table1 T1,
Table2 T2
WHERE
T1.IDTag = T2.IDTag
ORDER BY
T1.IDTag asc
;

– =====

I gave Table1 and Table2 alias names of T1 and T2 so I can reference them in by code with short names like T1.ColumnA instead of Table1.ColumnA.

I gave each of the IDTag columns their own alias too so I can tell them apart when I look at the data after running the SQL. So the IDtag from Table1 I called T1_IDTAG.

In the WHERE statement I have a one-to-one join of Table1 and Table2 using the IDTag column you said was exactly the same in both tables. Notice that I use the tables’s aliases so the same-named columns are differentiated.

This worked like a charm. Thank you so much!