π νμ΅ λͺ©ν
λ°μ΄ν° μ‘°ν(SELECT)λ λ°μ΄ν° μ‘°μμ΄(DML)μ΄λ©°, λ°μ΄ν° λΆμμμ κ°μ₯ λ§μ΄ μ¬μ©λλ λͺ
λ Ήμ΄μ
λλ€.
λ°μ΄ν° μ‘°ν(SELECT)λ μ¬λ¬ μ λ€κ³Ό ν¨κ» μ¬μ©λμ΄, λΆμμ νμν λ°μ΄ν°λ₯Ό μ‘°νν©λλ€.
SELECT
μ νμ΄λΌλ λ¨μ΄μ²λΌ λ°μ΄ν°μ μ ν¬ν¨λ νΉμ±λ€μ νΉμ ν©λλ€.
SELECT 'hello world';
SELECT 2;
SELECT 15 + 3;
FROM
ν μ΄λΈκ³Ό κ΄λ ¨μ΄ μλ κ²½μ° νμλ‘ λͺ μν΄μΌ νλ λͺ λ Ήμ΄ μ λλ€. κ²°κ³Όλ€μ λμΆν΄λΌ λ°μ΄ν°λ² μ΄μ€ ν μ΄λΈμ λͺ μν©λλ€.
SELECT νΉμ±_1
FROM ν
μ΄λΈ_μ΄λ¦;
-- μμ
SELECT FirstName
FROM customers;
νΉμ νΉμ±λ€μ ν
μ΄λΈμμ μ¬μ©
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦;
-- μμ
SELECT customers.FirstName, customers.LastName
FROM customers;
ν
μ΄λΈμ λͺ¨λ νΉμ±μ μ ν
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦;
-- μμ
SELECT *
FROM customers;
WHERE
(μ νμ μΌλ‘) νν° μν μ νλ 쿼리문μ
λλ€.
νΉμ κ°κ³Ό λμΌν λ°μ΄ν° μ°ΎκΈ°
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±_1 = "νΉμ κ°";
-- μμ
SELECT customers.FirstName, customers.LastName
FROM customers
WHERE customers.FirstName = 'Helena';
νΉμ κ°μ μ μΈν λ°μ΄ν° μ°ΎκΈ°
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±_2 <> "νΉμ κ°";
-- μμ
SELECT customers.FirstName, customers.LastName
FROM customers
WHERE customers.FirstName <> 'Helena';
νΉμ κ°λ³΄λ€ ν¬κ±°λ μμ λ°μ΄ν°λ₯Ό νν°ν λμλ '<', '>', λΉκ΅νλ κ°μ ν¬ν¨νλ 'μ΄μ', 'μ΄ν' κ°μ '<=', '>=' μ μ¬μ©ν©λλ€.
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±_1 > "νΉμ κ°";
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±1 <= "νΉμ κ°";
λ¬Έμμ΄μμ νΉμ κ°κ³Ό λΉμ·ν κ°λ€μ νν°ν λμλ LIKE μ '%' νΉμ '*' λ₯Ό μ¬μ©ν©λλ€.
SELECT νΉμ±_1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±_2 LIKE "%νΉμ λ¬Έμμ΄%";
-- μμ
SELECT customers.FirstName, customers.LastName
FROM customers
WHERE customers.LastName LIKE '%han%';
리μ€νΈμ κ°λ€κ³Ό μΌμΉνλ λ°μ΄ν°λ₯Ό νν°ν λμλ IN μ μ¬μ©ν©λλ€.
SELECT νΉμ±1, νΉμ±_2
FROM ν
μ΄λΈμ΄λ¦
WHERE νΉμ±_2 IN ("νΉμ κ°_1", "νΉμ κ°_2");
-- μμ
SELECT customers.FirstName, customers.LastName
FROM customers
WHERE customers.LastName IN ('Hansen', 'Johansson');
κ°μ΄ μλ NULL κ³Ό κ°μ κ²½μ°λ₯Ό μ°Ύμ λμλ IS λ₯Ό μ¬μ©ν©λλ€.
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦
WHERE νΉμ±_1 IS NULL;
-- μμ
SELECT *
FROM employees
WHERE employees.ReportsTo is NULL;
NULL μ μ΄λ€ κ²½μ°μ μ¬μ©μ΄ λλμ?
κ°μ΄ μλ κ²½μ°λ₯Ό μ μΈν λμλ NOT μ μΆκ°ν΄ μ΄μ©ν©λλ€.
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦
WHERE νΉμ±_1 IS NOT NULL;
ORDER BY
λλ €λ°λ λ°μ΄ν° κ²°κ³Όλ₯Ό μ΄λ»κ² μ λ ¬νμ§μ λν μ νμ νλͺ©μ
λλ€.
κΈ°λ³Έ μ λ ¬μ μ€λ¦μ°¨μμ λλ€.
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦
ORDER BY νΉμ±_1;
-- μμ
SELECT *
FROM employees
ORDER BY employees.EmployeeId;
λ΄λ¦Όμ°¨μμΌλ‘λ μ λ ¬ν μ μμ΅λλ€.
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦
ORDER BY νΉμ±_1 DESC;
-- μμ
SELECT *
FROM employees
ORDER BY employees.EmployeeId DESC;
LIMIT
λλ €λ°λ λ°μ΄ν° κ²°κ³Ό κ°―μλ₯Ό μ ν μ μμ΅λλ€. 쿼리문μμ μ¬μ©μ ν λμλ λ§μ§λ§μ μΆκ°ν΄μΌ ν©λλ€.
λ°μ΄ν° κ²°κ³Ό κ°―μλ₯Ό 200κ° νμ μΌλ‘ μ ν μ μμ΅λλ€.
SELECT *
FROM ν
μ΄λΈ_μ΄λ¦
LIMIT 200;
-- μμ
SELECT *
FROM customers
LIMIT 10;
DISTINCT
μ λν¬ν κ°λ€μ λ°κ³ μΆμ λμλ SELECT λ€μ λΆμ¬ μ¬μ©ν μ μμ΅λλ€.
νΉμ±_1 κΈ°μ€μΌλ‘ μ λν¬ν κ°λ€λ§ μ νν©λλ€.
SELECT DISTINCT νΉμ±1
FROM ν
μ΄λΈμ΄λ¦;
-- μμ
SELECT DISTINCT employees.Title
FROM employees;
νΉμ±_1, νΉμ±_2, νΉμ±_3 μ μ λν¬ν 'μ‘°ν©' κ°λ€μ μ νν©λλ€.
SELECT
DISTINCT
νΉμ±1
,νΉμ±_2
,νΉμ±_3
FROM ν
μ΄λΈμ΄λ¦;
INNER JOIN
μλ‘ κ³΅ν΅λ λΆλΆμΌλ‘λ§ μ°κ²°ν©λλ€.
INNER JOIN μ΄λ JOIN μΌλ‘ μ€νν μ μμ΅λλ€.
SELECT *
FROM ν
μ΄λΈ_1
JOIN ν
μ΄λΈ_2 ON ν
μ΄λΈ_1.νΉμ±_A = ν
μ΄λΈ_2.νΉμ±_B;
-- μμ
SELECT
c.FirstName || ' ' || c.LastName AS 'Customer Name',
e.Firstname || ' ' || e.LastName AS 'Employee Name'
FROM customers AS c
JOIN employees AS e ON c.SupportRepId = e.EmployeeId;
OUTER JOIN
Outer JOIN μ λ€μν μ νμ§κ° μμ΅λλ€.
μ ν¬λ κΈ°λ³Έμ μΈ 'left inclusive', 'right inclusive', 'full outer join' μ λ³Ό κ²μ λλ€.
LEFT INCLUSIVE μ LEFT OUTER JOIN μΌλ‘ μ§νμ ν μ μμ΅λλ€.
SELECT *
FROM ν
μ΄λΈ_1
LEFT OUTER JOIN ν
μ΄λΈ_2 ON ν
μ΄λΈ_1.νΉμ±_A = ν
μ΄λΈ_2.νΉμ±_B
RIGHT INCLUSIVE λ λΉμ·νκ² RIGHT OUTER JOIN μΌλ‘ μ§νν μ μμ΅λλ€.
SELECT *
FROM ν
μ΄λΈ_1
RIGHT OUTER JOIN ν
μ΄λΈ_2 ON ν
μ΄λΈ_1.νΉμ±_A = ν
μ΄λΈ_2.νΉμ±_B
sqlite μμλ RIGHT OUTER JOIN / FULL OUTER JOIN μ μ§μνμ§ μκΈ° λλ¬Έμ μμλ₯Ό λ°κΎΈμ΄ LEFT JOIN μ μ΄μ©νλ λ°©λ²μ μ¬μ©ν©λλ€.
μ¬λ¬ κ° μ¨λ³΄κΈ°
μ΄μ κ° λͺ
λ Ήμ΄λ₯Ό ν΅ν©ν΄μ μ€νν΄λ³΄λ μ°μ΅μ νκ² μ΅λλ€.
λ€μμ 'Brazil' μμ μ¨ κ³ κ°λ€μ λμλ³λ‘ λ¬Άμ λ€μ κ° λμ μμ λ°λΌ λ΄λ¦μ°¨μ μ λ ¬ ν CustomerId μ λ°λΌ μ€λ¦μ°¨μμΌλ‘ μ λ ¬ν κ²°κ³Όμ 3κ°λ§ λ°λ μμμ λλ€.
νμ§λ§ κΌ νλμ λͺ λ Ήμ΄λ§ μ λ΅μ΄ μλμλ μμ΅λλ€. μλ SQL 쿼리문μ μ΄λ»κ² λ€λ₯΄κ² μΈ μ μμκΉμ?
SELECT c.CustomerId, c.FirstName, count(c.City) as 'City Count'
FROM customers AS c
JOIN employees AS e ON c.SupportRepId = e.EmployeeId
WHERE c.Country = 'Brazil'
GROUP BY c.City
ORDER BY 3 DESC, c.CustomerId ASC
LIMIT 3;
[reference]