0

Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses).

For example:

AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S)

Query the number of occurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

SELECT name||'('||SUBSTR(occupation, 1, 1)||')'
FROM occupations
ORDER BY name

UNION

SELECT "There are a total of"|| count(*) from occupations group by occupation;
3

8 Answers 8

1
select concat(name, '(',left(occupation,1),')') 
from occupations 
order by name asc;

select 'There are a total of ' ,count(occupation) ,concat(lower(occupation), 's.') 
from occupations
group by occupation 
order by count(occupation) asc, occupation asc;

//status of Testcase passed

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 10, 2022 at 22:32
1

My Approach

  select CONCAT(Name,'(',substr(OCCUPATION,1,1),')') 
    from OCCUPATIONS 
order by Name asc;

  select CONCAT('There are a total of ',count(OCCUPATION),' ',lower(OCCUPATION),'s.') 
    from OCCUPATIONs group by OCCUPATION 
order by COUNT(OCCUPATION) asc,OCCUPATION asc;
0

see output

sample input and output with code:

create table ns_occupations(name varchar(20));

insert into NS_OCCUPATIONS values('AActorName');

insert into NS_OCCUPATIONS values('ADoctorName');

insert into NS_OCCUPATIONS values('AProfessorName');

insert into NS_OCCUPATIONS values('ASingerName');


insert into NS_OCCUPATIONS values('ASingerName');

select * from NS_OCCUPATIONS;

SELECT name||'('||SUBSTR(name, 2, 1)||')'  shortname,count(*)  no_of_occupations
FROM ns_occupations
group by name||'('||SUBSTR(name, 2, 1)||')';

output:
ADoctorName(D)  1
AActorName(A)   1
AProfessorName(P)   1
ASingerName(S)  2
0
0

ORDER BY has to be last clause when you use UNION/UNION ALL:

SELECT name||'('||SUBSTR(occupation, 1, 1)||')' AS col
FROM occupations
union
select 'There are a total of'|| count(*) 
from occupations 
group by occupation
ORDER BY CASE WHEN col LIKE 'There are a total%' THEN 1 ELSE 0 END, col;
1
  • @a_horse_with_no_name Thanks, funny thing is that during copy paste from OP's core I changed it inside WHERE clause :) Commented Sep 17, 2018 at 14:52
0

select name+'('+left(occupation,1)+')' from occupations order by name union select 'There are a total of '+convert(nvarchar,count(occupation))+' '+occupation+'s.' from occupations group by occupation order by count(occupation);

0

Use the following query. If you don't use UNION then it will display the column name for the second table, which is not the ask.

FROM OCCUPATIONS
ORDER BY NAME)

UNION 

(SELECT CONCAT('There are a total of ',COUNT(OCCUPATION),' ',LOWER(OCCUPATION),'s.')
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(OCCUPATION),OCCUPATION);
0

For Sql Server select Name+'('+substring(Occupation,1,1)+')' from Occupations order by Name

select 'There are a total of ' + cast(count() as varchar) +' '+ lower(Occupation) +'s.' from Occupations Group by Occupation order by Count()

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 17 at 8:27
0
SELECT CONCAT('There are a total of ', COUNT(*), ' ', LOWER(occupation), 's.') AS result
FROM occupations
GROUP BY occupation
ORDER BY COUNT(*);
1
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community? Commented Jul 10 at 3:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.