코딩테스트 연습 - 없어진 기록 찾기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 없어진 기록 찾기

ANIMAL_INS 테이블은 동물 보호소에 들어온 동물의 정보를 담은 테이블입니다. ANIMAL_INS 테이블 구조는 다음과 같으며, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE는 각각 동물의 아이디

programmers.co.kr

천재지변으로 인해 일부 데이터가 유실되었습니다. 입양을 간 기록은 있는데, 보호소에 들어온 기록이 없는 동물의 ID와 이름을 ID 순으로 조회하는 SQL문을 작성해주세요.

 


JOIN을 얼마나 잘 활용하느냐를 묻는 문제이다. 

ANIMAL_INS에는 없고, ANIMAL_OUTS에만 있는 데이터를 찾아달라는 것이다. 
따라서, 1번으로 join을 해주는데, inner가 아닌 outer조인을 해줘야 한다. 
그럼 어떻게 붙일까. 
난 ANIMAL_INS를 Select 문의 from에 썼으므로, right join으로 ANIMAL_OUTS를 붙였다. 
그리고 id가 같은녀석들로 붙였으며, 마지막에 where문을 사용하여, ANIMAL_INS.ANIMAL_ID IS NULL 을 조건으로 걸면 
outs 테이블에만 있는 녀석들을 찾을 수 있다!

SELECT ao.ANIMAL_ID, ao.NAME FROM ANIMAL_INS ai
right join ANIMAL_OUTS ao
on ao.ANIMAL_ID = ai.ANIMAL_ID
where ai.ANIMAL_ID is null

'SQL > SQL 공부' 카테고리의 다른 글

SQL Join에 대하여  (0) 2021.05.28
기초 문법  (0) 2021.05.28

SQL의 꽃 JOIN이다.
4가지의 Join이 있으며, 이에대하여 정리해보자!

(INNER) JOIN

Returns records that have matching values in both table.
두개의 테이블에 모두 있는 값을 매칭한다!

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
INNER JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

## 예제

위의 테이블에서 Employee Table의 EmpID와 Project를 연결해서 , 해당 인물의 프로젝트를 출력하자

SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Projects.ProjectID, Projects.ProjectName
FROM Employee
INNER JOIN Projects ON Employee.EmpID=Projects.EmpID;

FULL (OUTER) JOIN

Returns all records when there is a match in either left or right table
양쪽을 서로 붙이는 것으로, 서로 없는것들에 대해 NULL로 채우면서 JOIN하는것.

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
FULL JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Employee 테이블과 Projects 테이블을 EmpID를 기준으로 FULL JOIN하였다.
그 결과 EMP에 없는 내용이지만, Project에 기록이 있을경우 EmpFname에 Null이 입력되고, 반대의 경우도 똑같이 다 연결되었다

LEFT JOIN

Left join returns all records from the left table (table1), and matched recored from the right table.
The recult is NULL from the right side, if there is no match.
왼쪽의 있는 테이블을 기준으로, 오른쪽 테이블을 붙이며, 오른쪽 테이블 중 매칭되지 않는 데이터는 NULL로 매핑된다.
인덱스 개수 : 왼쪽 테이블 행 수 만큼

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
LEFT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

왼쪽테이블 (Employee)을 기준으로 오른쪽 테이블 (Projects)를 붙였다. 그 결과, 왼쪽의 모든것은 다 따라왔으나, 그 데이터 중 오른쪽에 없을경우 Project테이블 내용이 NULL로 매핑되었다

RIGHT JOIN

Returns all records form the right table, and the matched records from the left table
오른쪽 테이블 (join하는 테이블)을 기준으로 붙임 -> 인덱스 개수 : 오른쪽 테이블 행 수 만큼

Select * from Orders o
RIGHT JOIN Customers c
ON o.CustomerID = c.CustomerID;

SELF JOIN

A Self join is a regular join, but the table is joined with itself.
스스로를 조인하는것으로, 활용하는 방법에 대해 조금 난이도있다고 생각된다.
가장 대표적인 케이스로, 어떠한 인적정보 테이블에서, 자신과 같은 도시에 사는 다른사람 쌍을 구해보는 쿼리를 짠다고 생각하면

