Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two table one is EMP_DETAILS which is containing 7 columns (EMP_ID
[Primary Key], FIRST_NAME, LAST_NAME, AGE, ADDRESS, SALARY, DEPT_ID
[Foreign Key]
) and another one is DEP_DETAILS which is containing 2 columns (DEPT_ID [Primary Key], DEPT_NAME).

I want to create a single View for those tables but without using JOIN Query. Means :

EMP_ID FIRST_NAME LAST_NAME AGE ADDRESS SALARY DEPT_ID DEPT_NAME
Posted
Updated 16-Dec-14 23:53pm
v3
Comments
Tomas Takac 17-Dec-14 5:53am    
This seems to be trivial. What did you try so far?
Shweta N Mishra 17-Dec-14 5:54am    
Why dont you want to use JOIN?, Use where clause but this going backword in tech
Tomas Takac 17-Dec-14 5:57am    
I just noticed the "without using JOIN Query" requirement. What's wrong with joins?

I want to create a single View for those tables but without using JOIN
Unfortunatelly, you can't!

I'd suggest to back to basics and read this: Visual Representation of SQL Joins[^]
 
Share this answer
 
Comments
Tomas Takac 17-Dec-14 6:27am    
+5 for clearly saying the requirement is nonsense
Maciej Los 17-Dec-14 6:41am    
Thank you, Tomas ;)
jaket-cp 17-Dec-14 6:33am    
you have my 5 on this also, even though I put in a solution :)
Maciej Los 17-Dec-14 6:41am    
Thank you ;)
It can be done with a where clause.
Used to do it like this years ago, I believe before MS SQL Server 2000 come out.
SQL
select 
	FIRST_NAME, 
	LAST_NAME, 
	AGE, 
	ADDRESS, 
	SALARY,
	DEPT_NAME.DEPT_ID,
	DEPT_NAME
from EMP_DETAILS, DEPT_NAME
where EMP_DETAILS.DEPT_ID = DEPT_NAME.DEPT_ID

But I recommend to use join.
SQL
select 
	FIRST_NAME, 
	LAST_NAME, 
	AGE, 
	ADDRESS, 
	SALARY,
	DEPT_NAME.DEPT_ID,
	DEPT_NAME
from EMP_DETAILS
inner join DEPT_NAME
	on EMP_DETAILS.DEPT_ID = DEPT_NAME.DEPT_ID
 
Share this answer
 
Comments
Maciej Los 17-Dec-14 6:17am    
Using where in first query is - in fact - join. It meant that query returns only matching data.
My vote of 4, becasue of Join recommendation.
jaket-cp 17-Dec-14 6:24am    
Yes I get your point.
I wanted to show that even though it can be done, it is not the recommended way of doing it.
Hopefully they will use join syntax.
Also with more complicated queries, using the where clause to do the joins is not the way to go.
:)
Bittu14 17-Dec-14 6:20am    
THANK YOU SO MUCH.
jaket-cp 17-Dec-14 6:26am    
Glad to help, but take note off all the comments mentioned about using the JOIN - it is the best way to go.
As you can see from the two queries, the join query is very similar to the where clause method.
Try this

SQL
select t1.EMP_ID,	t1.FIRST_NAME,t1.LAST_NAME, t1.AGE, t1.ADDRESS, t1.SALARY, t2.DEPT_ID, t2.DEPT_NAME from EMP_DETAILS t1, DEP_DETAILS t2
 
Share this answer
 
Comments
Tomas Takac 17-Dec-14 6:10am    
I don't think that cross join is the solution. What is the result good for? Did you forget the where clause?
KM Perumal 17-Dec-14 6:20am    
Yeah ...without Join Keyword
Tomas Takac 17-Dec-14 6:24am    
I was tempted to vote you down but as OP accepted your answer I will let it be. But do you realize that there is no meaning in the results your query produces?
KM Perumal 17-Dec-14 6:41am    
Your wishes ... this is return expected answer
Maciej Los 17-Dec-14 6:13am    
Sorry, but your answer deserved for 1!
Above query - in fact - is cross join, which is undesirable.

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