Join data on a table based on a relationship table - sql

I have two tables in my database, "Fact" and "Fact_Cause", Here's the table structure:
Fact: ID(PK), Name, Date
Fact_Cause: IDCauseFact(FK -> Fact(ID)), IDEffectFact(FK -> Fact(ID))
What I want is a resultant table with this format
Cause | Effect
Each column containing the Name of the corresponding Fact.
Could you guide me?
Thank you.

Finally worked it out, here's the SQL query:
SELECT cause.Name AS 'Cause', effect.Name AS 'Effect'
FROM Fact_Cause c
INNER JOIN Fact cause ON c.IDCauseFact = cause.ID
INNER JOIN Fact effect ON c.IDEffectCause = effect.Id
I was failing on the joins and the aliases :)

Related

Problem accessing specific column using INNER JOIN

I have two tables Listings & Reviews that both have a column id.
I want to inner join them with Listings.id = Reviews.listing_id but when I am specifying something in my pgadmin, like listings.id or example.example it seems like it doesn't recognise it.
I get the error:
missing FROM-clause entry for table "listings"
Do I write something wrong?
Here is the code:
SELECT
id, listing_url, reviewer_id, reviewer
FROM
public."Listings"
INNER JOIN public."Reviews" ON Listings.id = listing_id
WHERE
reviewer = 'Vivek'
order by
reviewer_id;
Always qualify all columns names so both you and the SQL engine know where the columns come from. It is not clear where the columns come from. I've made guesses on where the columns come from:
SELECT l.id, l.listing_url, r.reviewer_id, r.reviewer
FROM public."Listings" l INNER JOIN
public."Reviews" r
ON l.id = r.listing_id
WHERE r.reviewer = 'Vivek'
ORDER BY r.reviewer_id;
Note that this introduces table aliases, which are abbreviations for tables. This makes it easier to qualify the column names (the default alias is the full table name).
Also, I may be wrong on where the columns come from; your question does not have complete information on the table structure.

Parent child relationship Join tables in oracle sql

I have a requirement below.
Now I have to get output like below
How can this be achieved ?
I have written the below SQL but parent_position_id is coming, not parent_position_code
select
hapf.position_code,
pphf.parent_position_id
from
hr_all_positions_f hapf, PER_POSITION_HIERARCHY_F pphf
where
hapf.position_id = pphf.position_id
Should I write a sub query? How should I proceed ?
This is Oracle SQL
Thanks,
Shivam
Noone ever said you could only join a table in once:
select
chi.position_code,
par.position_code as parent_position_code
from
hr_all_positions_f hapf
INNER JOIN PER_POSITION_HIERARCHY_F chi on hapf.position_id = chi.position_id
INNER JOIN PER_POSITION_HIERARCHY_F par on hapf.parent_position_id = par.position_id
Bear it in mind; I see people coming to thinking all the time that they can only join a table once. If one table decodes a value in 3 different columns, then you sure can join that same table in 3 times... Imagine if it were an address table, and a Student had a HomeAddressId, WorkAddressId and StudyAddressId, and the Address table held all these addresses - you'd join the addresses table to the Student table 3 times to get all the data..

Join SQL tables, but with additional info from joined table

Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key

Database design: table with NULL keys

I'm designing a table that will hold numeric values for 2-3 situations of data:
Situation 1: has Age and Sex, along with the numeric value
Situation 2: has only Age, along with the numeric value
Situation 3: has only Sex, along with the numeric value
I don't want to create 3 different tables. Instead, only one table, with the following columns:
AgeID (references a table that contains information about the Age)
SexID (references a table that contains information about the Age)
Value (the numeric value itself)
AgeID and SexID as Foreign Keys and linked to the appropriate tables.
The problem is: my query is always doing a INNER JOIN with Age and Sex tables. For Situation 1 it works well because values are present. For Situations 2 and 3 I don't get any data, because either AgeID or SexID is null.
What solution is the correct one?
Change something in the table design?
Use Entity-Attribute-Value table to be more generic?
Use LEFT JOIN instead of INNER JOIN for all queries involving the nullable columns??
Any other idea?
Could someone clarify?
Thanks!
Yes an outer Join, Left or right, the Inner join is meant to filter out everything that doesn't have a match in both tables.
Use a conditional INNER JOIN, like
INNER JOIN Table x ON
(AgeID IS NULL OR AgeID = x.AgeID)
AND (SexID IS NULL OR SexID = x.SexID)

Fetching data from two tables matching a criteria

Consider the two tables' schema:
1) Person(name varchar(100),income int)
2) IncomeGroups(incomeGroupName varchar(100), minIncome int, maxIncome int)
I was stumbled while developing a sql query for fetching Person Names with their IncomeGroupNames based on their income.
I am trying to accomplish something like (Name,IncomeGroupName).
Is it even possible? I'll be really glad if anyone can guide me in this.
SELECT a.Name, b.IncomeGroupName
FROM Person a
INNER JOIN IncomeGroups b
ON a.income BETWEEN b.minIncome AND b.maxIncome
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
You can use the following query which joins the tables:
select p.name,
i.incomeGroupName
from person p
inner join incomegroups i
on p.income >= i.minIncome
and p.income <= i.maxIncome;
See SQL Fiddle with Demo
This joins the tables based on the range that the person's income falls in.