I have two select query statements. I dont know to create table here.
Below are two tables. Id is same, but Fname and Lname is different.
Table1 have two columns
id Fname
1 Ahila
Table2 have two columns
id Lname
1 Kavitha
Output table should be single row with id and Fname and Lname : 1 and Ahila and Kavitha respectively
Please help me on this.
you need to use a join to join the two tables together, something like:
select t1.id, t1.Fname, t2.Lname
from Table1 t1
inner join Table2 t2 on t1.id = t2.id
Use JOIN statement
SELECT T1.id id , T1.Fname Fname, T2.Lname Lname
FROM table1 T1
JOIN table2 T2 ON T1.id = T2.id
select Fname
from Table1
join Table2
on Table1.id = Table2.id
where id = 1
Related
I wanted to update table1 based on table2 which has a common field employee_ID. I always have unique employee_ID in table1 but in table2 it may contain duplicate records with same employee_ID.
I got another column in table2 where the modified_date is inserted. I want to update the table1 employee name with the table2 employee name based on employee_id and recent modified date.
I got multiple columns to update with the same type of condition. any idea, here is so far i tried.
Here is the query, i have using inner join,
ssql = "Update Table1 INNER JOIN Table2
ON Table1.employee_id= Table2.employee_id
SET Table1.type= Table2.type"
any help will be appreciated
try this query
update t1 set t1.employee_name = t2.employee_name from table1 as t1
inner join table2 as t2 on t1.employee_id = t2.employee_id
where t2.Modified_date = (select Max(modified_date) from table2 as tb2 where
tb2.employee_id = t2.employee_id group by tb2.employee_id)
You need an intermediate step to associate the Max(modified_date) to each employee_id.
Using a CTE expression you can try something like this :
with
more_recent as (
select
employee_id,
Max(modified_date) max_modified_date
from
table2
group by
employee_id
)
update
t1
set
t1.employee_name = t2.employee_name
from
table1 as t1
inner join more_recent mr on
t1.employee_id = mr.employee_id
inner join table2 as t2 on
mr.employee_id = t2.employee_id
mr.max_modified_date = t2.modified_date
I have some set of records, but now i have to select only those records from this set which have theeir Id in either of the two tables.
Suppose I have table1 which contains
Id Name
----------
1 Name1
2 Name2
Now I need to select only those records from table one
which have either their id in table2 or in table3
I was trying to apply or operator witin inner join like:
select *
from table1
inner join table2 on table2.id = table1.id or
inner join table3 on table3.id = table1.id.
Is it possible? What is the best method to approach this? Actually I am also not able to use
if exist(select 1 from table2 where id=table1.id) then select from table1
Could someone help me to get over this?
Use left join and then check if at least one of the joins has found a relation
select t1.*
from table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
where t2.id is not null
or t3.is is not null
I would be inclined to use exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.id = t1.id) or
exists (select 1 from table3 t3 where t3.id = t1.id) ;
The advantage to using exists (or in) over a join involves duplicate rows. If table2 or table3 have multiple rows for a given id, then a version using join will produce multiple rows in the result set.
I think the most efficient way is to use UNION on table2 and table3 and join to it :
SELECT t1.*
FROM table1 t1
INNER JOIN(SELECT id FROM Table2
UNION
SELECT id FROM Table3) s
ON(t.id = s.id)
Alternatively, you can use below SQL as well:
SELECT *
FROM dbo.Table1
WHERE id Table1.IN ( SELECT table2.id
FROM dbo.table2 )
OR Table1.id IN ( SELECT table3.id
FROM Table3 )
I am trying to to join two tables in order to replace the numeric values of one from the other, the problem is that i have two columns in the same table. See this example to make it clear :
Table_1 t1_ID INT, t1_Name VARCHAR(20)
Table2 t2_ID INT , ONE Table_1 , TWO Table_1
in table 2 i store the id and i want to make a join in order to replace these id's with t1_Names.
I have tried this structure but it gives wrong answers
Select *
FROM table1
JOIN table2 ON table1.id=table2.table1_id
JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id
You can join the same table twice, but you need to give this table different names (alias name)
Select t2.id, t1_1.name, t1_2.name
FROM table2 t2
JOIN table1 t1_1 ON t1_1.id = table2.table1_id
JOIN table1 t1_2 ON t1_2.id = table2.table2_id
I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.
I have two tables:
table 1
fid
field
queryorder
table 2
fid
field
name
fid and field are same in both tables. I want a query like
select fid
from table 1
where field in table 1 corresponding to name = dell
and should not have queryorder = 1
Your question is not clearly worded, but if I'm reading it right, all you want to show is the fid from table 1, where the "name" field in table2 = dell, and the "queryorder" field from table 1 is not equal to "1".
SELECT
table1.fid
FROM
table1
INNER JOIN
table2
ON
table1.fid = table2.fid
AND table1.field = table2.field
WHERE
table2.name = 'dell'
AND table1.queryorder <> 1
Small piece of advice: you should not name a field "name" -- that's a keyword, and will cause you headaches down the road.
SELECT fid FROM table1 WHERE name='dell' AND fid NOT IN (SELECT fid FROM table2 WHERE queryorder != 1)
It's very hard to tell what you're asking from the question, there. This will give you all fids where the name in table1 is 'dell' and the queryorder in table2 is not 1.
This looks like it's really just a join, but I can't tell what you're looking for exactly..
SELECT DISTINCT(fid) FROM Table1 T1
JOIN Table2 T2 ON T1.fid = T2.fid
WHERE t2.queryOrder != 1 AND T1.name = 'DELL'
select t1.fid
from table1 t1, table2 t2
where t1.fid = t2.fib
AND t1.field = t2.field
and t2.name= 'dell'
and t1.queryorder <> 1