Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
3.40/5 (3 votes)
See more:
Hi,

i am having two tables..
1) employee table columns are EmpId,EmpName,CityId....
2) City table columns are CityId,CityName...

Now i want to fetch employeeId,Employee name,cityname from these two tables without using joins...

is it possible ...
Posted
Updated 12-Oct-13 0:19am
v2

It is possible.try this

select a.EmpId,a.EmpName,a.CityId,b.CityId,b.CityName from employeetable as a,citytable as b
 
Share this answer
 
v2
Comments
Balasubramanian T 12-Oct-13 6:31am    
ya..its working fine.thank you. may i know why we go for this instead of joins...
Well...yes. But it's a stupid idea!

If you have a link between the two tables, then you presumably want to return data on a row that is only relevant to that row - and the best way to do that is with a JOIN.
If you don't return only the relevant rows:
SQL
SELECT * FROM MyTable, MyTable2
Then it can only retirn each row of MyTable2 for each row of MyTable: so if you have 6 Rows in MyTable and 2 in MyTable2, the it will return 12 rows.

If you use a join:
SQL
SELECT * FROM MyTable m1
JOIN MyTable2 m2 ON m1.ID=m2.Id
Then it returns only the rows you are interested in.
You could do it without a JOIN if you were really desperate:
SQL
SELECT * FROM MyTable, MyTable2 WHERE MyTable.ID=MyTable2.Id
And it will return the same rows.

In performance terms, there is no difference, but the JOIN form is a lot easier to maintain, particularly if the query becomes more complex.
Good practice recommends the JOIN over the WHERE form.
 
Share this answer
 
SQL
SELECT
    e.EmpId
    , e.EmpName
    , e.CityId
    , (SELECT TOP 1 c.CityName FROM City c WHERE c.CityId = e.CityID) AS CityName
FROM
    employee e
 
Share this answer
 
SQL
select e.EmpId,e.EmpName,f.CityName from Employee e,City f where e.CityId=f.CityId
 
Share this answer
 
v2
SELECT EmpId,EmpName,CityName FROM employee  CROSS JOIN City WHERE City.CityId =  employee.CityId
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900