SQL 기본 치트 시트

SQL(Structured Query Language)은 관계형 데이터베이스를 관리하고 그 안에 있는 데이터에 대한 작업을 수행하는 데 사용되는 프로그래밍 언어입니다.

샘플 데이터





1. 간단한 쿼리




-- Fetch / retrieve all columns from a table.
SELECT *
FROM table_name;

-- Fetch columns from the table and order the results.
SELECT col1, col2
FROM table_name
ORDER BY col1 DESC;


별칭




-- COLUMNS
SELECT col1 AS col_11
FROM table_name;

-- TABLES
SELECT col1, col1_1, col1_2
FROM table_name AS t1
JOIN name_table AS t2
ON t1.col1 = t2.col1_1;


2. 출력 필터링



비교




SELECT *
FROM table_name
WHERE col1 BETWEEN range1 AND range2; 

-- aggregate functions
SELECT *
FROM table_name
WHERE col1 > 3;


텍스트 연산자




-- List with Names starting with Mar
SELECT * 
FROM employees
WHERE first_name LIKE ('Mar%');

-- list with NAMES ENDING WITH 'rk'
SELECT * 
FROM employees 
WHERE first_name LIKE ('%rk');

-- Others
SELECT  * 
FROM employees
WHERE first_name
NOT IN ('John' , 'Mark', 'Jacob'); 


3. 여러 테이블 쿼리




-- CROSS JOIN 
-- Connecting two tables
SELECT
    dm.*, d.*
FROM
    dept_manager dm
     CROSS JOIN
    departments d
ORDER BY dm.emp_no, d.dept_no;  

-- INNER JOIN 
-- Null values are also not displayed
SELECT m.dept_no, m.emp_no, d.dept_name
FROM dept_manager_dup m
INNER JOIN
departments_dup d on m.dept_no = d.dept_no
GROUP BY m.emp_no
ORDER BY m.dept_no;

-- LEFT JOIN (interchangeable with LEFT OUTER JOIN)
-- All matching values of the two tables + all values from the left table that matches no values
SELECT m.dept_no, m.emp_no, d.dept_name
FROM dept_manager_dup m
    LEFT JOIN departments_dup d ON m.dept_no = d.dept_no
GROUP BY m.emp_no
ORDER BY m.dept_no;


-- RIGHT JOIN
-- Identical to Left JOIN, with the only difference being that the direction of the operation is inverted. 
SELECT m.dept_no, m.emp_no, m.dept_name
FROM dept_manager_dup m,
     departments_dup d
WHERE m.dept_no = d.dept_no
ORDER BY m.dept_no DESC;

-- SQL Self Join
-- Applied when a table must join itself (Combining rows OF A TABLE with other rows of same table)
-- From the emp_manager table, extract the record data only of those employees who are managers as well.
SELECT *
FROM emp_manager em
ORDER BY em.emp_no;
SELECT em1.*
FROM emp_manager em1
    JOIN emp_manager em2 ON em1.emp_no = em2.manager_no;


4. 하위 쿼리



IN 중첩 내부 WHERE 쿼리 내부 쿼리가 있는 SQL 하위 쿼리
내부 쿼리/중첩 쿼리/내부 선택/외부 선택이라고도 합니다.

SELECT * FROM dept_manager;

SELECT e.first_name, e.last_name
FROM employees e
WHERE e.emp_no IN (
        SELECT dmd.emp_no
        FROM dept_manager_dup dmd
    );


5. 설정 작업



집합 연산은 둘 이상의 쿼리 결과를 단일 결과로 결합하는 데 사용됩니다. 결합된 쿼리는 동일한 수의 열과 호환 가능한 데이터 유형을 반환해야 합니다. 해당 열의 이름은 다를 수 있습니다.

-- UNION
 SELECT
    e.emp_no,  e.first_name, e.last_name,
    NULL AS dept_no, NULL AS from_date
FROM employees_dup e
WHERE e.emp_no = 10001
UNION  -- difference
    SELECT 
        NULL AS emp_no, NULL AS first_name,
        NULL AS last_name, dmd.dept_no,
        dmd.from_date
FROM dept_manager_dup dmd;

-- UNION ALL
 SELECT
    e.emp_no,  e.first_name, e.last_name,
    NULL AS dept_no, NULL AS from_date
FROM employees_dup e
WHERE e.emp_no = 10001
UNION ALL -- difference
    SELECT 
        NULL AS emp_no, NULL AS first_name,
        NULL AS last_name, dmd.dept_no,
        dmd.from_date
FROM dept_manager_dup dmd;


참고: 데이터 분석에 중점을 둔 SQL 초보자를 위한 자습서MySQL for Data Analytics and Business Intelligence를 적극 권장합니다.

좋은 웹페이지 즐겨찾기