Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a query to display the name of the alumni user(s) (Role-'Alumni') who has/have worked in maximum number of companies, sorted by name.


SQL
profile_id  company_name
1           Tcs
2           cts
3           hcl
1           hexa
4           mindtree
3           cts
1           cts


so the answer should be


SQL
profile-id    company_count
    1             3


This my query

SQL
select u.name,Count(distinct ex.company_name)
from user u
inner join role r on (r.id = u.role_id)
inner join experience ex on (ex.profile_id = u.profile_id)
where r.name ="Alumni"
group by u.name
--having Count(distinct ex.company_name) in (select max(ex.company_name) from experience ex where ex.profile_id = u.profile_id)
order by u.name
Posted
Updated 16-Apr-15 2:33am
v5
Comments
ZurdoDev 16-Apr-15 7:37am    
This is quite easy to do. Where are you stuck?
Harshit Wadhera 16-Apr-15 7:49am    
i know to count the distinct name but how find max that i don"t know.
ZurdoDev 16-Apr-15 7:51am    
You use the MAX() function. See Solution 1.
Harshit Wadhera 16-Apr-15 8:02am    
it's giving me error
ZurdoDev 16-Apr-15 8:07am    
1. Then give the error so we don't have to guess.
2. I did look at the solution and you posted the error there. The problem is you tagged this as SQL and not MySql. I suggest update your question so that it says MySql.

SQL
SELECT TOP 1 profile_id,COUNT(company_name) company_count FROM COMPANY GROUP BY profile_id ORDER BY MAX(company_name) DESC
 
Share this answer
 
Comments
Harshit Wadhera 16-Apr-15 7:53am    
i am getting this error
Check the manual that corresponds to your MySQL server version for the right syntax
vangapally Naveen Kumar 16-Apr-15 8:30am    
see this link you may get an idea regarding ERROR:
http://stackoverflow.com/questions/21509026/error-in-your-sql-syntax-check-the-manual-that-corresponds-to-your-mysql-server
Sandeep Mogaveer 16-Apr-15 8:32am    
i wrote this assuming that you are using SQL .. not My SQL!!
Harshit Wadhera 16-Apr-15 8:35am    
can you tell me query for mysql
Sandeep Mogaveer 16-Apr-15 8:45am    
No.Sorry I don't use MySQL
You are using the Reserved word User as table name in Your query .insted of user use [user]
Here explained
http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql[^]

try like this:
SQL
select u.name,Count(distinct ex.company_name)
from [user] u
inner join role r on (r.id = u.role_id)
inner join experience ex on (ex.profile_id = u.profile_id)
where r.name ="Alumni"
group by u.name
--having Count(distinct ex.company_name) in (select max(ex.company_name) from experience ex where ex.profile_id = u.profile_id)
order by u.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