ãã¼ã¿æ¦ç¥å®¤ã§ä½¿ç¨ãã¦ããSQLã®ã³ã¼ãã£ã³ã°è¦ç´ããç´¹ä»ãã¾ã
ã¯ããã«
ããã«ã¡ã¯ããã¼ã¿æ¦ç¥å®¤ã®æ£®ä¸ã§ããä»åã¯ãã¼ã¿ã使ãéã«åã£ã¦ãåãé¢ããªãSQLã«ã¤ãã¦ãã¬ãã¬ã¸ã¼ãºã®ãã¼ã¿æ¦ç¥å®¤ã§ä½æããã³éç¨ãã¦ããã³ã¼ãã£ã³ã°è¦ç´ã«ã¤ãã¦ã話ãã¾ãã ã¬ãã¬ã¸ã¼ãºã®ãã¼ã¿æ¦ç¥å®¤ã§ã¯ä»åãç´¹ä»ããè¦ç´ãçå®ãã¦ãã¾ããããã¼ã¿æ´»ç¨ã®ãã§ã¼ãºãç°å¢ã«ãã£ã¦å¤åã«å¤ããé¨åã§ãããã¾ãã®ã§ãã¨ããä¸ç¤¾ã®éç¨æ¹æ³ã¨ãã¦åèã«ãã¦ããã ããã°å¹¸ãã§ãã
ã¬ãã¬ã¸ã¼ãºã®SQLå®è¡ç°å¢ã«ã¤ãã¦
Google BigQueryãåãã¨ããã«ã©ã ããã¼ã¿ãã¼ã¹ã«ãã¼ã¿ãæ ¼ç´ããSQLãç¨ãã¦ãã¼ã¿ãæ½åºãããã¨ãå¤ãã§ãããã®ãããä»åãç´¹ä»ããSQLã³ã¼ãã£ã³ã°è¦ç´ã«ã¯ãRDBã«å¯¾ããããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ã«é¢é£ãããããªãã¨ã¯è¦ç´ã«å ¥ã£ã¦ãã¾ããã
ã³ã¼ãã£ã³ã°è¦ç´ã«ã¤ãã¦
ã³ã¼ãã£ã³ã°è¦ç´ã¯ããå¿ ãå®ã£ã¦ã»ããé ç®ãã¨ãå¯è½ã§ããã°å®ã£ã¦ã»ããé ç®ãã«åããã¦ãã¾ããã¬ãã¬ã¸ã¼ãºã§ã¯ãRDB/MAãã¼ã«/ã·ã¹ãã ãã°æ å ±ãªã©ãå¤æ°ã®ãã¼ã¿ã½ã¼ã¹ãããã¼ã¿ãåéãããã¼ã¿æ´»ç¨åºç¤ã«ã¤ã³ãã¼ããã¦ãã¾ããå ¨ã¦ãã¼ãã«æ§é ã®ãã¼ã¿ã«ãªãã¨ã¯ãããå ¨ã¦ã®ãã¼ã¿ã½ã¼ã¹ã«å¯¾ãã¦ä¸å¾ã®è¦å®ãçå®ãããã¨ã¯ç¾å®çã§ã¯ããã¾ããã ã¾ããã»ã¼ã«ã¹ããã¼ã±ã¿ã¼ã¨ãã£ããã¼ã¿æ¦ç¥å®¤ä»¥å¤ã®ã¡ã³ãã¼ãããããã¯ã¨ãªãä½æãå®è¡ããç°å¢ãªã®ã§ãããç¨åº¦ã®ä»»æé ç®ãè¨å®ãã¤ã¤ãã³ã¼ãã®å質ãä¸å®ä»¥ä¸ã«ä¿ãããããã«ãã¦ãã¾ãã è¦ç´èªä½ã¯å ¨é¨ã§20é ç®ã»ã©ããã®ã§ãããä»åã¯ããããç¹å¾´çãªé¨åãé¸æãã¦ç°¡åã«èª¬æãè¨è¼ãã¦ããã¾ãã
å¿ ãå®ã£ã¦ã»ããé ç®
ã»äºç´èªã¯å ¨ã¦å°æåã«ãã ã»äºç´èªããã«ã©ã å/ãã¼ãã«åã1ã¤ã¤ã³ãã³ããã ã»select, from, where, order by, group by, having çã¯ã¤ã³ãã³ããæãã ã»join ã¯ãã¼ãã«åã¨åãã¤ã³ãã³ãã«ãã ã»select * ã¯åºæ¬çã«ç¦æ¢ ã»æ¹è¡åå¾ã®ã«ã³ãã¯æ¬¡ã®è¡ã®å é ã«ç½®ã
å¯è½ã§ããã°å®ã£ã¦ã»ããé ç®
ã»with ã使ç¨ãã¦ããµãã¯ã¨ãªã®ä½¿ç¨ã¯é¿ãã
[å¿ é ]äºç´èªã¯å ¨ã¦å°æåã«ãã
SQLã®æ¸ãæ¹ã§æãå·®ãåºãé¨åã§ã¯ãªãã§ãããããSQLãã¼ã¯ã¼ããäºç´èªã®ã¿å¤§æåã¨ãã£ãã¹ã¿ã¤ã«ãä¸è¬çãã¨æãã¾ãã大æåã«ããã®ã¯è¦èªæ§åä¸ã主ãªçç±ã ã¨æãã¾ãããå¼ç¤¾ã®SQLå®è¡ç°å¢ã«ã¯ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããããã¾ãããã¤ã³ãã³ããã¡ããã¨æãããã¨ã§è¦èªæ§ã¯åå確ä¿ã§ãã¾ããè¦èªæ§ã«å¤§ããªå·®ããªã以ä¸ããããã大æåã«ããã®ãé¢åãªã®ã§ããã¼ã¿æ¦ç¥å®¤ã§ã¯äºç´èªå«ãã¦å ¨ã¦ãå°æåçµ±ä¸ã«ãã¦ãã¾ããå ãã¦ãããããè·ç¨®ãSQLãæ¸ãã¬ãã¬ã¸ã¼ãºã®ç°å¢ã§ã¯ããç¹å®ã®ãã¼ã¯ã¼ãã®ã¿å¤§æåãããããããããã¼ã¯ã¼ããå°æåãã®ã»ããéç¨ãããããã¨ããã®ãçç±ã¨ãã¦æãããã¾ãã
[å¿ é ]äºç´èªããã«ã©ã å/ãã¼ãã«åã1ã¤ã¤ã³ãã³ããã
[å¿ é ]select, from, where, order by, group by, having çã¯ã¤ã³ãã³ããæãã
ããã2ã¤ã¯è¦èªæ§åä¸ã®ããã«è¨å®ãã¾ãããSQLã¯éã¨ã³ã¸ãã¢ã§ãæ±ãããããå®éã«ã¬ãã¬ã¸ã¼ãºã§ã¯å¤ãã®ã»ã¼ã«ã¹ããã¼ã±ã¿ã¼ãèªèº«ã§SQLãæ¸ãã¦ãã¼ã¿ãæ½åºãã¦ãã¾ãããããã£ãç°å¢ãªãã¨ããããSQLã³ã¼ãã£ã³ã°è¦ç´çå®åã¯ãããããèªã¿ãããããã«æ¸ãã¦ãããããã¤ã³ãã³ãããã©ãã©ã§æµç¨æ§ãä½ãSQLã¨ãªã£ã¦ãã¾ãããã¤ã³ãã³ãã¯æèããã°èª°ã§ãä¿®æ£å¯è½ã§ãããã¤ã³ãã³ããæã£ã¦ãããã¨ã«ããå¯èªæ§ã¯å¤§ããåä¸ãããããã¤ã³ãã³ãã¯ããå³ãç®ã«è¦ç´ã«çãè¾¼ã¿ã¾ããã
[å¿ é ]join ã¯ãã¼ãã«åã¨åãã¤ã³ãã³ãã«ãã
join 㯠from ã«å å«ãããé¢ä¿ã ã¨èããfrom ã®æåã®ãã¼ãã«ã¨åãã¤ã³ãã³ããã¦ãã¾ããããã«ãã£ã¦ãã©ãããã©ãã¾ã§ãå¦çã«ä½¿ç¨ãã¦ãããã¼ãã«é¡ãªã®ããä¸ç®ã§ææ¡ãããã¨ãã§ãã¾ããSQLã®ãã¼ã¿ã½ã¼ã¹ã¨ãªããã¼ãã«ä¸è¦§ãä¸ç®ã§ææ¡ããããã«ããjoin 㯠from ãããã¤ã³ãã³ããããã¨ã§å¯èªæ§ãåä¸ããã¨æãã¾ãã
from all_log_data left join temp_search_log on all_log_data.userid = temp_search_log.userid and all_log_data.user_session_id = temp_search_log.user_session_id left join temp_activity_log on temp_search_log.userid = temp_activity_log.userid and temp_search_log.user_session_id = temp_activity_log.user_session_id and temp_search_log.keywords = temp_activity_log.keywords where â¦
[å¿ é ]select * ã¯ç¦æ¢
ä¸è¦ãªäºæ ãé²ãããã« select * ã®ä½¿ç¨ã¯ç¦æ¢ã«ãã¦ãã¾ããããã¯ããå®è¡ããªãã¨ã©ã®ã«ã©ã ãé¸æãã¦ããã®ããããããã³ã¼ãã¬ãã¥ã¼ãããã¥ãããã¨ããç¥ããç¥ããã®ãã¡ã«ä¸è¦ãªã«ã©ã ãåå¾ãã¦ãã¾ããã¨ããçç±ã«æãããã¾ãã å®è£ æã¯ã¤ã楽ãããã¡ã§ select * ã使ããã¡ã§ãããSQLãä»ã®ããã°ã©ãã³ã°è¨èªã¨åããèªãæéã®æ¹ãé·ããã¨ãããå¯èªæ§ãéè¦ãã¦ãã®è¦ç´ãå ¥ãã¦ãã¾ãã
[å¿ é ]æ¹è¡åå¾ã®ã«ã³ãã¯æ¬¡ã®è¡ã®å é ã«ç½®ã
Pythonã®ä¸è¬çãªã³ã¼ãã£ã³ã°è¦ç´ã§ãã PEP 8 ã«ããã¨ããã«ã³ãã¯æ«å°¾ã«ã¤ãã¾ããããã¨ãªã£ã¦ãã¾ãããSQLã¯åã«ã¤ããããã«è¦ç´ã§è¦å®ãã¦ãã¾ããSQLã«ãããselect æã¯å¦ççµæãããã¤ã®ã«ã©ã æ°ã«ãªãã®ããé常ã«å¤§äºãªæ å ±ã ã¨èããåã«ã³ãã«ãããã¨ã§ã«ã³ãã®æ°ãæ°ããã ãã§ã«ã©ã æ°ãææ¡ãããã¨ãã§ãã¾ããã¾ããcase æã windowé¢æ°ã使ç¨ããéã«ãselect å¥ã®ä¸ã§æ¹è¡ãããã¤ã³ãã³ããããããã¨æãã¾ããããã®éã«åã«ã³ãã§ã¤ã³ãã³ããæã£ã¦ããã»ããè¦èªæ§ãé«ããªãã¾ããå ãã¦ãä½æ¥ä¸ã«ã«ã©ã åãã³ããããå ´åãå¾ãã«ã³ãã ã¨select å¥ã®æå¾ã¯ã«ã³ããåé¤ããå¿ è¦ãããã®ã«å¯¾ããåã«ã³ããªãã«ã©ã åãå¤æ´ããã ãã§æ¸ã¿ã¾ãããã¼ã¿ãã¼ãä½æã¨ãã£ãæ¥åã®ãªãã§å¤ãã®æéãSQLä½æã«è²»ããå ´åãå°å³ã§ããä½æ¥ã®ãããããå ¨ç¶éãã¨æãã¾ãã
[ä»»æ]with ã使ç¨ãã¦ããµãã¯ã¨ãªã®ä½¿ç¨ã¯é¿ãã
ãµãã¯ã¨ãªã¯è¤éåããã¨èªã¿ã¥ãããªãã®ã§ãé·ããµãã¯ã¨ãªãé¿ããä¸ã§å¯èªæ§ãåä¸ãã¦ã»ããã¨æã£ã¦ãã¾ãããã ããµãã¯ã¨ãªãä»»æã®id ã®ã¿ã«çµããã¨ãã£ã使ãæ¹ãããå ´åãããããwithå¥ã使ç¨ããã»ã©ã§ããªãããªã¨æãã¾ãããã®ãããã¯ããã¨è¦ãæã«ã©ãããå¦çãªã®ããåããã°ããã®ã§ããã¾ãå³å¯ã«è¦ç´ã¨ãã¦è¦å®ããã»ã©ã§ããªãããªã¨èããä»»æé ç®ã«ãã¦ãã¾ãã
ãä¾ã
select name ,address from users where id in ( select user_id from access_log )
ãããã«
ä»åã¯ã¬ãã¬ã¸ã¼ãºã®ãã¼ã¿æ¦ç¥å®¤ã§éç¨ããã¦ããSQLã®ã³ã¼ãã£ã³ã°è¦ç´ã«ã¤ãã¦ã話ããã¾ãããSQLã¯éã¨ã³ã¸ãã¢ã§ãæ±ãããããã¨ãããå¤ãã®ä¼æ¥ã§æ§ã ãªè·ç¨®ã®æ¹ã«ä½¿ç¨ããã¦ãããã¨æãã¾ãããã®ããã人ã«ãã£ã¦æ¸ãæ¹ãç°ãªããã¨ãå¤ãã®ã§ãããç¨åº¦ç¤¾å ã§SQLãæ±ãã人ãå¢ãã¦ãã段éã§ãçµç¹ã®ç°å¢ãã¦ã¼ã¶ã¼ã®ã¬ãã«æãèæ ®ãã¦è¦ç´ãä½ã£ã¦ã¿ãã¨ããã¨æãã¾ããSQLã³ã¼ãã£ã³ã°è¦ç´ã¯ãç¾å ´ã«å°å ¥ãããéç¨ããã¦åãã¦å¹åãçºæ®ããã®ã§ãå¼ç¤¾ã®ã±ã¼ã¹ãåèã«ãªãã°å¹¸ãã§ãã
SQLã³ã¼ãã£ã³ã°è¦ç´çå®ã§åèã«ãããã®
ãªã¼ããã«ã³ã¼ã âããè¯ãã³ã¼ããæ¸ãããã®ã·ã³ãã«ã§å®è·µçãªãã¯ããã¯
PEP8