ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ãã¨ãã¦æ´»èºããããã«ã¯ãSQLã®ç¿å¾ãå¿ é ã¹ãã«ã¨ãªã£ã¦ãã¾ãããããããSQLãã©ã®ããã«å¦ãã§ããã°ããã®ãåãããªããã¨ãã声ãããè³ã«ãã¾ãã
ãã®è¨äºã§ã¯ãSQLã®åºæ¬çãªæ¦å¿µããå®è·µçãªä½¿ãæ¹ã¾ã§ããã¼ã¿ãµã¤ã¨ã³ãã£ã¹ããç®æãæ¹ã åãã«ãå¹ççãªSQLå¦ç¿ã®éçããç´¹ä»ãã¾ãã
ç¹ã«ãWebç³»ã®å®åã§å¿ è¦ã¨ãªãSQLã®ä½¿ãæ¹ã«ç¦ç¹ãå½ã¦ãå ·ä½çãªä¾ã交ããªãã解説ãã¦ããã¾ãã
æ³å®èªè
- ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ããç®æãå¦çã»ä»æ¥ç¨®ã®ç¤¾ä¼äººã®æ¹ã§SQLã®å¦ã³æ¹ãããããªãæ¹
1.SQLã®æ¦è¦
SQLã¨ã¯ä½ãï¼
SQLã¨ã¯Structured Query Languageï¼æ§é åååãè¨èªï¼ã®ç¥ã§ããã¼ã¿ãã¼ã¹ãæä½ããããã®æ¨æºçãªããã°ã©ãã³ã°è¨èªã§ãã主ã«ãã¼ã¿ã®æ¤ç´¢ãæ´æ°ããã¼ã¿ãã¼ã¹ã®ä½æãå¤æ´ãªã©ãè¡ãããã«ä½¿ç¨ããã¾ãããªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ãæä½ããããã«ãã¡ãããã1974å¹´ã«IBMã§éçºããã¦ä»¥æ¥ããã¼ã¿ãã¼ã¹æä½ã®ä¸çæ¨æºã¨ãã¦åºã使ããã¦ãã¾ãã
ç°¡åãªä¾ããæããã¨ã大éã®æ´çãããæ å ±ï¼ä¾ï¼å³æ¸é¤¨ã®æ¬ã®ã«ã¿ãã°ï¼ããå¿ è¦ãªæ å ±ãæ¢ãåºãããï¼æ¤ç´¢ï¼ãæ°ããæ å ±ã追å ãããï¼æ¿å ¥ï¼ãæ¢åã®æ å ±ãå¤æ´ãããï¼æ´æ°ï¼ããéã«ä½¿ç¨ãããè¨èªãã¨ããã¾ãã
ãªãSQLãå¦ã¶å¿ è¦ãããã®ãï¼
SQLã¯ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ãã®å®åã§ãå¤ãç¨ãããã¦ãããç§ãå¾äºãã¦ããWebç³»ã®æ¡ä»¶ã§ã¯ä¸»ã«ä»¥ä¸ã®ç¨éã«ç¨ãããã¦ããã
- ã¦ã¼ã¶ã¼è¡ååæï¼ä¾ï¼æ°è¦ç»é²ããã¦ã¼ã¶ã¼ã1é±é以å ã«ã©ã®ãããªæ©è½ã使ç¨ããããåæï¼
-ã¢ã¯ã»ã¹ãã°è§£æï¼ä¾ï¼ç¹å®ã®æé帯ã«ããããã¼ã¸ã®æ»å¨æéã®èª¿æ»ï¼
-PVæ°ãSSæ°ã®éè¨ï¼ä¾ï¼æ°æ©è½ãªãªã¼ã¹åå¾ã§ã®å©ç¨ç¶æ³ã®æ¯è¼ï¼
-ãã¼ã±ãã£ã³ã°ãã¡ãã«ã®è¿½è·¡ï¼ä¾ï¼ååé²è¦§ããè³¼å ¥ã¾ã§ã®å°ç·åæï¼
etc
- A/Bãã¹ãã®åæçµæã®éè¨ï¼ä¾ï¼ãã¿ã³ã®è²ãé ç½®ãå¤æ´ããéã®ã³ã³ãã¼ã¸ã§ã³çã®éããåæï¼
- ãã¸ãã¹ã¡ããªã¯ã¹(LTVããªãã³ã·ã§ã³ï¼ã®ç®åºãªã©ï¼ä¾ï¼æéã¢ã¯ãã£ãã¦ã¼ã¶ã¼æ°ã®æ¨ç§»ãç¶ç¶çã®è¨ç®ï¼
ãã®ããã«ãSQLã¯ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ãã«ã¨ã£ã¦ä¸çªæåã«è¦ããã¹ãè¨èªã¨è¨ã£ã¦ãéè¨ã§ã¯ããã¾ããã
2.SQLå¦ç¿ãã¼ãããã
å¦ç¿ãã¼ããããã¯ä»¥ä¸ã®éãã§ãã
ð åºæ¬æ§æãè¦ãã
â¬ï¸
âï¸ ç·´ç¿åé¡ã解ã
â¬ï¸
ð» å®è·µã§ä½¿ã£ã¦ã¿ã
ããã§ã¯ãå®éã«ããããã®å·¥ç¨ã®åãçµã¿æ¹ãè¦ã¦ããã¾ãããï¼
åºæ¬çãªSQLææ³ã®è©³ç´°èª¬æï¼SELECT, INSERT, UPDATE, DELETEï¼
1.åºæ¬æ§æãè¦ãã
åºæ¬æ§æã¯select, insert, update, deleteã®4ã¤ã§ãã
4ã¤ã®åºæ¬çãªSQLæ§æã説æãã¾ãï¼
 -- SELECT: ãã¼ã¿ã®æ¤ç´¢
SELECT ã«ã©ã å FROM ãã¼ãã«å WHERE æ¡ä»¶;
ä¾: SELECT name, age FROM users WHERE age > 20;
-- INSERT: ãã¼ã¿ã®è¿½å
INSERT INTO ãã¼ãã«å (ã«ã©ã 1, ã«ã©ã 2) VALUES (å¤1, å¤2);
ä¾: INSERT INTO users (name, age) VALUES ('å±±ç°å¤ªé', 25);
-- UPDATE: ãã¼ã¿ã®æ´æ°
UPDATE ãã¼ãã«å SET ã«ã©ã = æ°ããå¤ WHERE æ¡ä»¶;
ä¾: UPDATE users SET age = 26 WHERE name = 'å±±ç°å¤ªé';
-- DELETE: ãã¼ã¿ã®åé¤
DELETE FROM ãã¼ãã«å WHERE æ¡ä»¶;
ä¾: DELETE FROM users WHERE name = 'å±±ç°å¤ªé';
ãããã®æ§æãåºæ¬ã¨ãªããç¹ã«SELECTæã¯å®åã§æãé »ç¹ã«ä½¿ç¨ããã¾ãã
ã¨ã¯ããã¤ããselectæã使ãæ©ä¼ãå§åçã«å¤ãã®ã§ãä»åã¯selectã¨åããã¦ç¨ããããæ§æã4ã¤ãç´¹ä»ãã¾ãï¼
以ä¸ãå®ç§ã«è¦ããã°ãSQLã使ã£ãã¯ã¨ãªã¯ã»ã¨ãã©æ¸ããã¨æãã¾ãã
- group byã¨order by
ãããã®æ§æã«ã¤ãã¦ãç°¡åãªä¾ãè¦ã¦ã¿ã¾ãããï¼
-- GROUP BY: ãã¼ã¿ãã°ã«ã¼ãåãã¦éè¨
SELECT department, COUNT(*) as 社å¡æ°
FROM employees
GROUP BY department;
-- é¨ç½²ãã¨ã®ç¤¾å¡æ°ãéè¨
-- ORDER BY: ãã¼ã¿ã並ã³æ¿ã
SELECT name, salary
FROM employees
ORDER BY salary DESC;  -- 給ä¸ã®é«ãé ã«ä¸¦ã³æ¿ã
-- GROUP BYã¨ORDER BYã®çµã¿åãã
SELECT department, AVG(salary) as å¹³å給ä¸
FROM employees
GROUP BY department
ORDER BY å¹³åçµ¦ä¸ DESC;
-- é¨ç½²ãã¨ã®å¹³å給ä¸ããé«ãé ã«è¡¨ç¤º
ãã®ããã«ãGROUP BYã¯éè¨ã®éã«ãORDER BYã¯ä¸¦ã¹æ¿ãã®éã«ãã使ç¨ããã¾ãã
- group byã¨ã»ããã§éè¨é¢æ°ãè¦ãã(count,sumãªã©ï¼
以ä¸ã«ã代表çãªéè¨é¢æ°ã®ä¾ã示ãã¾ãï¼
-- COUNT: ã¬ã³ã¼ãæ°ãã«ã¦ã³ã
SELECT department, COUNT(*) as 社å¡æ°
FROM employees
GROUP BY department;
-- é¨ç½²ãã¨ã®ç¤¾å¡æ°ãéè¨
-- SUM: åè¨ãè¨ç®
SELECT department, SUM(salary) as 給ä¸ç·é¡
FROM employees
GROUP BY department;
-- é¨ç½²ãã¨ã®çµ¦ä¸ç·é¡ãéè¨
-- AVG: å¹³åå¤ãè¨ç®
SELECT department, AVG(age) as å¹³åå¹´é½¢
FROM employees
GROUP BY department;
-- é¨ç½²ãã¨ã®å¹³åå¹´é½¢ãéè¨
-- MAX/MIN: æ大å¤ã»æå°å¤ãåå¾
SELECT department,Â
    MAX(salary) as æé«çµ¦ä¸,
    MIN(salary) as æä½çµ¦ä¸
FROM employees
GROUP BY department;
-- é¨ç½²ãã¨ã®æé«çµ¦ä¸ã¨æä½çµ¦ä¸ã表示
ãããã®éè¨é¢æ°ã¯ãGROUP BYå¥ã¨çµã¿åããããã¨ã§ãã°ã«ã¼ããã¨ã®éè¨å¤ãç°¡åã«è¨ç®ãããã¨ãã§ãã¾ãã
- (INNER) JOINã¨LEFT JOINï¼ãã¼ãã«ã®çµåã®ã¤ã¡ã¼ã¸ãã¤ããï¼
(INNER) JOINã¨LEFT JOINã¯ãè¤æ°ã®ãã¼ãã«ãçµåããããã«ä½¿ç¨ãããéè¦ãªæä½ã§ãã以ä¸ã«ä¾ã示ãã¾ãï¼
-- 社å¡ãã¼ãã«ã¨é¨ç½²ãã¼ãã«ã®çµåä¾
-- INNER JOINï¼ä¸¡æ¹ã®ãã¼ãã«ã«åå¨ãããã¼ã¿ã®ã¿åå¾ï¼
SELECT employees.name, departments.dept_name
FROM employees
JOIN departments ON employees.dept_id = departments.id;
-- é¨ç½²ã«æå±ãã¦ãã社å¡ã®ã¿è¡¨ç¤º
-- LEFT JOINï¼å·¦ãã¼ãã«ã®å
¨ãã¼ã¿ãåå¾ï¼
SELECT employees.name, departments.dept_name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.id;
-- é¨ç½²ã«æå±ãã¦ããªã社å¡ãå«ãã¦å
¨ç¤¾å¡ã表示
(INNER) JOINã¯ä¸¡æ¹ã®ãã¼ãã«ã«åå¨ãããã¼ã¿ã®ã¿ãåå¾ããã®ã«å¯¾ããLEFT JOINã¯å·¦å´ã®ãã¼ãã«ï¼ãã®ä¾ã§ã¯employeesï¼ã®å ¨ãã¼ã¿ãä¿æããã¾ã¾çµåãè¡ãã¾ããããã«ãããé¨ç½²ã«æå±ãã¦ããªã社å¡ã®ãã¼ã¿ãåå¾ãããã¨ãã§ãã¾ãã
LEFT JOINã¯ããã¹ã¿ãã¼ãã«ãå·¦å´ã«é ç½®ããç´ã¥ããããã¼ã¿ãå ã®ã¬ã³ã¼ãæ°ãç¶æããã¾ã¾çµåã§ãã¾ãã
å®åã§ã¯ã3ã¤ä»¥ä¸ã®ãã¼ãã«ãåå¨ãããã¨ãä¸è¬çã§ãããã®éã¯ããã©ã¤ããªã¼ãã¼ï¼ãã¼ãã«å ã§ã¦ãã¼ã¯ãªèå¥åã¨ãªãã«ã©ã ï¼ã®ç¹å®ã¨ããã¼ãã«éã®é¢ä¿æ§ï¼ERå³ã§è¡¨ç¾ãããï¼ãååã«ç解ããä¸ã§çµåå¦çãè¡ãå¿ è¦ãããã¾ããERå³ã®è©³ãã説æã«ã¤ãã¦ã¯ãåèæ¸ç±ãé人ã«å¦ã¶DBè¨è¨å¾¹åºæåæ¸ããã覧ãã ããã
- Withå¥
WITHå¥ã¯ãè¤éãªSQLã¯ã¨ãªãåãããããæ´çããããã«ä½¿ç¨ãããæ§æã§ããä¸æçãªååä»ãã®çµæã»ãããä½æã§ãã¾ãã以ä¸ã«ä¾ã示ãã¾ãï¼
 -- é¨ç½²ãã¨ã®å¹³å給ä¸ãè¨ç®ãããããåºã«é«çµ¦ä¸é¨ç½²ãç¹å®ãã
WITH é¨ç½²å¥å¹³åçµ¦ä¸ AS (
  SELECT department,
      AVG(salary) as avg_salary
  FROM employees
  GROUP BY department
)
SELECT department,
    avg_salary
FROM é¨ç½²å¥å¹³å給ä¸
WHERE avg_salary > 500000;
-- è¤æ°ã®WITHå¥ã使ç¨ããä¾
WITH æéå£²ä¸ AS (
  SELECT date_trunc('month', sale_date) as month,
      SUM(amount) as monthly_sales
  FROM sales
  GROUP BY month
),
å¹´éå¹³å AS (
  SELECT AVG(monthly_sales) as yearly_avg
  FROM æé売ä¸
)
SELECT month,
    monthly_sales,
    yearly_avg,
    monthly_sales - yearly_avg as difference
FROM æé売ä¸, å¹´éå¹³å
ORDER BY month;
Withå¥ã使ç¨ãããã¨ã§ãè¤éãªã¯ã¨ãªãå°ããªé¨åã«åå²ããããèªã¿ãããä¿å®ããããã³ã¼ããä½æãããã¨ãã§ãã¾ããç¹ã«ãåããµãã¯ã¨ãªãè¤æ°å使ç¨ããå ´åã«ä¾¿å©ã§ãã
SQLåèæ¸ç±ï¼
é人ã«å¦ã¶DBè¨è¨å¾¹åºæåæ¸
 https://www.amazon.co.jp/é人ã«å¦ã¶DBè¨è¨-å¾¹åºæåæ¸-åç´è ã§çµãããããªãããªãã¸-ããã¯/dp/4798124702
- ç·´ç¿åé¡ã解ã
åºæ¬çãªSQLææ³ãè¦ããããç·´ç¿åé¡ãããªã«å½¢å¼ã§ç¹°ãè¿ã解ãã¦ããã¾ãããï¼
ä¸ã®SQLåèæ¸ã§ç´¹ä»ããããªã«å½¢å¼ã®æ¼ç¿æ¸ãã³ã³ãã¹ãã«åå ãã¦ã¿ãã®ãããã§ãã
SQLåèæ¸ç±ï¼
SQLæ¸ãæ¹ããªã«
 https://www.amazon.co.jp/æ¹è¨3ç-ããããã¨æãåãããã«ãªã-SQLæ¸ãæ¹ããªã«-PRESS-plus-ebook/dp/B07JHHSFJX/ref=sr_1_3?crid=2ME9CFNR56AEQ&dib=eyJ2IjoiMSJ9.QSnYk_Gj6WWC_W6PiYLAhLxfzCGcvMP5hYc7S53jBcbB4DwJZojR1VR6WcVIAVptmEwU_fx45PHkm5nCdqCfW2Pz0nGzFpU8WRjd93bRI9F2gJl4S3Tb75W_0oMi5--72tNP6Fly3lQb9UG2ebHuv_lCKlPUKpL6xKokZ7LmVkA6my88u_7RG4EPksDiXMm5S3lGvPSP7GIshs5cmuH5o4GYdpYzVpS_DOGN7Xao7LW7-xcGeSQAiOgHI17zTALaGEPv6r7DTDrMzUy2AlnNGpsTUTAJybOZoZUksLaLtcI.27wjA4jT0hJzdDXKhpR2H1xgiwJT6lVTbXHgE8MDBLY&dib_tag=se&keywords=sqlæ¸ãæ¹ããªã«&qid=1733907538&sprefix=sqlãã%2Caps%2C152&sr=8-3
ä¸ã®æ¸ç±ã§ã¯ãSQLã®æ¸ãæ¹ãç·´ç¿åé¡ãããã¨ãã§ãã¾ããç¿ã£ããã¨ãå®çãããããã«ç¹°ãè¿ãç·´ç¿ãã¾ããã
SQLå ¥é
 https://www.amazon.co.jp/ã¹ãããªãããSQLå ¥é-第4ç-ããªã«256åä»ãï¼-ã¹ãããªãããå ¥éã·ãªã¼ãº-ä¸å±±/dp/4295018465/ref=sr_1_1_sspa?__mk_ja_JP=ã«ã¿ã«ã&crid=3TOF30XTJB5LT&dib=eyJ2IjoiMSJ9.A59kzqon0gCVaMvqURGahDvi2q5S4X6j5vfDPm4tm0UfeqQDhv-_KY3ej_VZnX4jb2nuRsphrifXrRFggS1fjsDQN-TEFvq6dHYYPccDPlSo_HjOnHYz682_7hXClJdmunBjpC9I3UfC0qi8KvkKxJgYfooc2453B1iLBDaoiBFRNQKs-B8qWslgOcc1dAVd-mkJum3NCn8hCdftvxaQc7TlSc76NTLMmvOPk8jXY1m2U-Q6p2bKy7viz53IbRUpO-kywvBS6YVT6ChwEHYBbNAqG-lNNizjZ7W7nnbz4o4.AFgqn6dVkpofup25YZLfsNTRvpQevfWb3Awoh2TqYzI&dib_tag=se&keywords=sql+å ¥é&qid=1733907778&sprefix=sqlå ¥é%2Caps%2C151&sr=8-1-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY&psc=1
ä¸ã®æ¸ç±ã§ã¯ãã¯ã©ã¦ããµã¼ãã¹ãç¨ãã¦ãç°¡åã«SQLãç·´ç¿ã§ãã¾ãã
SQLab
ç·´ç¿åé¡ãè±å¯ã§ãç°å¢æ§ç¯ä¸è¦ãªSQLç·´ç¿ãµã¼ãã¹ã§ãããã©ã¦ã¶ä¸ã§ç·´ç¿ã§ããã®ã§ãåå¿è ã§ãåãçµã¿ãããã§ãã
3.å®è·µã§ä½¿ã£ã¦ã¿ã
æå¾ã¯ä»ã¾ã§å®çããã¦ããç¥èãå®è·µã§ä½¿ã£ã¦ã¿ã¾ãããï¼
ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ãæªçµé¨ã®æ¹ã¯ã§ããã ãå®è·µã«è¿ã(ä½ã®ããã«ãã©ãããåæãããã®ãçãªãã¨ãæ¸ããã¦ãã)æ¬ãèªã¿è¾¼ããæ¸ãä¸ããã¨ãããç·´ç¿ã«ãªãã¾ãï¼
以ä¸ã®æ¬ã¯ããããã®æ¸ç±ã§ãã
SQLåèæ¸ç±ï¼
SQLã¬ã·ã
å®éã®ãã°ãã¼ã¿ã«è¿ãä»®æ³çãªãã¼ã¿ã»ããã使ã£ã¦ãåºç¤çãªãã¼ã¿å å·¥ã売ä¸ã»ã¦ã¼ã¶ã¼ç解ã®ããã®Webãã¼ã±ãã£ã³ã°ã®ããã®ã¯ã¨ãªãæ°å¤ãç´¹ä»ããã¦ãã¾ããchapter5ã®å 容ã¾ã§ä½¿ãããªããããã«ãªãã°ãå®åã®SQLã¯ã»ã¼åé¡ãªãã¨ãã£ã¦å·®ãæ¯ããªãã§ãããã
3. SQLä¸éã®ã³ãã¨ã¾ã¨ã
SQLæ大ã®ä¸éã®ã³ãã¯ç¹°ãè¿ãSQLã®ã¯ã¨ãªãæ¸ããã¨ã§ããåºæ¬æ§æãè¦ãããã¨ã¯ããã¾ã§é£ãããªãã®ã§ãè¦ããããç¹°ãè¿ãæ¸ãã¦å®çããããã¨ãå®è·µã§ã®ä½¿ç¨ãæèããã¯ã¨ãªãæ¸ãã¦ããã¾ãããï¼
æªçµé¨è ã§ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ããç®æãæ¹ã«ã¨ã£ã¦ã¯ãSQLã¨Pythonãããç¨åº¦ã§ããã¨åºç¤æè¡ã¯èº«ã«ã¤ãã¦ããã¨é¢æ¥ãªã©ã§ã¢ãã¼ã«ã§ããå¯è½æ§ãé«ãã§ãããã£ã¡ãç·´ç¿ãã¦ããã°ã確å®ã«ãã¹ã¿ã¼ã§ããã¹ãã«ãªã®ã§ãä»æ¥ããSQLåãä¸ãã¦ããã¾ãããï¼
å人çã«ã¯SQLã§ãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ã®èãæ¹ã身ã«ã¤ãã¦ããã®æ¹ãPythonãç¿çããããã®ã§ãã¾ãSQLããåãçµããã¨ãããããã§ãã
ãããªæãã§ãä»å¾ããã¼ã¿ãµã¤ã¨ã³ã¹ç³»ã®è¨äºãæ´æ°ãã¦ããããã¨æãã¾ãã®ã§ããããããé¡ããããã¾ãï¼