SQL question - why i can't solve a problemHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to import an SQL file using the command line in MySQL?MySQL Query error using UNION/UNION ALL and Group ByMYSQL Order of Operations failing: ORDER BY is affecting the results of SELECT StatementKeep ORDER BY after UNIONSQL query to print occupation with print name with order
Why did Captain America age?
What does formal training in a field mean?
Improving Sati-Sampajañña (situative wisdom)
How to make a language evolve quickly?
How is CoreiX like Corei5, i7 is related to Haswell, Ivy Bridge?
Exception propagation
Is every story set in the future "science fiction"?
Different problems with tabularx
How can I avoid subordinates and coworkers leaving work until the last minute, then having no time for revisions?
We are two immediate neighbors who forged our own powers to form concatenated relationship. Who are we?
What does this quote in Small Gods refer to?
Two researchers want to work on the same extension to my paper. Who to help?
Would encrypting a database protect against a compromised admin account?
Is a vertical stabiliser needed for straight line flight in a glider?
Is there an application which does HTTP PUT?
Is there some sort of formula to determine how many bricks I would need to build a completely new structure?
Visual representation code coverage in VSCode
What's the "magic similar to the Knock spell" referenced in the Dungeon of the Mad Mage adventure?
I might have messed up in the 'Future Work' section of my thesis
What can cause an unfrozen indoor copper drain pipe to crack?
How to select certain lines (n, n+4, n+8, n+12...) from the file?
What's the difference between const array and static const array in C/C++
Was Mohammed the most popular first name for boys born in Berlin in 2018?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
SQL question - why i can't solve a problem
How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to import an SQL file using the command line in MySQL?MySQL Query error using UNION/UNION ALL and Group ByMYSQL Order of Operations failing: ORDER BY is affecting the results of SELECT StatementKeep ORDER BY after UNIONSQL query to print occupation with print name with order
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Why my solution of a problem on the study site is not accepted and showing "wrong answer"?
Here is the problem - https://www.hackerrank.com/challenges/the-pads/problem/ :
Generate the following two result sets:
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:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in
OCCUPATIONSand [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
My query is:
SELECT
CAST(NAME, CASE
WHEN occupation = 'Actor' THEN '(A)'
WHEN occupation = 'Doctor' THEN '(D)'
WHEN occupation = 'Professor' THEN '(P)'
WHEN occupation = 'Singer' THEN '(S)'
END)
FROM
occupations;
and
SELECT
CASE
WHEN occupation = 'Actor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Doctor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Singer'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Professor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
END
FROM
occupations
GROUP BY
occupation
ORDER BY
COUNT(occupation), occupation;
The query has same output like in the example on HackerRank. What am I doing wrong?
sql case-study
add a comment |
Why my solution of a problem on the study site is not accepted and showing "wrong answer"?
Here is the problem - https://www.hackerrank.com/challenges/the-pads/problem/ :
Generate the following two result sets:
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:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in
OCCUPATIONSand [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
My query is:
SELECT
CAST(NAME, CASE
WHEN occupation = 'Actor' THEN '(A)'
WHEN occupation = 'Doctor' THEN '(D)'
WHEN occupation = 'Professor' THEN '(P)'
WHEN occupation = 'Singer' THEN '(S)'
END)
FROM
occupations;
and
SELECT
CASE
WHEN occupation = 'Actor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Doctor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Singer'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Professor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
END
FROM
occupations
GROUP BY
occupation
ORDER BY
COUNT(occupation), occupation;
The query has same output like in the example on HackerRank. What am I doing wrong?
sql case-study
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54
add a comment |
Why my solution of a problem on the study site is not accepted and showing "wrong answer"?
Here is the problem - https://www.hackerrank.com/challenges/the-pads/problem/ :
Generate the following two result sets:
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:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in
OCCUPATIONSand [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
My query is:
SELECT
CAST(NAME, CASE
WHEN occupation = 'Actor' THEN '(A)'
WHEN occupation = 'Doctor' THEN '(D)'
WHEN occupation = 'Professor' THEN '(P)'
WHEN occupation = 'Singer' THEN '(S)'
END)
FROM
occupations;
and
SELECT
CASE
WHEN occupation = 'Actor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Doctor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Singer'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Professor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
END
FROM
occupations
GROUP BY
occupation
ORDER BY
COUNT(occupation), occupation;
The query has same output like in the example on HackerRank. What am I doing wrong?
sql case-study
Why my solution of a problem on the study site is not accepted and showing "wrong answer"?
Here is the problem - https://www.hackerrank.com/challenges/the-pads/problem/ :
Generate the following two result sets:
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:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in
OCCUPATIONSand [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
My query is:
SELECT
CAST(NAME, CASE
WHEN occupation = 'Actor' THEN '(A)'
WHEN occupation = 'Doctor' THEN '(D)'
WHEN occupation = 'Professor' THEN '(P)'
WHEN occupation = 'Singer' THEN '(S)'
END)
FROM
occupations;
and
SELECT
CASE
WHEN occupation = 'Actor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Doctor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Singer'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
WHEN occupation = 'Professor'
THEN CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
END
FROM
occupations
GROUP BY
occupation
ORDER BY
COUNT(occupation), occupation;
The query has same output like in the example on HackerRank. What am I doing wrong?
sql case-study
sql case-study
edited Mar 23 at 13:06
Kars
366211
366211
asked Mar 23 at 9:40
aka86aka86
33
33
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54
add a comment |
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54
add a comment |
1 Answer
1
active
oldest
votes
Two issues:
- The solution should work for any occupation, not just for Actor, Doctor, Profession or Singer. The tests that have different occupation data will fail.
- The first result is not ordered as was requested
First query:
SELECT CONCAT(Name, '(', substr(Occupation, 1, 1), ')')
FROM Occupations
ORDER BY Name;
Your second query again expects a few static occupations but will return null when the occupation in the test data is not one of them. Don't use CASE here.
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM Occupations
GROUP BY Occupation
ORDER BY COUNT(Occupation),
Occupation;
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312430%2fsql-question-why-i-cant-solve-a-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Two issues:
- The solution should work for any occupation, not just for Actor, Doctor, Profession or Singer. The tests that have different occupation data will fail.
- The first result is not ordered as was requested
First query:
SELECT CONCAT(Name, '(', substr(Occupation, 1, 1), ')')
FROM Occupations
ORDER BY Name;
Your second query again expects a few static occupations but will return null when the occupation in the test data is not one of them. Don't use CASE here.
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM Occupations
GROUP BY Occupation
ORDER BY COUNT(Occupation),
Occupation;
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
add a comment |
Two issues:
- The solution should work for any occupation, not just for Actor, Doctor, Profession or Singer. The tests that have different occupation data will fail.
- The first result is not ordered as was requested
First query:
SELECT CONCAT(Name, '(', substr(Occupation, 1, 1), ')')
FROM Occupations
ORDER BY Name;
Your second query again expects a few static occupations but will return null when the occupation in the test data is not one of them. Don't use CASE here.
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM Occupations
GROUP BY Occupation
ORDER BY COUNT(Occupation),
Occupation;
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
add a comment |
Two issues:
- The solution should work for any occupation, not just for Actor, Doctor, Profession or Singer. The tests that have different occupation data will fail.
- The first result is not ordered as was requested
First query:
SELECT CONCAT(Name, '(', substr(Occupation, 1, 1), ')')
FROM Occupations
ORDER BY Name;
Your second query again expects a few static occupations but will return null when the occupation in the test data is not one of them. Don't use CASE here.
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM Occupations
GROUP BY Occupation
ORDER BY COUNT(Occupation),
Occupation;
Two issues:
- The solution should work for any occupation, not just for Actor, Doctor, Profession or Singer. The tests that have different occupation data will fail.
- The first result is not ordered as was requested
First query:
SELECT CONCAT(Name, '(', substr(Occupation, 1, 1), ')')
FROM Occupations
ORDER BY Name;
Your second query again expects a few static occupations but will return null when the occupation in the test data is not one of them. Don't use CASE here.
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM Occupations
GROUP BY Occupation
ORDER BY COUNT(Occupation),
Occupation;
edited Mar 23 at 10:04
answered Mar 23 at 9:59
trincottrincot
134k1697133
134k1697133
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
add a comment |
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
You're welcome. Let me know if it works.
– trincot
Mar 23 at 10:45
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
Ye, it works! I google it(Concat) and write my own like you did it
– aka86
Mar 24 at 12:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312430%2fsql-question-why-i-cant-solve-a-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to stack overflow. Please format your code and put it in code blocks next time.
– Kars
Mar 23 at 9:46
Welcome to SO! Please don't link to external sites with your problem description. Add all necessary information in your question to make it a Minimal, Complete, and Verifiable example
– Frank Schmitt
Mar 23 at 9:54