inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] The Blunder

    silver's avatar
    silver
    Apr 22, 2025
    [SQL문제풀기] The Blunder
    Contents
    문제MySQL Oracle MS SQL ServerDB2정리!

    문제

    The Blunder | HackerRank
    Query the amount of error in Sam's result, rounded up to the next integer.
    The Blunder | HackerRank
    https://www.hackerrank.com/challenges/the-blunder/problem?isFullScreen=true
    The Blunder | HackerRank

    MySQL

    내가 작성한 정답

    → ceil - 올림, cast ( as unsigned ) - 부호 없는 정수 (*char 문자로, date 날짜, decimal 소수), replace - 대체
    select ceil(avg(salary) - cast(avg(replace(salary,'0','')) as unsigned)) from employees;

    Oracle

    내가 작성한 정답

    → ceil, to_number, replace
    select ceil(avg(salary) - to_number(avg(replace(salary,'0','')))) from employees;

    MS SQL Server

    내가 작성한 오답

    : 결과값이 다르다. 2253이 출력되어야 한다.
    select ceiling(avg(salary) - avg(cast(replace(salary,'0','') as decimal))) from employees;
    notion image

    내가 작성한 정답

    : salary자체도 decimal(소수점 나오는 수)로 변형한 후 평균을 구해 올림해야 한다.
    → ceiling, cast( as decimal), replace
    select ceiling(avg(cast(salary as decimal)) - avg(cast(replace(salary,'0','') as decimal))) from employees;

    DB2

    select avg(salary)-avg(replace(salary,'0','')) from employees;
    select avg(cast(salary as decimal))-avg(replace(salary,'0','')) from employees;
    notion image
    notion image

    내가 작성한 정답

    → ceiling, cast( as decimal), replace
    select ceiling(avg(cast(salary as decimal))-avg(replace(salary,'0',''))) from employees;

    정리!

    💡
    숫자: SIGNED (부호 있는 정수), UNSIGNED (부호 없는 정수), DECIMAL(총자릿수, 소수점자릿수), FLOAT, DOUBLE문자열: CHAR, VARCHAR, BINARY, VARBINARY날짜/시간: DATE, DATETIME, TIME기타: JSON 등
    • 숫자: SIGNED (부호 있는 정수), UNSIGNED (부호 없는 정수), DECIMAL(총자릿수, 소수점자릿수), FLOAT, DOUBLE
    • 문자열: CHAR, VARCHAR, BINARY, VARBINARY
    • 날짜/시간: DATE, DATETIME, TIME
    • 기타: JSON 등
    Share article

    silver

    RSS·Powered by Inblog