6 kyu

SQL Bug Fixing: Fix the JOIN

9,417matt c
Description
Loading description...
SQL
Fundamentals
  • Please sign in or sign up to leave a comment.
  • AnalystADAL Avatar

    Here is my poem for this exercise:

    -- I thought I could round with ROUND(),

    -- I even thought I could cast something with CAST(),

    -- but only trunc() helped me before I truncated, while I was progressing!

    -- The decimals danced away, ROUND couldn’t stay,

    -- But trunc() stepped in, leading the way!

  • chomp Avatar
    expected: [{:average_salary=>99626.51, :job_title=>"fisherman", :total_people=>1, :total_salary=>99626.51}, {:a....52}, {:average_salary=>14386.49, :job_title=>"cashier", :total_people=>1, :total_salary=>14386.49}]
         got: [{:average_salary=>99626.51, :job_title=>"fisherman", :total_people=>1, :total_salary=>99626.51}, {:a...}, {:average_salary=>10266.25, :job_title=>"astronomer", :total_people=>1, :total_salary=>10266.25}]
    

    Average and total values are the same in this dataset, as every job has only 1 person. Sorting should be done descending by highest average salary. But the cashier is expected to be the last/ the one with the least salary of 14386.49, even though the astronomer is only receiving a measly salary of 10266.25.

    Edit

    Nevermind, my bad!

  • John Sypro Avatar

    This comment has been hidden.

  • DanGrey Avatar

    This comment has been hidden.

  • KyPhamGit Avatar

    was stuck on this problem for days........

    first thing i learnt is that round() needs a numeric input and integer for dp. i.e round(x,y), x must be numeric and y must be integer.

    second thing i learnt is that the rounding changes only apply once converted to float afterwards.. confusing. i.e round(x,y)::float will then show the rounded number as however decimal places.

  • mvaldes42 Avatar

    Had the issue of output detected 11 decimals when <= 2 was asked on testing. But this issue does not appear on submitting.

  • felipesebben Avatar

    Thank you for this kata. It is always challenging to deal with float numbers and decimal points in POSTGRES. It took me a while to finally round the numbers while casting them as float types.

  • Amna Iqbal Avatar

    My kata passes when I test it but it fails on the attempt.Does anyone have any idea why? This is the error that I get : expected: [{:average_salary=>98677.2, :job_title=>"engineer", :total_people=>1, :total_salary=>98677.2}, {:aver...1}, {:average_salary=>10252.36, :job_title=>"economist", :total_people=>1, :total_salary=>10252.36}] got: [{:average_salary=>82058.12, :job_title=>"lifeguard", :total_people=>4, :total_salary=>328232.49}, {:...1}, {:average_salary=>10252.36, :job_title=>"economist", :total_people=>1, :total_salary=>10252.36}]

  • sl4dex Avatar

    omg why is it so difficult to get a result with 2 decimal places. Converting to float results in 3 decimal places, ROUND, TRUNC, FORMAT are not accepted. I have also tried CAST and CONVERT with decimal and numerical. how do I get exactly 2?

  • ThienQuocDongPhuc Avatar

    This comment has been hidden.

  • okutewonah Avatar

    For some weird reason CAST AS FLOAT gives more than 2 decimal places for some outputs.

    I ended up learning something new today. Once you get hold of the casting, it just flows

  • Dfach Avatar

    round(avg(salary), 2) did not appear to work, which tripped me up. Whats going on is it is doing the round you want, but the numeric format is weird and it is displaying it in a really weird way. You need to convert it to the format that they tell you to, which doesn't display it in a weird way after you convert the numeric salary column.

  • Infuzibil Avatar

    Easier to rewrite the query than fix the freaking code.

  • user5763476 Avatar

    it's a bit tricky this one, but one needs to pay attention to the relationship between the function round and what datatype it takes on. Then do a final conversion.

  • MikeIsHereWorking Avatar

    expected: [{:average_salary=>99886.55, :job_title=>"mechanic", :total_people=>1, :total_salary=>99886.55}, {:av....29}, {:average_salary=>12786.66, :job_title=>"painter", :total_people=>1, :total_salary=>12786.66}] got: [{:average_salary=>99886.55, :job_title=>"mechanic", :total_people=>1, :total_salary=>99886.55}, {:av....29}, {:average_salary=>12786.66, :job_title=>"painter", :total_people=>1, :total_salary=>12786.66}]

    That's all.

  • MikeIsHereWorking Avatar

    Error: total_salary should be 2 or less decimal places expected: <= 2 got: 12

    job_title average_salary total_people total_salary tailor 15900.29 1 15900.29 actor 17954.17 1 17954.17 factory worker 19717.98 1 19717.98 doctor 21722.36 1 21722.36 businessman 23037.04 1 23037.04 lawyer 27057.47 1 27057.47 engineer 30808.39 1 30808.39 receptionist 35000.08 1 35000.08 secretary 39797.21 1 39797.21 fisherman 40295.4 1 40295.4 butcher 41223.13 1 41223.13 gardener 41857.21 1 41857.21 police officer 47045.42 1 47045.42 artist 47755.41 1 47755.41 shop assistant 27663.36 2 55326.72 firefighter 59212.62 1 59212.62 cashier 60425.93 1 60425.93 air traffic controller 63211.79 1 63211.79 attorney at law 65427.96 1 65427.96 waiter 70950.09 1 70950.09 bartender 71742.92 1 71742.92 translator 73089.47 1 73089.47 model 24517.37 3 73552.12 statistician 76844.82 1 76844.82 mechanic 39000.95 2 78001.91 actress 79604.24 1 79604.24 soldier 84446.22 1 84446.22 programmer 85284.75 1 85284.75 newsreader 42697.26 2 85394.52 writer 86959.81 1 86959.81 hairdresser 29945.77 3 89837.32 architect 91175.88 1 91175.88 bookkeeper 53904.55 2 107809.1 judge 54984.5 2 109969 coach 56126.13 2 112252.26 nurse 57505.69 2 115011.39 economist 61333.53 2 122667.06 interpreter 62317.38 2 124634.77 banker 62665.09 2 125330.17 librarian 64190.55 2 128381.11 traffic warden 66440.38 2 132880.77 advocate 45191.47 3 135574.4 optician 71266.32 2 142532.64 teacher 71551.3 2 143102.6 farmer 75535.01 2 151070.02 lifeguard 52054.48 3 156163.45 photographer 78222.95 2 156445.9 carpenter 81509.43 2 163018.86 biologist 54818.24 3 164454.73 philosopher 57412.12 3 172236.38 veterinary doctor 60126.23 3 180378.7 dentist 46161.26 4 184645.05 window cleaner 48197.38 4 192789.52 builder 68239.43 3 204718.3 dietician 69915.12 3 209745.34 taxi driver 87674.31 4 350697.22

  • LukeJanicki Avatar

    This comment has been hidden.

  • vishnu.konjeti Avatar

    This comment has been hidden.

  • JRBOH Avatar

    It seems to me that both the tests and the submission don't check for the requirement, resulting table should be ordered by average salary descending.

  • sarahsavage Avatar

    Why do so many solutions have a join in them? All the relevant information was in the job table. Am I missing something here?

  • sergio_l Avatar

    This comment has been hidden.

  • sergio_l Avatar

    Do you have some advice on how to approach error handling in SQL Katas?

    I did a lot of Katas in python. With print()-Function and IDE I can look for mistakes in my code easily, but in SQL I am doing to much trial and error.

  • Testing_Cat Avatar

    No idea if anyone ran into this issue, but sometimes when testing the solution to this kata the test will fail stating that "total_salary does not have 2 decimals" despite all values in the table fulfil this requierement.

    And when skipping the test completely and running the attempt this does not happen! Maybe is a bug with the test code.

  • antonia.nwufo Avatar

    I'm struggling to understand why my test code passes but fails the attempt, especially when the numbers that I can see match the expected output? Help!

  • nockz57 Avatar

    This comment has been hidden.

  • thenischaygowda Avatar

    How to roundoff for 2 dp. ROUND() giving an error.

  • yashkumar1992 Avatar

    It looks like need to sort by total salary also. Really not sure why!

  • Jengkan Avatar

    Fun SQL katas!

    I suggest adding a test to check the ORDER BY. Otherwise it's crystal clear what is working and not.

  • zidoxx Avatar

    I do everything as possible to made this kata, passed the first test and when i try to do the attemp this fail, and the results are the same that the output, i hope you can fixed, thanks.

  • VladislavPb Avatar

    I don't really get why this kata named '...JOIN'

  • goncalo29 Avatar

    This comment has been hidden.

  • connect2mak Avatar

    Test Failedexpected 513.01 (#BigDecimal:55970764d9c8,'0.51301E3',18(18)) to be a kind of Float --> how to get float successfully in postgres 1 test case fail due to float cast( (SUM(j.salary) / COUNT(p)) as float(2) ) as average_salary numeric is failing

  • tomato-project Avatar

    Is the expected solution really ordered by the total_salary?? hmm..

  • Ann-Kathrin Avatar

    Whenever I click "Run sample tests" I get different results - without changing anything in my code. I always pass all the tests but then my solution gets rejected because of wrong values. Any suggestions what I can do about that?

  • Meiruv Avatar

    This comment has been hidden.

  • ArtificialCyst Avatar

    Every value rounded to 2 decimals and still not passing the test. What's going on? :(

  • nathanumb Avatar

    This comment has been hidden.

  • ResidualWolf Avatar

    Submitted a query, it didn't pass; waited 10 minutes, resubmitted and it worked. wtf?

  • kendyhiga Avatar

    This comment has been hidden.

  • StephenMayeux Avatar

    Why wouldn't the TRUNC() function work?

  • Seemen Avatar

    Something weird with rounding, some attempts fail and some attempts pass. Looks like test cases use bank rounding

  • luca118 Avatar

    This comment has been hidden.

  • fgengineer Avatar

    This comment has been hidden.

  • bromero Avatar

    This comment has been hidden.

  • zer0gravty Avatar

    Great Kata; took me forever to figure out. Bad news is it took me like three hours, as I know SQLite and not POSTgreSQL. Good news is I learned a lot about the differences (I think.)

  • sergio.nader Avatar

    Am I missing something or the salaries are a percetage of something? When I select * from job, I get salaries like 0.253123333333333333E2, which does not look like a salary at all.

  • ckevinhill Avatar

    This comment has been hidden.

  • sql-consumer Avatar

    There are a ton of solutions on here that are exposed to divide by zero errors. Code stability must be a measurable variable in terms of correct results.

  • wenima Avatar

    This comment has been hidden.

  • wenima Avatar

    Not sure if others have commented on this already (looks like they didn't from scanning the discourse) but it would be helpful if the kata stated that it was ok to round numbers. I thought about using round but thought it wasn't correct and ended up using an ugly double conversion.

    Otherwise, I enjoy Timmy's mishaps :)

  • user1464245 Avatar

    This comment has been hidden.

  • zardoxnet Avatar

    This comment has been hidden.

  • biskinis Avatar

    This comment has been hidden.

  • biskinis Avatar

    The phrases "total average salary" and "total people" are confusing, if not wrong. I suggest rephrasing as "average salary" and "applicant count".

  • staticor Avatar

    very puzzled at casting, somewhat

  • lightcast Avatar

    This comment has been hidden.

  • peteredworthy Avatar

    The test based on diff'ing the output can fail if there are multiple jobs with identical salarys

  • mmalkavian Avatar

    you should specify the descending order in the description

  • OwenCR Avatar

    This comment has been hidden.

  • pmatseykanets Avatar

    It looks like in this particular case JOIN isn't necessary at all. Therefore either the assignment should be changed to justify the JOIN or Kata should be renamed.

  • jtmp2r Avatar

    This comment has been hidden.

  • dinglemouse Avatar

    This comment has been hidden.