Natural join on in SQL - sql

TableA TableB
Column1 Column2 Column3 Column4
1 2 1 3
I have two table TableA(Column1,Column2) and TableB(Column3,Column4).I want to join two table using column1 ,column4(LIKE NATURAL JOIN). Is in SQL any things to join two table and return a new table with deleting repeated columns?
I want select this:
column1 column2 column4
1 2 3

DBMSes that support NATURAL JOIN require the column names of the join keys to match, and if you do SELECT * you will get only the unique column names. It doesn't make sense to try to specify column names, because the whole thing works by the names already being the same.
You MUST have same-named columns between the two tables, as it will use every same-named column between them to perform the join. Your tables TableA and TableB are unsuitable for a natural join as they don't share any column names.
So you are relegated to doing a regular join:
SELECT
A.*, -- you can at least get all the columns from one table
B.Column4 -- but you have to specify the rest one at a time
FROM
TableA A
INNER JOIN TableB B
ON A.Column1 = B.Column3
;
You just have to bite the bullet and write the query. You may want to not have to write the column names, but that's just not possible.
Some notes: When you say "return a new table", I think I know what you mean, but technically it is a rowset since to be a table it would have to be stored in the database with a name.
It may be possible to alias the column in a view or inline derived table, but you haven't told us what specific DBMS you're using so we can answer for its exact capabilities. It might look something like this:
SELECT
*
FROM
TableA A
NATURAL JOIN (
SELECT Column1 = Column3, Column4
FROM TableB B
) B
;
But notice that you still have to list all the other columns in TableB in order to do this. And I'm not even sure it works.

Joining two tables and querying on some or all columns doesn't return you a new table but record set. To get what you wanted try this. Below query adheres to SQL standard and thus should work on all SQL compliant databases.
SELECT ta.column1, ta.column2, tb.column4 from TableA ta INNER JOIN TableB tb ON (ta.column1 = tb.column4)
If you want to use Natural Join, you need to have same columns.

'Distinct' statement prevents repeating the similar rows too
SELECT Distinct
TableA.Column1,
TableA.Column2,
TableB.Column4
FROM
TableA INNER JOIN TableB ON TableA.Column1 = TableB.Column3

Related

SQL Inner Join w/ Unique Vals

Questions similar to this one about using DISTINCT values in an INNER JOIN have been asked a few times, but I don't see my (simple) use case.
Problem Description:
I have two tables Table A and Table B. They can be joined via a variable ID. Each ID may appear on multiple rows in both Table A and Table B.
I would like to INNER JOIN Table A and Table B on the distinct values of ID which appear in Table B and select all rows of Table A with a Table A.ID which appears matching some condition in Table B.
What I want:
I want to make sure I get only one copy of each row of Table A with a Table A.ID matching a Table B.ID which satisfies [some condition].
What I would like to do:
SELECT * FROM TABLE A
INNER JOIN (
SELECT DISTINCT ID FROM TABLE B WHERE [some condition]
) ON TABLE A.ID=TABLE B.ID
Additionally:
As a further (really dumb) constraint, I can't say anything about the SQL standard in use, since I'm executing the SQL query through Stata's odbc load command on a database I have no information about beyond the variable names and the fact that "it does accept SQL queries," ( <- this is the extent of the information I have).
If you want all rows in a that match an id in b, then use exists:
select a.*
from a
where exists (select 1 from b where b.id = a.id);
Trying to use join just complicates matters, because it both filters and generates duplicates.

SQL: Combining Join and where

I just started today with SQL and have following (probably quite newbie) question:
Given two Data Bases Data1 & Data2 having the same number of rows and an identical first column. I want to get all the columns from Data1, but only the rows that meet a condition involving columns of Data2.
I tried something like
SELECT
column2
column3
...
FROM
Data1
INNER JOIN Data2 ON Data1.column1 = Data2.column1
WHERE 'condition1 involving columns in Data2',
'condition2 involving columns in Data2',
...
;
This does not give me the column1, though. If I include it in the select statement above it throws an error 'Column reference column1 is ambiguous'.
Any ideas what is going on?
SELECT Data1.column1
FROM Data1 INNER JOIN Data2
WHERE Data1.column1 = Data2.column2
AND 'SOME CONDITION IS MET'
The key here is that your select needs to define which database column 1 is being selected from.
WHERE allows you to pair the two databases on their primary key (I assume)
AND allows you to add multiple conditions to your select.
The problem is that you are joining two tables with a column with the same name. So in those cases you must prefix the column name with the table name or alias (and it is a good idea to avoid future errors to prefix column names even if there are no repeated names). Data1.column1 in your case.
Basically, you should always precede column names with table names they belong to, or - a better option - use a table alias. For example:
select a.column1,
b.column2,
b.column3
from table1 a join table2 b on a.id = b.id
where b.some_column = 20
Doing so, there won't be any ambiguity there.

