본문 바로가기

SQL

(13)
LeetCode로 공부하기180. Consecutive Numbers Table: Logs +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ In SQL, id is the primary key for this table. id is an autoincrement column. Find all numbers that appear at least three times consecutively. Return the result table in any order. The result format is in the following example. Example 1: Input: Logs tabl..
HackerRank로 SQL 공부하기 - The Report 1. Students 와 Grades를 between을 사용하여 조인한다. 2. 8등급보다 낮은 등급을 받은 학생들의 이름은 null 사용한다. 3. 높은 등급 순으로 정렬한다. 혹시 같은 등급이 있다면 이름을 알파벳 순서로 정렬한다. null 인 이름이 있는 것은 학년별로 내림차순한다. 만약 그들에게 같은 등급(1-7)을 받은 학생이 둘 이상 있다면, 그 학생들의 marks 를 기준으로 오름차순으로 정렬한다. 1. Students 와 Grades를 between을 사용하여 조인한다. select * from students as s join grades as g on s.marks between g.min_mark and g.max_mark 2. 8등급보다 낮은 등급을 받은 학생들의 이름은 null 사..
HackerRank로 SQL 공부하기 - Challenges 1. hacker_id, name, 각 학생별 Challenges에 문제를 만든 개수를 출력 2. 각 학생별 Challenges에 문제를 만든 개수를 기준으로 내림차순 정렬 3. 위의 조건 중 같은 개수가 있다면 hacker_id로 정렬 4. 최대값은 같은 값이 중복되도 출력 5. 각 학생별 Challenges에 문제를 만든 개수의 최대값보다 작은 개수 중 같은 값이 있는 경우 출력에서 해당 값 모두 제외 1. hacker_id, name, 각 학생별 Challenges에 문제를 만든 개수를 출력 2. 각 학생별 Challenges에 문제를 만든 개수를 기준으로 내림차순 정렬 3. 위의 조건 중 같은 개수가 있다면 hacker_id로 정렬 SELECT C.hacker_id, H.name, count(C...
데이터를 받고 파악하는 법, 시간 데이터 파악 select count(*) from prod_order po; #3431 select count(DISTINCT order_no) from prod_order po; #3215 차이가 나넵 업자인가 한방에 많이 사네 SELECT min(order_dt) as dt_start, max(order_dt) as dt_end from prod_order po ; #날자 데이터 언제부터 언제까지인지 봐봐야지. SELECT DISTINCT product_name from prod_order po ; #A만 있구먼 SELECT DISTINCT `options` from prod_order po ; SELECT min(qty), round(avg(qty),3), max(qty) from prod_order po ; ..
LeetCode 로 공부하기 184. Department Highest Salary Table: Employee +--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------+---------+ id is the primary key (column with unique values) for this table. departmentId is a foreign key (reference columns) of the ID from the Department table. Each row of this table indicates the ID, name, and sa..
LeetCode 로 공부하기 196. Delete Duplicate Emails Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key (column with unique values) for this table. Each row of this table contains an email. The emails will not contain uppercase letters. Write a solution to delete all duplicate emails, keeping only one unique email with the smalles..
LeetCode 로 공부하기 672. Swap Salary Table: Salary +-------------+----------+ | Column Name | Type | +-------------+----------+ | id | int | | name | varchar | | sex | ENUM | | salary | int | +-------------+----------+ id is the primary key (column with unique values) for this table. The sex column is ENUM (category) value of type ('m', 'f'). The table contains information about an employee. Write a solution to swap all 'f' and 'm'..
HackerRank로 SQL 공부하기 - Weather Observation Station 15 문제: STATION에서 127.2345보다 작은 가장 큰 북위(LAT_N)에 대한 Western Longitude(LONG_W)를 조회한다. 답을 소수점 이하로 반올림한다. select round(long_w, 4) from STATION where lat_n
HackerRank로 SQL 공부하기 - Top Earners 문제: 모든 직원의 최대 총 수입과 최대 총 수입을 가진 직원의 총 수를 찾아라. SELECT months * salary, COUNT(*) FROM Employee group by months * salary order by months * salary desc limit 1; 서브쿼리를 사용한 방법 SELECT MAX(months * salary), COUNT(*) FROM Employee WHERE (months * salary) = (SELECT MAX(months * salary) FROM Employee); select salary*months, count(*) from employee where salary*months = (select max(salary*months)from employe..
HackerRank로 SQL 공부하기 - The Blunder 문제 0이 제거된 급여 사용 평균과 실제 평균 월급의 차이를 찾아라. select round(avg(salary))-round(avg(REPLACE(salary, '0', ''))) from EMPLOYEES; REPLACE 함수를 처음 써보았다. REPLACE 함수는 문자열에서 특정 부분을 다른 문자열로 대체하는 데 사용된다. 일반적으로 특정 열의 데이터를 검색하고 대체하거나, 문자열에서 특정 부분을 찾아서 대체할 때 유용하다. REPLACE(원본문자열, 찾을문자열, 대체할문자열) 그렇게 하면 결과는...