Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
SQL
select name from user where profile_id in (select profile_id from experience where company_name='TCS' and ((year(end)>='2012' and year(start)<='2012') or end is null)) order by name;
Posted
Updated 28-Apr-16 2:29am
v3
Comments
Nigam,Ashish 28-Apr-16 6:29am    
what is the problem you are getting?
Patrice T 28-Apr-16 8:18am    
What is wrong here ?

1 solution

You are getting errors because you have used reserved words for column and table names user, end, name.

All you need to do is surround those names with square brackets i.e.
SQL
select [name] 
from [user] where profile_id in 
(
	select profile_id 
	from experience where company_name='TCS' and ((year([end])>='2012' and year(start)<='2012') or [end] is null)
) order by [name];


Your checking of year(end) is also a little messy - you only need to check for equality because you are only looking at the datepart Year...
SQL
select [name] 
from [user] where profile_id in 
(
	select profile_id 
	from experience where company_name='TCS' and (year([end]) = 2012 or [end] is null)
) order by [name];


A better approach to using the sub-query on table experience would be to use an INNER JOIN - that way only data that matches both tables will be returned. I.e.
SQL
select [name] 
from [user] U
INNER JOIN experience E ON U.profile_id = E.profile_id
where E.company_name='TCS' and (year(E.[end]) = 2012 or E.[end] is null)
order by [name];
 
Share this answer
 
v2

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