본문 바로가기

Algorithm/MySQL

(75)
[해커랭크(HackerRank)] Symmetric Pairs (MySQL) 문제 ou are given a table, Functions, containing two columns: X and Y. Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1. Write a query to output all such symmetric pairs in ascending order by the value of X. List the rows such that X1 ≤ Y1. 코드 # X와 Y가 같지 않은 것과 같은 것을 UNION으로 묶어서 보여주기 # X와 Y가 같지 않을 때 SELECT A.X, A.Y FROM Functions A INNER JOIN Functions B ON A.X ..
[LeetCode] 197. Rising Temperature (MySQL) 문제 코드 SELECT today.id as id FROM Weather as today INNER JOIN Weather as yesterday ON yesterday.recordDate = date_add(today.recordDate, interval -1 day) WHERE yesterday.temperature < today.temperature 이전 날짜(어제)에 비해 온도가 높은 모든 날짜의 ID 찾기 먼저 today 테이블과 yesterday테이블의 조인 조건을 찾는 게 관건 순서가 any order라서 join 조건을 날짜로 줘야한다 오늘의 온도가 어제보다 더 높은 데이터만 출력 어제의 날짜는 today.recordDate에서 하루를 뺀 값 출처
[LeetCode] 181. Employees Earning More Than Their Managers (MySQL) 문제 코드 SELECT a.name as Employee FROM Employee as a /*사원 테이블*/ INNER JOIN Employee as b /*매니저 테이블*/ ON a.managerId = b.id WHERE a.salary > b.salary /* ["employee_name", "manager_name"], ["Joe", "Sam"] ["Henry", "Max"] */ employee 테이블에는 모든 직원과 매니저들의 데이터를 가지고 있다 이 사람들 중에서 매니저보다 더 많이 버는 사람을 찾아라 a.managerId와 b의 id를 조인 조건으로 주면서 a는 사원 테이블이 되고 b는 매니저 테이블로 만든다 사원의 월급이 매니저보다 많은 데이터만 출력 출처
[LeetCode] 183. Customers Who Never Order (MySQL) 문제 Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything. 코드 SELECT Customers.Name as Customers FROM Customers LEFT JOIN Orders ON Customers.Id = Orders.CustomerId WHERE orders.Id is null 아무것도 주문하지 않은 모든 고객을 찾아라 그렇다면 Customer테이블에는 데이터가 있지만 Orders테이블에는 데이터가 없을 것이다 그렇기 때문에 데이터가 있는 Customers를 기준으로 LEFT ..