force table name before field in select - sql

SELECT *
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
Both tables have a date_createField, so when I use select *, date_createField is returned twice. I could solve this by changing my select to:
SELECT T1.date_createField, T2.date_createField
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
But is it possible to not specify the specifik fields (keep select *), and force the table name in front of the property?
I'm having this problem because I'm joining 2 tables with a lot of columns and some columns have the same name. I would like to use select * and still have a distinction between columns present in both tables. Is this possible?

This is not possible you need to specify the column names or accept all columns. If this is a query you will execute often make a view which can be reused.

May this will help you
SELECT T1.*, T2.* FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK

Related

SQL joining of two different queries

Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;

How to query the specific vales in columns form different tables(SQLite)?

I have two tables, table1 and table2. table1 has 'id' column. table2 has 'id' and 'quantity' columns. I want to compare the same values under 'id' columns from both tables and show the value under 'quantity' column form table2.
The simple logic is "select id=1 from table1 compare with select id=1 from table2, then show the quantity value from table2". Is there any way to query this statement?
Join the tables:
SELECT *
FROM
table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
Using INNER JOIN will cause the results to show only rows where the same ID is present in both tables. If you want to show all rows from table 1 and matching rows from table 2 plus blanks for any rows where there is no table 2 id for a particular table 1 id, then use LEFT JOIN instead of INNER JOIN.
If you only want certain columns in your output you can list them:
SELECT t1.id, t2.quantity
...
etc
More about joins - for future learning
In most other database systems there exists the corollary of LEFT join; RIGHT join. If you want all rows from table 2 plus matching rows from table 1, and blanks where there is no related table 1 row, use RIGHT JOIN. SQLite doesn't support RIGHT joins, so you'll have to rewrite your query (swap the table names around) so t2 is on the LEFT. It's the table name that matters, not the order of appearance in the ON section:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
--all rows from t2 plus any matching rows from t1
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Eventually you'll come across a FULL [OUTER] JOIN which is "all rows from t1 and any matching from t2, plus any additional rows from t2 and their possible matches from t1". SQLite doesn't support this either, but it can be emulated. Ordinarily it's emulated with a LEFT JOIN UNION RIGHT JOIN, but as SQLite doesn't support RIGHT, you'll have to emulate it with two LEFT joins, one with the tables swapped round:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
UNION
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Cor. All that for the sake of being able to say this in e.g. SQLServer:
SELECT *
FROM
table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = t2.id

SQL Join tables with empty values

I have 2 tables
Lets say Table1 and Table2
They both have one shared value(id)
What I'm looking for is whether there is any function to combine them both based on that key, however if table2 has more elements, i want columns of table1 to be empty, and if table1 has more elements, table 2 columns to be empty
I tried a lot of different joins, but most of the time I end up with a lot of duplicate values as it tries to fill in both sides.
Tried Full outer join, Full join, etc
You are looking for full join:
select t1.*, t2.*
from t1 full join
t2
on t1.id = t2.id;
The above code from Gordon is right. However, since you have not specified the database and its version, I will post an alternate version for MySQL, which should also work for other databases.
Without duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id
With duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION ALL
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id

create a sql join with more columns

i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz
You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.

SQL select rows based on 2 col criteria in a separate table

I wish to select some rows from a table based on values from another table:
Table1 (wish to select from here)
Columns Date, Name, Pay
Table2 (contains a 'list' that determines what is selected from Table1)
Columns Date, Name
The query I wish to write is to:
Select Date,Name,Pay from Table1 where Date,Name is present in Table2
I got as far as being able to do it on one value
SELECT Date,Name,Pay FROM Table1 WHERE Table1.Name IN (Select Table2.name from Table2)
but Im stuck with how to add the date qualifier. The names in either table are not unique, what makes them unique is the date and name combination.
If I understood your question clearly, you want to apply join
select t1.Date,t1.Name,t1.Pay FROM Table1 t1 inner join Table2 t2
ON t1.Name = t2.Name and t1.Date = t2.Date
The generic SQL solution uses exists:
Select Date, Name, Pay
from Table1 t1
where exists (select 1 from table2 t2 where t2.date = t1.date and t2.name = t1.name);
This will not match values in table 2 if they are NULL. For that, you would need a NULL-safe comparison operation. The ANSI standard is is not distinct from.
Some databases support in with tuples. In those databases, you can write:
Select Date, Name, Pay
from Table1 t1
where (t1.date, t1.name) in (select t2.date, t2.name from table2 t2);
Once again, this might have an issue with NULL values, depending on how you want to treat them.
Interestingly, you could extend your logic by using a correlated subquery:
SELECT Date, Name, Pay
FROM Table1 t1
WHERE t1.Name IN (Select t2.name from Table2 t2 where t2.date = t1.date);
Although this does what you want, I think the previous two approaches are clearer in their intent.
I should note that you could use a join for this. However, that would return duplicate values if you had duplicates in table2. For that reason, I prefer the exists or in methods, because these have no risk of duplicating values.
You can use alias (and instead of subquery a join ) for a more easy vision of your related table
SELECT a.Date, a.Name, a.Pay
FROM Table1 a
inner join Table2 b on a.name = b.name
in this case date is obtain from table1, changing the alias or addingi both column if you need more