Relational Algebra: Natural join with NULL value - relational-database

Table 1
Customer id city
John 1 LA
Nancy 2 NULL
Table 2
Customer $ in the pocket
John 20
Nancy 30
I am wondering what happen if Table 1 natural join with Table 2? My guess is that the result would be 4 attributes and both John and Nancy will appear.
But my friend told me that only John will appear, Nancy won't because there is a null value.

In the case above, your friend is wrong, you are right!
Let's see a case where it would be otherwise:
Table 'Customer'
Id Name AccNo
1 John 44
2 Nancy NULL
Table 'Account'
AccNo $_in_Pocket
44 20
45 30
Here, with a natural join, we would get all attributes for John but Nancy would be missing from the results.

Related

Moving consecutive results to different columns

I have the following table:
Transaction Id
Authorizer
1
john
1
robert
2
Jane
2
Paul
And the result table I need is like this:
Transaction Id
Authorizer_1
Authorizer_2
1
john
Robert
2
Jane
Paul
How can i make this in Teradata SQL assistant?
Thanks in advance!

I am trying to return combined rows in the same table based on a key in a second table

I probably haven't explained this very well in the title but I have two tables. Here is a simple version.
channel_data
entry_id channel_id first_name last_name model other_fields
1 4 John Smith
2 4 Jane Doe
3 4 Bill Evans
4 15 235
5 15 765
6 15 543
7 15 723
8 15 354
9 15 976
10 1 xxx
11 2 yyy
12 3 123
channel_titles
entry_id author_id channel_id
1 101 4
2 102 4
3 103 4
4 101 15
5 101 15
6 101 15
7 102 15
8 102 15
9 103 15
10 101 1
11 102 2
12 103 3
I am not able to re-model the data unfortunately.
I need to list all the rows with a channel_id 15 from channel_data and beside them the first_name and last_name which has the same author_id from channel_titles.
What I want to return is this:
Model First Name Last Name
---------------------------------
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
If Model was in one table and Names were in another this would be much simpler but I'm not sure how to go about this when they are in the same table.
========================================
Edited to clarify.
I need to get each model with a channel_id 15 from channel_data
For each model I need to look up the entry_id in channel_titles to find the author_id
I need to find the row with that author_id AND channel_id 4 in channel titles (each row with channel_id 4 has a unique author_id).
I need to take the entry_id of this row back to channel_data and get the first_name and last_name to go with the model.
I am well aware that the data is not structured well but that is what I have to work with. I am trying to accomplish a very small task in a much larger system, remodelling the data is not an option at this point.
I think sub-queries might be what I am looking for but this is not my area at all usually.
Ok, that is convoluted. However, based on your description, this query should give you the results you want. The WHERE and JOIN descriptions follow the logic you have described in your question.
SELECT cd1.model, cd2.first_name, cd2.last_name
FROM channel_data cd1
JOIN channel_titles ct1 ON ct1.entry_id = cd1.entry_id
JOIN channel_titles ct2 ON ct2.channel_id = 4 AND ct2.author_id = ct1.author_id
JOIN channel_data cd2 ON cd2.entry_id = ct2.entry_id
WHERE cd1.channel_id = 15
ORDER BY cd1.entry_id
Output:
model first_name last_name
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
Demo on SQLFiddle

Join Predicates And Comparison Operators

In an attempt to better understand how SQL joins work, I've run into difficulty understanding the result set returned from a query -- The tables I'm using are:
Employees
Id Name Salary Gender City
1 Sam 2500 Male London
5 Todd 3100 Male Toronto
3 John 4500 Male New York
6 Jack 7000 Male Shangri La
4 Sara 5500 Female Tokyo
2 Pam 6500 Female Sydney
and Gender:
ID Gender
1 Male
2 Female
3 Unknown
The first query returns all the columns from an inner join on the Gender column -
SELECT *
FROM Employees
INNER JOIN Gender
ON Employees.Gender = Gender.Gender
The returned result -
Id Name Salary Gender City ID Gender
1 Sam 2500 Male London 1 Male
5 Todd 3100 Male Toronto 1 Male
3 John 4500 Male New York 1 Male
6 Jack 7000 Male Shangri La 1 Male
4 Sara 5500 Female Tokyo 2 Female
2 Pam 6500 Female Sydney 2 Female
Which is pretty much what I expected. However when I changed the comparison operator -
SELECT *
FROM Employees
INNER JOIN Gender
ON Employees.Gender != Gender.Gender
What I originally thought would return an empty set, returned this -
Id Name Salary Gender City ID Gender
4 Sara 5500 Female Tokyo 1 Male
2 Pam 6500 Female Sydney 1 Male
1 Sam 2500 Male London 2 Female
5 Todd 3100 Male Toronto 2 Female
3 John 4500 Male New York 2 Female
6 Jack 7000 Male Shangri La 2 Female
1 Sam 2500 Male London 3 Unknown
5 Todd 3100 Male Toronto 3 Unknown
3 John 4500 Male New York 3 Unknown
6 Jack 7000 Male Shangri La 3 Unknown
4 Sara 5500 Female Tokyo 3 Unknown
2 Pam 6500 Female Sydney 3 Unknown
While I can kinda see how the not-equals(!=) operator would return this result it begs the question of what type of comparisons are useful in join predicates and which aren't - does the type of join [inner, right, left...] impact the returned result adversely or can the join type and comparison be resolved to actionable behavior(in other words does it always have to be ==)? Also if there are any sources out there that could help me, that would be great. Thanks.
An inner join is a subset of the Cartesian product of both tables. That is, every row in one table is paired with every row of the other table.
So, if one table has n rows and the other m rows, then the Cartesian product has n * m rows.
The on clause filters these rows. Equality is the most common filter and such joins are often called equi-joins. They are offer the most optimization opportunities and are typically the most efficient.
(Outer joins are similar but have a mechanism to include unmatched rows.)
Normally, join predicates contain at least one equality comparison (although this is not necessary). Other comparisons -- including subqueries using exists/in -- are allowed and often useful.
Any decent documentation or tutorial or book should be able to explain this.
In your case, often not exists is the intention. To find employees whose gender is not in the reference table:
SELECT e.*
FROM Employees e
WHERE NOT EXISTS (SELECT 1 FROM Gender g WHERE e.Gender = g.Gender)
Of course, such a query would be unnecessary if you used the primary key to reference the table and included proper foreign key declarations.

How to select value if only all criteria are met?

First time Poster here so I appoligize about the formatting and am really novice at sql, but this has me stumped. That and I am using 2016 MS Access's SQL as well.
I have a table and I want to select only the names of the people who have fulfilled all the requirements.
Table Chore
ID Name Chore Done
1 Joe Sweep Yes
2 Joe Cook Yes
3 Joe Dust Yes
4 Bill Vacuum No
5 Bill Dust Yes
6 Carrie Bathroom Yes
7 John Cook No
8 John Beds No
9 John Laundry Yes
10 Mary Laundry No
11 Mary Sweep No
12 Cindy Car Yes
13 Cindy Garden Yes
In this case, only Joe, Carrie and Cindy's names should be returned because under their name, they finished all their chores.
Help please and thanks in advance!
You can use not in
select name from my_table
where name not in (select name from my_table where chore_done ='No');
You could check the value of max(done), like
select
name
from
my_table
group by name
having max(done) = -1
In Access, Yes/True is -1, No/False is 0, so max(done) is Yes

prediction on rowwise data or progressive data

I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.