QUERYING columns from 3 tables using SQL - sql

I have 3 tables, TABLE1, TABLE2 and TABLE3.
TABLE1 have ID and TYPE as columns
TABLE2 have ID, DATE and VALUE as columns
TABLE3 have ID and DESC
Basically TABLE1 is just used as a mapping table. It just tells what type of thing a certain ID is with the TYPE column. We have 1-3 as TYPE.
TABLE2 contains transaction dates per ID. So it's possible to have multiple same ID in TABLE2 but with different DATE. VALUE is just how much the transaction was that DATE for that ID
TABLE3 is like TABLE1 but with a different column DESC.
So my question is how can I query all IDs that are TYPE = 1 (TABLE1) that happened on DATE = 5/27/2018 (TABLE2). The query would contain the columns ID, DATE, VALUE and DESC (TABLE3)
My approach in this one is to use JOIN
SELECT TABLE1.ID, TABLE2.DATE, TALBE2.VALUE
FROM TALBE2
INNER JOIN TABLE1 ON TABLE1.ID = TABLE2.ID
WHERE TABLE1.TYPE = 1 AND TABLE2.DATE = '5/27/2018';
My problem here is that it doesn't work and it doesn't include TABLE3.DESC yet.

I hope you are not using the exact SQL statement as given. It has 2 TALBE-TABLE mix ups.
The query should be something like:
SELECT TABLE1.ID, TABLE2.DATE, TABLE2.VALUE, TABLE3.DESC
FROM TABLE2
LEFT JOIN TABLE1 ON TABLE2.ID = TABLE1.ID
LEFT JOIN TABLE3 ON TABLE2.ID = TABLE3.ID
WHERE TABLE1.TYPE = 1 AND TABLE2.DATE = '5/27/2018'

Related

Print all the data from table1 and matching data from table 2 but what if the table2 is null

As I am trying to learn SQL, I have came across this question.
Let's say I have two tables:
table1 which contains two columns
id and data1
and
table2 which also has two columns
p_id and data2.
How would I print out all the data from table1 and just matching data from table2 in two different scenarios.
Scenario 1: table1.id = 1
Scenario 2: what if the table2 is null, (but I still want to print out all the data from table1)
So my approach was
Scenario 1:
Select table1.id, table1.data1, table2.data2
From table1
Inner Join table2 On table1.id=table2.p_id
Where table1.id = 1;
Scenario 2:
Select table1.id, table1.data1, table2.data2
From table1
Inner Join table2 On table1.id=table2.p_id
Where table1.id = 1 and table2.data2 is not null;
For the second scenario, you are describing a left join:
Select table1.id, table1.data1, table2.data2
From table1 left Join
table2
On table1.id = table2.p_id
where table1.id = 1 ;
Because you are filtering on the first table, the condition remains in the where clause. If you wanted to filter on the second table, you would presumably want something like:
Select table1.id, table1.data1, table2.data2
From table1 left Join
table2
On table1.id = table2.p_id and
table2.col = 'xyz';

Using SELECT * from multiple tables in postgres/redshift

I have 3 tables with 2 common fields for all tables (id, timestamp). I want to do something like this:
SELECT *
FROM table1,
table2,
table3
WHERE id = '123'
and timestamp = '1704'
What I would expect is that it returns all the rows with the id and timestamp matching from the 3 tables. How can the query be modified to achieve this?
select /*your_columns*/
from table1
inner join table2 on table1.id = table2.id and table1.timestamp = table2.timestamp
inner join table3 on table2.id = table3.id and table2.timestamp = table3.timestamp
where table1.id = '123'and table1.timestamp = '1704'
See more here about JOINs

Joining to Same Column but Getting No Rows

I have two tables.
Table1
ID Text
Table2
ID ParentID Text
I am trying to join to the same table twice using 2 different columns. I want all the rows where Table1.ID = Table2.ID as well as all the rows where Table1.ID = Table2.ParentID. I've tried the following but it returns no rows. When I run the script below in two different select statements each with one of the joins only, I get the result I want but not in the same select statement. Any ideas on what I'm doing wrong?
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = Table1.ID
JOIN Table2 22
on 22.ParentID = Table1.ID
You're only getting the records where Table 1 is both a Parent ID and an ID... maybe you want:
SELECT
* FROM Table 1
JOIN Table2 2 on 2.ID = Table1.ID
UNION
SELECT
* FROM Table 1
JOIN Table2 22 on 22.ParentID = Table1.ID
Or potentially UNION ALL?
You could try this query:
SELECT
*
FROM
TABLE1,
TABLE2
WHERE
TABLE1.ID = TABLE2.ID
OR
TABLE1.ID = TABLE2.ParentID
Your aliasing is a little off.
Give this a go:
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = 1.ID
JOIN Table2 22
on 22.ParentID = 1.ID
You may still get an error on the SELECT * by not having an alias there... I suggest doing SELECT 1.*, or even doing SELECT 1.myColumn, 1.myColumn2 etc...

Selecting a mapping table with fields from two other tables

I have a mapping table referring to ids from two different tables. I would like to select the mapping table with each id being replaced by another field in the respective table.
To be a little more explicit: there are three tables with two columns each:
Table1 has a id (the primary key) and field1
Table2 has a id (the primary key) and field2
Table3 (the mapping table) has fields Table1_id (which takes values in Table1.id) and Table2_id (which takes values in Table2.id)
What I want is to get the content of Table3 with Table1.field1 and Table2.field2 as columns.
I know how to replace one of the columns in the mapping table with another column of one of the other tables, by using a inner join:
SELECT Table1.field1, Table3.Table2_id
FROM Table1
INNER JOIN Table3
ON Table1.id=Table3.Table1_id;
however I don't know how to do basically the same thing with both columns.
If i understood correctly you are trying to get field1 from Table1 and field2 from table 2. If so you just need to join the three tables
SELECT a.field1, c.field2
FROM Table1 a
INNER JOIN Table3 b
ON a.id=b.Table1_id
INNER JOIN Table2 c
ON b.Table2_id = c.id
Do another join.
SELECT Table1.field1, Table2.field
FROM Table1
INNER JOIN Table3
ON Table1.id = Table3.Table1_id
INNER JOIN Table 2
ON Table2.id = table3.table2_id;
You just need to join all three tables; something like
SELECT Table1.Field1, Table2.Field2
FROM Table3
JOIN Table1 ON Table1.Id = Table3.Table1_id
JOIN Table2 ON Table2.Id = Table3.Table2_id
I do not know if I understood correctly what you want to achieve but this should get you results from both tables joined by thier keys
SELECT Table1.field1, Table2.field2
FROM Table1
INNER JOIN Table3 ON Table1.id = Table3.Table1_id;
INNER JOIN Table2 ON Table2.id = Table3.Table2_id;

Selecting records based on two other tables

I have a query with inner join to another table, with this I want also include records which are contained in another column.
Example:
select name, address from table1
inner join table2 on table1.id = table2.id
With this, I want to also include rows which are having table1.recno = (1,2,4).
How could I write query for that?
One option I know is to use the IN keyword instead of the first table join. But our client doesn't want to use the IN keyword.
Use a left join and then use the WHERE clause to filter out the rows that you need.
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL OR table1.ID In (1,2,4)
Or if you want to avoid an innocuous IN for silly reasons, use:
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL
OR table1.ID = 1
OR table1.ID = 2
OR table1.ID = 4