But both answers do not work. I have pasted the question below followed by my solution. Thanks for the help! If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically. The longest-named city is obviously PQRS, but there are options for shortest-named city; we choose ABC, because it comes first alphabetically. Note You can write two separate queries to get the desired output. It need not be a single query.
You have a problem here - there's no such command as fetch first row only. Actually your codes seems right. If you are running your codes on "MS SQL Server" it will give you some "built-in function" problems Like on mysql its written lengt but on ms sql server its written len Or "Limit 1" etc.
You can also use this query as a slightly different answer. Learn more.
Asked 2 years, 1 month ago. Active 2 months ago. Viewed 8k times. Jassim Jassim 29 1 1 silver badge 2 2 bronze badges. I would like to know why my answer is wrong and secondly the solution to the problem. Active Oldest Votes. Your solution on github looks as follows: select city, length city from station order by length city DESC,city ASC fetch first row only; select city, length city from station order by length city asc ,city asc fetch first row only; You have a problem here - there's no such command as fetch first row only.
Anatolii Anatolii 6, 2 2 gold badges 17 17 silver badges 43 43 bronze badges. I excluded the union and it worked fine. Can you correct me on using union here? ProteetiProva which SQL system are you running this on? ProteetiProva I edited my answer with your fixed solution. Mickael B. Elletlar 2, 4 4 gold badges 22 22 silver badges 33 33 bronze badges. Shruti Srivastava Shruti Srivastava 1. Welcome to Stack Overflow. Vikrant Sharma Vikrant Sharma 1 2 2 bronze badges.
Communist Hacker Communist Hacker 21 5 5 bronze badges. While the shared code may answer the question, adding a bit of context or explanation is generaly useful and improve the quality of the answer. JArpit JArpit 1 2 2 bronze badges. Maryam Maryam 23 7 7 bronze badges. Rivu Dass Rivu Dass 1.
Sign up or log in Sign up using Google.Add a description, image, and links to the hackerrank-sql-solutions topic page so that developers can more easily learn about it. Curate this topic. To associate your repository with the hackerrank-sql-solutions topic, visit your repo's landing page and select "manage topics. Learn more. Skip to content. Here are 12 public repositories matching this topic Language: All Filter by language.
Sort options. Star Code Issues Pull requests. My solutions to HackerRank problems. Updated May 11, Java. Star 6.
Updated Mar 28, C. Updated Jul 14, SQL. Star 1. My own HackerRank coding interview solutions. Contains solutions of the sql queries of HackerRank. Star 0. Updated Jul 4, Python. Updated Apr 30, C. Updated Jan 26, Solution to HackerRank problems.Questions solved from Various Coding websites viz.
This repository also contains Questions from various offline and onsite competitions. Programming everyday. Program makes not only computer smarter but makes us smarter too. So keep solving and write program. Programs that we find in the competitions and some brainstorming questions. My solutions to problems from various competitive programming websites. Add a description, image, and links to the hackerrank-solutions topic page so that developers can more easily learn about it.
Curate this topic. To associate your repository with the hackerrank-solutions topic, visit your repo's landing page and select "manage topics. Learn more. Skip to content. Here are 1, public repositories matching this topic Language: All Filter by language. Sort options. Star Code Issues Pull requests. Updated Jun 25, Java. Updated Jun 4, Python.
Hackerrank Problem solving solutions in Python. Updated Jul 6, Python. Solutions of challenges of Hackerrank Python domain.
The reason for this is that when you group only c. Had same issue on some other questions. I did it exactly the same, but in Oracle.
Subscribe to RSS
Didn't took me that much time though. You need to find the max count only 1 then a list of counts where there's only one hacker. You bro are a lifesaver, i think the bigger challenge with these problems is understanding the langugae.
Now I can die alone in peace. Same cat differently skinned.
Sharing since it is more explicit though admittedly not as efficient in the hopes it might help anyone stuck on how to arrive at the set of unique counts to include after including max count achieving hackers. In a literal sense, aliasing like the use of "counts" helps you follow that the immediately prior code represents a logical "table" of values, and in many cases is required by MySQL.
In the step above the portion involving "counts" the first instance of a HAVING clausewe find the max number of challlenges for all hackers. The alias "counts" represents our compared to "table" of challenge values done by grouping it by counts of challenges. It the particular value of challenges completed is unique because when compared, there is only one instance of that count of challenges completed. This is all done to meet this requirement in the problem: "If more than one student created the same number of challenges AND the count is less than the maximum number of challenges created, then exclude those students from the result.
If you don't include 'counts' or some other alias name then you will get an error because every "derived table" must have an alias, even if it is not referred to anywhere else. Thank you very much for your help. I didn't understand from the text of the problem what was pretended. But looking for your query I understand it.The output column headers should be DoctorProfessorSingerand Actorrespectively. Occupation will only contain one of the following values: DoctorProfessorSinger or Actor.
Explanation The first column is an alphabetically ordered list of Doctor names. The second column is an alphabetically ordered list of Professor names. The third column is an alphabetically ordered list of Singer names. The fourth column is an alphabetically ordered list of Actor names. The empty cell data for columns with less than the maximum number of names per occupation in this case, the Professor and Actor columns are filled with NULL values.
To solve this problem, we can use user-defined variables to help create a new table. Take the sample input as example, the table we want to create looks like below:. The RowLine represents the line where the name should be put. To get table tuser-defined variables and CASE operator can help. We create four variables to record the line number RowLineone for each occupation. We use CASE to add variables according to occupation.
Toggle navigation Memogrocery. Home About Archives Tags. Analysis To solve this problem, we can use user-defined variables to help create a new table. Solution 1.Julia asked her students to create some coding challenges. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
Students and both created 4 challenges, but the maximum number of challenges created is 6 so these students are excluded from the result. Students and both created 6 challenges. Because 6 is the maximum number of challenges created, these students are included in the result. Next, we need to exclude students who have same number of challenges with others and the number is not the maximum.
Students included in the output should satisfy either of the two requirements: the total number is the maximum or the number only appears once. Toggle navigation Memogrocery. Home About Archives Tags. Problem Julia asked her students to create some coding challenges.
For Sample Case 1we can get the following details: Students and both created 6 challenges.Structured Query Language is one of the most important languages used in the industry. HackerRank is a platform for competitive coding. Let us code and find answers to our given problems.
Revising the Select Query 1. Input Format. The CITY table is described as follows:. Revising the Select Query 2. Select All. Query all columns attributes for every row in the CITY table. Select By ID. Weather Observation Station 1. Weather Observation Station 3. You may print the results in any order but must exclude duplicates from your answer.
Weather Observation Station 4. Weather Observation Station 5.Querying Multiple Tables with SQL
If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically. Weather Observation Station 6. Query the list of CITY names starting with vowels i. Your result cannot contain duplicates. Weather Observation Station 7. Weather Observation Station 8. Weather Observation Station 9. Weather Observation Station Higher Than 75 Marks.
Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters i. The Name column only contains uppercase A - Z and lowercase a - z letters.