SQL query what to include in from statement - sql

Say I have four tables.
Table 1:
PK_Column_a
Table 2
PK_Column_c
FK_Column_a
Table 3
FK_Column_c
FK_Column_e
PK_c,e
Table 4
PK_Column_e
If I now want write a SQL query that will select
table1.Column_a, table2.column_c, table4.Column_e
And I wish to connect them where their foreign keys are pointing (e.g. Where table1.Column_a = table2.Column_a).
Do I need to include table 3 in my "FROM" statement? or can I connect table 2 and table 4 without joining them through table 3?

I believe the answer is yes, you would need to join to Table-3, because otherwise you won't be able to bring in data from Table-4. (There's no other way to describe the relationship for the data in Table-4 to the data in Table-1 or Table-2.)

You have to join through the Table-3, otherwise you will generate a cross join and the data won't be valid. Simply every row of table 1&2 will merge with every row from Table 4 ...

Related

How to get the differences between two - kind of - duplicated tables (sql)

Prolog:
I have two tables in two different databases, one is an updated version of the other. For example we could imagine that one year ago I duplicated table 1 in the new db (say, table 2), and from then I started working on table 2 never updating table 1.
I would like to compare the two tables, to get the differences that have grown in this period of time (the tables has preserved the structure, so that comparison has meaning)
My way of proceeding was to create a third table, in which I would like to copy both table 1 and table 2, and then count the number of repetitions of every entry.
In my opinion, this, added to a new attribute that specifies for every entry the table where he cames from would do the job.
Problem:
Copying the two tables into the third table I get the (obvious) error to have two duplicate key values in a unique or primary key costraint.
How could I bypass the error or how could do the same job better? Any idea is appreciated
Something like this should do what you want if A and B have the same structure, otherwise just select and rename the columns you want to confront....
SELECT
*
FROM
B
WHERE NOT EXISTS (SELECT * FROM A)
if NOT EXISTS doesn't work in your DBMS you could also use a left outer join comparing the rows columns values.
SELECT
A.*
from
A left outer join B
on A.col = B.col and ....

Merging 2 SQL tables with same table convention design without using update command

I have 2 tables in my SQL database:
And I want to merge them in a way the result will be:
This is just an example for 2 tables which need to be merged into one new table (The tables contain an example data, the statement should work for any amount of data inside the tables).
The ID which got different value in CSV should be updated into the new table for example:
ID 3's value is 'KKK' and in table T is 'CCC', then what should be updated is the CSV table.
You seem to want a left join and to match to the second table if available:
select t.id, coalesce(csv.value, t.value) as value
from t left join
csv
on t.id = csv.id;
If you want this in a new table, use the appropriate construct for your database, or use insert to insert into an existing table.

2 SQL Queries from 2 Different Databases, Create 1 Table merging Columns

Suppose that I have two queries from two databases as below.
LIST 1 from [Alphabet] table
ABCD
A
B
C
D
LIST 2 from [Integers] table
Numbers
101
201
301
401
I would like to merge them so that in Excel sheet, I want to see that alphabet table is in columns A and integers table is in column B.
Here my suggestion:
Create Table merged ( Select [ABCD] from [Alphabet] join with [Numbers] from [Integers])
How can I improve the quality to work?
And should the row numbers be equal in both tables? Say, 27 letters and 27 integers, or would work with 27 letters and integers [1,20]?
This Is My First Answer and i hope it would help ...Back to your question
How can I improve the quality to work?
You Must use constraints to enforce rules for the data in your tables.
You need two columns of the same type, one on each table [Alphabet table] and [Integers table], to JOIN on.
Whether they're primary and foreign keys or not doesn't matter.
And should the row numbers be equal in both tables?
the answer is No
Depending on what joins you will Use !
SQL joins are used to query data from two or more tables, based on a relationship between the corresponding rows in these tables.
• INNER JOIN: Returns all corresponding rows from multiple tables.
• LEFT OUTER JOIN: Returns all rows from the left table, corresponding rows in the right table & if there are no matches, returns NULL.
• RIGHT OUTER JOIN: Returns all rows from the right table, corresponding rows in the left table, & if there are no matches, returns NULL.

insert from one table into two tables

I have three tables:
Table A has columns name, id, nationality
Table B has a column name
Table C has a column id
I was wondering if it is possible to extract from Table A and insert its name column into Table B and id column into Table C in one single SQL query? Not in two separate queries.
I know it is possible in Oracle.
I am using Teradata, which supports all SQL queries.
It is not possible to do in a single query. One table at a time only. Use a Transaction or a Stored statement to query the data and then two more queries to insert the data in each table. This does save you making the query for both inserts but you cant do an INSERT on two tables.

Oracle Select Query Load Optimization

I have two table A and B. A has a column b_id which act as a foreign key reference for a many to one relationship.
So is there any load difference in Oracle when executing the query like
select A.* from A, B where A.b_id=B.ID and B.ID=? -- auto-generated by hibernate
and
select * from A where b_id = ? -- Created manually
UPDATE : I need data from only table A
For sure there will be a difference between both queries, the first one is getting data from two tables and the second one is just querying one unique table.
Even if you don't return any results of table B in the first query, these data are used for the jointure condition (not the case in the second query).