SELECT e1.FirstName, e2.FirstName, e1.city
FROM employees e1, employees e2
where e1.EmployeeID <> e2.EmployeeID and e1.City = e2.City

where e1.EmployeeId <> e2.EmployeeId 의 부분은 자기 스스로와 매핑되지 않도록 하기위함이고,

and e1.city=e2.City 이 부분은 city가 같은 다른사람을 조인하기 위한 부분이다.

'SQL > SQL 공부' 카테고리의 다른 글

Programmers SQL 없어진 기록찾기  (0) 2021.06.05
기초 문법  (0) 2021.05.28

FROM

~~ FROM Table 어떤 테이블로부터 가져온다는 뜻

SELECT

말 그대로 선택 →

select * 하게되면 전부 가져오는것

SELECT column1, column2, ...
FROM table_name;

위와 같은 방식으로 작성하여 사용

DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values.

유일한 값으로 가져온다는 뜻으로, 중복 없이 가져오게된다.

SELECT DISTINCT column1, column2, ...
FROM table_name;

Example

-- Write a statement that will select the City column from the Customers table.
SELECT City FROM Customers;

-- Select all the different values from the Country column in the Customers table.
SELECT DISTINCT Country FROM Customers;

WHERE

필터링 하는 것 . 원하는 조건을 걸 수 있다.

The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified condition.

LIKE or NOT LIKE

특정 문자 또는 문자열을 포함하고 있는 값을 검색하고 싶을 때 사용.

% : 0개 이상의 문자열과 대치

_ : 임의의 한 개의 문자와 대치

%,_ 를 검색하고싶을땐? → @% , @_

--Genres에서 name이 B로 시작하는것 가져오기
select * from genres g
where g.Name like 'B%';

AND, OR and NOT Operators

생각대로 and , or , not 연산자를 사용하여 조건을 추가할 수 있다.

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

-- Customers 테이블에서 Country는 Germany이고, City는 Berlin이나 Munchen인 records를 골라라
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
--Select all records where the City column has the value "Berlin".
SELECT * FROM Customers
WHERE City = Berlin;

--The following SQL statement selects all the customers from the country "Mexico", in the "Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico';

--#Select all records where the City column has the value 'Berlin' and the PostalCode column has the value 12209.
Select * From Customers
Where City = 'Berlin' and PostalCode = 12209

SELECT * FROM Customers
Where 

IN, BETWEEN

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

INSERT

The INSERT INTO statement is used to insert new records in a table.

새로운 레코드(행)을 추가할 때 사용한다.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INTO의 ( ... ) 와 VALUES의 ( ... )의 개수가 당연히 같아야한다.
해당 테이블의 column을 전부 채우지 않을 경우 null이 입력된다

NULL Values

A field with a NULL value is a field with no value.

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

UPDATE

The UPDATE statement is used to modify the existing records in a table.

현재 있는 records를 업데이트한다

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Customers테이블에서, Country가 Norway인 레코드들의 City를 Oslo로 바꾼다

Set the value of the City columns to 'Oslo', but only the ones where the Country column has the value "Norway".

update Customers
set City = 'Oslo'
where Country = 'Norway';

DELETE

삭제하는것

말그대로 삭제이며, from으로 어디서 삭제할지 명시해줘야한다

DELETE FROM table_name WHERE condition;

MIN(), MAX()

최대 최소 뽑기

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

SELECT MIN(column_name)
FROM table_name
WHERE condition;

SELECT MAX(column_name)
FROM table_name
WHERE condition;

COUNT(), AVG() and SUM()

개수새기, 평균, 합계 함수

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

SELECT AVG(column_name)
FROM table_name
WHERE condition;

SELECT SUM(column_name)
FROM table_name
WHERE condition;

참조 : https://jhnyang.tistory.com/127

'SQL > SQL 공부' 카테고리의 다른 글

Programmers SQL 없어진 기록찾기  (0) 2021.06.05
SQL Join에 대하여  (0) 2021.05.28

+ Recent posts