SQL(26)
-
DML
1. INSERT - 모든 컬럼에 값을 입력할 때 INSERT INTO 테이블명 VALUES (value_list) ; - 일부 컬럼에만 값을 입력할 때 INSERT INTO 테이블명 (column_list) VALUES (value_list); 2. UPDATE - 기본 구조 UPDATE 테이블명 SET 컬럼=값 ; - 조건이 있을 때 UPDATE 테이블명 SET 컬럼=값 WHERE 조건식 ; 3. DELETE - 기본구조 DELETE FROM 테이블명; - 조건이 있을 때 DELETE FROM 테이블명 WHERE 조건절 ;
2022.10.28 -
[leetcode] 196. Delete Duplicate Emails
DELETE 1. 기본 구문 DELETE FROM 테이블명 ; 2. 조건이 있을때 DELETE FROM 테이블명 WHERE 조건절 ; 리트코드 정답 쿼리 1. 서브쿼리 사용 DELETE FROM person WHERE id NOT IN (SELECT sub.min_id FROM (SELECT email, MIN(id) AS min_id FROM person GROUP BY email )sub ) 2. 셀프 조인 사용 DELETE p1 FROM person AS p1 INNER JOIN person AS p2 ON p1.email = p2.email WHERE p1.id > p2.id
2022.10.25 -
[LeetCode] 627. Swap Salary
UPDATE문에 CASE WHEN 사용하기 UPDATE의 기본 구조 1. 조건 없이 모든 컬럼의 값을 변경할때 UPDATE 테이블명 SET 컬럼=값 ; 2. 조건에 맞는 컬럼의 값을 변경할때 UPDATE 테이블명 SET 컬럼=값 WHERE 조건식 ; 나의 작성 코드 UPDATE salary set sex = CASE WHEN MOD(id,2)=0 THEN 'm' WHEN MOD(id,2)!=0 THEN 'f' END ; 인프런 백문이불여일타의 정답 UPDATE salary set sex = CASE WHEN sex='f' THEN 'm' ELSE 'f' END ; 주요 구문 UPDATE 테이블명 SET = CASE WHEN 구문
2022.10.25 -
[LeetCode] 1965. Employees With Missing Information
SELECT employee_id FROM (SELECT e.employee_id FROM employees AS e LEFT JOIN salaries AS s ON e.employee_id = s.employee_id WHERE s.salary IS NULL UNION SELECT s.employee_id FROM employees AS e RIGHT JOIN salaries AS s ON e.employee_id = s.employee_id WHERE e.name IS NULL)sub ORDER BY 1 OUTER JOIN : FULL OUTER JOIN , LEFT OUTER JOIN, RIGHT OUTER JOIN FULL OUTER JOIN = LEFT JOIN UNION RIGHT JOIN U..
2022.06.16 -
[LeetCode] 626. Exchange Seats
SELECT new_id AS id ,student FROM (SELECT CASE WHEN MOD(id,2)=1 AND id=(SELECT MAX(id) FROM seat) THEN id WHEN MOD(id,2)=1 THEN (id+1) WHEN MOD(id,2)=0 THEN (id-1) END AS new_id, student FROM seat) AS sub ORDER BY new_id;
2022.06.09 -
[LeetCode] 180. Consecutive Numbers
SELECT id AS ConsecutiveNums FROM ( SELECT id, num, LEAD(num,1) OVER (ORDER BY id) AS two_num, LEAD(num,2) OVER (ORDER BY id) AS three_num FROM Logs) sub WHERE num=two_num AND two_num=three_num
2022.06.09