SQL merge tables with matching columns

How can I merge 2 tables with the same column names into 1 table? Something like this:
The 2nd table should fill in the 1st table.
This is as close as I got
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
But it seems to concatenate the 2nd table on there.
Is LEFT JOIN really the correct way to do this?
You should list the columns in the SELECT. Then you would readily see that all you need is COALESCE():
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;

How does JOIN work exactly in SQL

I know that joins work by combining two or more tables by their attributes, so if you have two tables that both have three columns and both have column INDEX, if you use table1 JOIN table2 you will get a new table with 5 columns, but what if you do not have a column that is shared by both table1 and table2? Can you still use JOIN or do you have to use TIMES?
Join is not a method for combining tables. It is a method to select records (and selected fields) from 2 or more tables where every table in the query must carry a field that can be matched to a field in another table in the query. The matched fields need not have the same name, but must carry the same type of data. Lacking this would be like trying to create meaning from joining a list of license plates of cars in NYC, with height data from lumberjacks in Washington state -- not meaningful.
Ex:)
Select h.name, h.home_address, h.home_phone, w.work_address,
w.department
from home h, work w
where h.employee_id = w.emp_id
As long as both columns: employee_id and emp_id carry the same information this query will work
In Microsoft Access, to get five rows from a three column table joined to a two column table, you'd use:
SELECT Table1.*, Table2.* FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1;
You can query whatever you want, and join whatever you want, though.
If your one table is a list of people, and your other is a list of cars, and you want to see what people have names that are also models of cars, you can do:
SELECT Table1.Name, Table1.Age, Table2.Make, Table2.Year
FROM Table1 INNER JOIN Table2 ON Table1.Name = Table2.Model;
Only when Name is the same as Model will it show a record.
This is the same idea for joining tables in any relational DBMS I've used.
You are right you can join two tables even if they do not have shared column.
Join uses primary to prevent mistakes on inserting or deleting when user trying to insert record that does not has a parent one or some thing like this.
join methods has many types you can view them here:
http://dev.mysql.com/doc/refman/5.7/en/join.html
LEFT JOIN: select all records from first table, then selecting all records from second table that fulfilling the condition after ON clause.
you can't join the tables if they do not share a common column. If you can find a 3rd table that has common columns with table1 and table2 you can get them to join that way. so join table2 and tabl3 on a common column and than join table3 back to table1 on a common column.

SQL Join for cell content, not column name

I read up on SQL Join but as far as I understand it, you can only join tables which have a column name in common.
I have information in two different tables, but the column name is different in each. I need to pull information on something which is only in one of the tables, but also need information from the other. So was looking to join/merge them.
Here is what I mean..
TABLE1:
http://postimg.org/image/hnd63c2f5/
The cell content 18599 in column from_pin_id also pertains to content in another table:
TABLE2:
http://postimg.org/image/apmu26l5z/
My question is how do I merge the two table details so that it recognizes 18599 is referring to the same thing, so that I can pull content on it from other columns in TABLE2?
I've looked through the codes on W3 but cannot find anything to what I need, as mentioned above, it seems to be just for joining tables with a common column:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
You can write as :
select * from table1
where from_pin_id in
(
select from_pin_id
from table1
intersect
select id
from table2
)
Intersect operator selects all elements that belong to both of the sets.
Change the table names and the columns that you select as needed.
SELECT table1.id, table1.owner_user_id, table1.from_pin_id, table2.board_id
FROM table1
JOIN table2 ON table1.from_pin_id = table2.id
GROUP BY id, owner_user_id, from_pin_id, board_id