[{"data":1,"prerenderedAt":829},["ShallowReactive",2],{"guide-sql-best-practices":3},{"id":4,"title":5,"body":6,"date":818,"description":819,"extension":820,"meta":821,"navigation":74,"path":825,"readingTime":84,"seo":826,"stem":827,"__hash__":828},"guides\u002Fguides\u002Fsql-best-practices.md","SQL Best Practices: Industry Standards for Query Style",{"type":7,"value":8,"toc":792},"minimark",[9,14,18,21,25,30,33,100,103,160,164,167,216,219,248,252,255,259,266,286,290,296,336,340,343,381,385,389,392,459,463,488,492,495,515,519,522,526,579,583,626,630,633,675,679,682,731,734,738,755,759,768,772,788],[10,11,13],"h2",{"id":12},"why-sql-style-standards-matter","Why SQL Style Standards Matter",[15,16,17],"p",{},"Without a shared style guide, every developer on your team writes SQL their own way. One person uses snake_case, another uses camelCase. One aliases every table, another skips aliases entirely. Code reviews turn into nitpicks about formatting instead of focusing on logic.",[15,19,20],{},"A SQL style guide eliminates these debates. When everyone follows the same conventions, reviews move faster, onboarding takes less time, and bugs get caught earlier because the code structure is predictable.",[10,22,24],{"id":23},"naming-conventions","Naming Conventions",[26,27,29],"h3",{"id":28},"tables-and-columns","Tables and Columns",[15,31,32],{},"Use snake_case for all database identifiers. It is the most widely adopted convention across PostgreSQL, MySQL, and SQL Server.",[34,35,40],"pre",{"className":36,"code":37,"language":38,"meta":39,"style":39},"language-sql shiki shiki-themes github-light github-dark","-- Consistent\nSELECT first_name, last_name, hire_date\nFROM employees\nWHERE department_id = 5;\n\n-- Inconsistent\nSELECT firstName, lastName, hireDate\nFROM Employees\nWHERE DepartmentID = 5;\n","sql","",[41,42,43,51,57,63,69,76,82,88,94],"code",{"__ignoreMap":39},[44,45,48],"span",{"class":46,"line":47},"line",1,[44,49,50],{},"-- Consistent\n",[44,52,54],{"class":46,"line":53},2,[44,55,56],{},"SELECT first_name, last_name, hire_date\n",[44,58,60],{"class":46,"line":59},3,[44,61,62],{},"FROM employees\n",[44,64,66],{"class":46,"line":65},4,[44,67,68],{},"WHERE department_id = 5;\n",[44,70,72],{"class":46,"line":71},5,[44,73,75],{"emptyLinePlaceholder":74},true,"\n",[44,77,79],{"class":46,"line":78},6,[44,80,81],{},"-- Inconsistent\n",[44,83,85],{"class":46,"line":84},7,[44,86,87],{},"SELECT firstName, lastName, hireDate\n",[44,89,91],{"class":46,"line":90},8,[44,92,93],{},"FROM Employees\n",[44,95,97],{"class":46,"line":96},9,[44,98,99],{},"WHERE DepartmentID = 5;\n",[15,101,102],{},"Rules to follow:",[104,105,106,120,132,141],"ul",{},[107,108,109,110,113,114,113,117],"li",{},"Table names: plural, lowercase — ",[41,111,112],{},"users",", ",[41,115,116],{},"orders",[41,118,119],{},"product_categories",[107,121,122,123,113,126,113,129],{},"Column names: singular, lowercase — ",[41,124,125],{},"email",[41,127,128],{},"created_at",[41,130,131],{},"is_active",[107,133,134,135,113,138],{},"Join tables: combine both names — ",[41,136,137],{},"users_roles",[41,139,140],{},"orders_products",[107,142,143,144,113,147,150,151,154,155,113,157],{},"Boolean columns: prefix with ",[41,145,146],{},"is_",[41,148,149],{},"has_",", or ",[41,152,153],{},"can_"," — ",[41,156,131],{},[41,158,159],{},"has_subscription",[26,161,163],{"id":162},"aliases","Aliases",[15,165,166],{},"Use short, meaningful aliases. Avoid single letters unless the context is obvious.",[34,168,170],{"className":36,"code":169,"language":38,"meta":39,"style":39},"-- Good: aliases mirror table names\nFROM orders o\nJOIN customers c ON o.customer_id = c.id\nJOIN shipping_addresses sa ON o.shipping_address_id = sa.id\n\n-- Bad: cryptic aliases\nFROM orders a\nJOIN customers b ON a.foo = b.bar\nJOIN shipping_addresses x ON a.baz = x.qux\n",[41,171,172,177,182,187,192,196,201,206,211],{"__ignoreMap":39},[44,173,174],{"class":46,"line":47},[44,175,176],{},"-- Good: aliases mirror table names\n",[44,178,179],{"class":46,"line":53},[44,180,181],{},"FROM orders o\n",[44,183,184],{"class":46,"line":59},[44,185,186],{},"JOIN customers c ON o.customer_id = c.id\n",[44,188,189],{"class":46,"line":65},[44,190,191],{},"JOIN shipping_addresses sa ON o.shipping_address_id = sa.id\n",[44,193,194],{"class":46,"line":71},[44,195,75],{"emptyLinePlaceholder":74},[44,197,198],{"class":46,"line":78},[44,199,200],{},"-- Bad: cryptic aliases\n",[44,202,203],{"class":46,"line":84},[44,204,205],{},"FROM orders a\n",[44,207,208],{"class":46,"line":90},[44,209,210],{},"JOIN customers b ON a.foo = b.bar\n",[44,212,213],{"class":46,"line":96},[44,214,215],{},"JOIN shipping_addresses x ON a.baz = x.qux\n",[15,217,218],{},"Alias rules:",[104,220,221,232,239,242],{},[107,222,223,224,227,228,231],{},"One or two characters for common tables — ",[41,225,226],{},"u"," for users, ",[41,229,230],{},"o"," for orders",[107,233,234,235,238],{},"Abbreviations for longer names — ",[41,236,237],{},"sa"," for shipping_addresses",[107,240,241],{},"Never reuse the same alias for different tables in one query",[107,243,244,245],{},"Always alias computed columns — ",[41,246,247],{},"SUM(amount) AS total_amount",[10,249,251],{"id":250},"comment-style","Comment Style",[15,253,254],{},"Comments explain why, not what. The SQL itself should explain what it does through clear formatting and naming.",[26,256,258],{"id":257},"inline-comments","Inline Comments",[15,260,261,262,265],{},"Use ",[41,263,264],{},"--"," for single-line explanations placed above the line they describe.",[34,267,269],{"className":36,"code":268,"language":38,"meta":39,"style":39},"-- Exclude trial accounts from revenue calculations\nWHERE account_type != 'trial'\n    AND created_at \u003C CURRENT_DATE\n",[41,270,271,276,281],{"__ignoreMap":39},[44,272,273],{"class":46,"line":47},[44,274,275],{},"-- Exclude trial accounts from revenue calculations\n",[44,277,278],{"class":46,"line":53},[44,279,280],{},"WHERE account_type != 'trial'\n",[44,282,283],{"class":46,"line":59},[44,284,285],{},"    AND created_at \u003C CURRENT_DATE\n",[26,287,289],{"id":288},"block-comments","Block Comments",[15,291,261,292,295],{},[41,293,294],{},"\u002F* *\u002F"," for multi-line explanations, especially for complex logic or business rules.",[34,297,299],{"className":36,"code":298,"language":38,"meta":39,"style":39},"\u002F*\n * Revenue is calculated using the cash-basis method.\n * Only completed payments from the current fiscal year\n * are included per finance team policy (RFC-2024-017).\n *\u002F\nWHERE payment_status = 'completed'\n    AND payment_date >= '2024-07-01'\n",[41,300,301,306,311,316,321,326,331],{"__ignoreMap":39},[44,302,303],{"class":46,"line":47},[44,304,305],{},"\u002F*\n",[44,307,308],{"class":46,"line":53},[44,309,310],{}," * Revenue is calculated using the cash-basis method.\n",[44,312,313],{"class":46,"line":59},[44,314,315],{}," * Only completed payments from the current fiscal year\n",[44,317,318],{"class":46,"line":65},[44,319,320],{}," * are included per finance team policy (RFC-2024-017).\n",[44,322,323],{"class":46,"line":71},[44,324,325],{}," *\u002F\n",[44,327,328],{"class":46,"line":78},[44,329,330],{},"WHERE payment_status = 'completed'\n",[44,332,333],{"class":46,"line":84},[44,334,335],{},"    AND payment_date >= '2024-07-01'\n",[26,337,339],{"id":338},"what-not-to-comment","What Not to Comment",[15,341,342],{},"Avoid comments that repeat the code:",[34,344,346],{"className":36,"code":345,"language":38,"meta":39,"style":39},"-- Bad: this comment adds nothing\n-- Select name and email from users\nSELECT name, email FROM users;\n\n-- Good: explain the business reason\n-- Primary contact info for weekly digest emails\nSELECT name, email FROM users;\n",[41,347,348,353,358,363,367,372,377],{"__ignoreMap":39},[44,349,350],{"class":46,"line":47},[44,351,352],{},"-- Bad: this comment adds nothing\n",[44,354,355],{"class":46,"line":53},[44,356,357],{},"-- Select name and email from users\n",[44,359,360],{"class":46,"line":59},[44,361,362],{},"SELECT name, email FROM users;\n",[44,364,365],{"class":46,"line":65},[44,366,75],{"emptyLinePlaceholder":74},[44,368,369],{"class":46,"line":71},[44,370,371],{},"-- Good: explain the business reason\n",[44,373,374],{"class":46,"line":78},[44,375,376],{},"-- Primary contact info for weekly digest emails\n",[44,378,379],{"class":46,"line":84},[44,380,362],{},[10,382,384],{"id":383},"team-collaboration-standards","Team Collaboration Standards",[26,386,388],{"id":387},"shared-formatting-rules","Shared Formatting Rules",[15,390,391],{},"Agree on these basics and document them:",[393,394,395,411],"table",{},[396,397,398],"thead",{},[399,400,401,405,408],"tr",{},[402,403,404],"th",{},"Decision",[402,406,407],{},"Recommendation",[402,409,410],{},"Reason",[412,413,414,426,437,448],"tbody",{},[399,415,416,420,423],{},[417,418,419],"td",{},"Keyword case",[417,421,422],{},"UPPERCASE",[417,424,425],{},"Visual separation from identifiers",[399,427,428,431,434],{},[417,429,430],{},"Indentation",[417,432,433],{},"4 spaces",[417,435,436],{},"Standard in most SQL formatters",[399,438,439,442,445],{},[417,440,441],{},"Comma placement",[417,443,444],{},"Trailing",[417,446,447],{},"Cleaner diffs when adding columns",[399,449,450,453,456],{},[417,451,452],{},"Line length",[417,454,455],{},"120 characters",[417,457,458],{},"Readable on most screens without wrap",[26,460,462],{"id":461},"version-control-practices","Version Control Practices",[104,464,465,472,475,485],{},[107,466,467,468,471],{},"Store all queries in ",[41,469,470],{},".sql"," files, never in application code strings",[107,473,474],{},"One logical query per file — avoid 500-line scripts with ten unrelated queries",[107,476,477,478,481,482],{},"Use consistent file naming — ",[41,479,480],{},"get_user_orders.sql",", not ",[41,483,484],{},"query3.sql",[107,486,487],{},"Run a SQL formatter as a pre-commit hook so formatting stays consistent",[26,489,491],{"id":490},"code-review-checklist","Code Review Checklist",[15,493,494],{},"Before approving a SQL change, check:",[104,496,497,500,503,506,509,512],{},[107,498,499],{},"Does the query use the correct JOIN type (INNER vs LEFT)?",[107,501,502],{},"Are all WHERE conditions present and correctly scoped?",[107,504,505],{},"Does GROUP BY include all non-aggregated SELECT columns?",[107,507,508],{},"Is the query indexed properly (see below)?",[107,510,511],{},"Does the query handle NULL values correctly?",[107,513,514],{},"Are table and column names consistent with project conventions?",[10,516,518],{"id":517},"cross-dialect-formatting-differences","Cross-Dialect Formatting Differences",[15,520,521],{},"SQL dialects share 90% of their syntax but diverge in specific areas. Knowing these differences prevents errors when your team works across databases.",[26,523,525],{"id":524},"string-concatenation","String Concatenation",[393,527,528,538],{},[396,529,530],{},[399,531,532,535],{},[402,533,534],{},"Database",[402,536,537],{},"Syntax",[412,539,540,550,560,570],{},[399,541,542,545],{},[417,543,544],{},"PostgreSQL",[417,546,547],{},[41,548,549],{},"'Hello' || ' World'",[399,551,552,555],{},[417,553,554],{},"MySQL",[417,556,557],{},[41,558,559],{},"CONCAT('Hello', ' World')",[399,561,562,565],{},[417,563,564],{},"SQL Server",[417,566,567],{},[41,568,569],{},"'Hello' + ' World'",[399,571,572,575],{},[417,573,574],{},"SQLite",[417,576,577],{},[41,578,549],{},[26,580,582],{"id":581},"limit-vs-top","LIMIT vs TOP",[34,584,586],{"className":36,"code":585,"language":38,"meta":39,"style":39},"-- PostgreSQL, MySQL, SQLite\nSELECT name FROM users LIMIT 10;\n\n-- SQL Server\nSELECT TOP 10 name FROM users;\n\n-- ANSI SQL:2008 (Oracle 12c+, PostgreSQL, MySQL)\nSELECT name FROM users FETCH FIRST 10 ROWS ONLY;\n",[41,587,588,593,598,602,607,612,616,621],{"__ignoreMap":39},[44,589,590],{"class":46,"line":47},[44,591,592],{},"-- PostgreSQL, MySQL, SQLite\n",[44,594,595],{"class":46,"line":53},[44,596,597],{},"SELECT name FROM users LIMIT 10;\n",[44,599,600],{"class":46,"line":59},[44,601,75],{"emptyLinePlaceholder":74},[44,603,604],{"class":46,"line":65},[44,605,606],{},"-- SQL Server\n",[44,608,609],{"class":46,"line":71},[44,610,611],{},"SELECT TOP 10 name FROM users;\n",[44,613,614],{"class":46,"line":78},[44,615,75],{"emptyLinePlaceholder":74},[44,617,618],{"class":46,"line":84},[44,619,620],{},"-- ANSI SQL:2008 (Oracle 12c+, PostgreSQL, MySQL)\n",[44,622,623],{"class":46,"line":90},[44,624,625],{},"SELECT name FROM users FETCH FIRST 10 ROWS ONLY;\n",[26,627,629],{"id":628},"date-functions","Date Functions",[15,631,632],{},"Each dialect handles dates differently. Always check before writing date logic:",[34,634,636],{"className":36,"code":635,"language":38,"meta":39,"style":39},"-- PostgreSQL: current timestamp\nNOW()\n\n-- SQL Server\nGETDATE()\n\n-- MySQL\nCURRENT_TIMESTAMP or NOW()\n",[41,637,638,643,648,652,656,661,665,670],{"__ignoreMap":39},[44,639,640],{"class":46,"line":47},[44,641,642],{},"-- PostgreSQL: current timestamp\n",[44,644,645],{"class":46,"line":53},[44,646,647],{},"NOW()\n",[44,649,650],{"class":46,"line":59},[44,651,75],{"emptyLinePlaceholder":74},[44,653,654],{"class":46,"line":65},[44,655,606],{},[44,657,658],{"class":46,"line":71},[44,659,660],{},"GETDATE()\n",[44,662,663],{"class":46,"line":78},[44,664,75],{"emptyLinePlaceholder":74},[44,666,667],{"class":46,"line":84},[44,668,669],{},"-- MySQL\n",[44,671,672],{"class":46,"line":90},[44,673,674],{},"CURRENT_TIMESTAMP or NOW()\n",[26,676,678],{"id":677},"quoting-identifiers","Quoting Identifiers",[15,680,681],{},"When you must use reserved words or mixed case as identifiers:",[393,683,684,693],{},[396,685,686],{},[399,687,688,690],{},[402,689,534],{},[402,691,692],{},"Quote Style",[412,694,695,704,713,722],{},[399,696,697,699],{},[417,698,544],{},[417,700,701],{},[41,702,703],{},"\"ColumnName\"",[399,705,706,708],{},[417,707,554],{},[417,709,710],{},[41,711,712],{},"`ColumnName`",[399,714,715,717],{},[417,716,564],{},[417,718,719],{},[41,720,721],{},"[ColumnName]",[399,723,724,727],{},[417,725,726],{},"ANSI standard",[417,728,729],{},[41,730,703],{},[15,732,733],{},"Avoid quoting altogether by choosing non-reserved, snake_case names.",[10,735,737],{"id":736},"key-takeaways","Key Takeaways",[104,739,740,743,746,749,752],{},[107,741,742],{},"Use snake_case for all table and column names — it works everywhere",[107,744,745],{},"Alias tables with short, meaningful abbreviations",[107,747,748],{},"Comment the \"why\" — the \"what\" should be obvious from the code",[107,750,751],{},"Agree on formatting rules as a team and enforce them with automated tools",[107,753,754],{},"Know the syntax differences between database dialects before writing cross-platform SQL",[10,756,758],{"id":757},"try-it-yourself","Try It Yourself",[15,760,761,762,767],{},"Apply consistent formatting to your queries instantly with our free ",[763,764,766],"a",{"href":765},"\u002Ftools\u002Fsql-formatter","SQL Formatter",". Paste SQL from any dialect and get clean, standardized output.",[10,769,771],{"id":770},"related-guides","Related Guides",[104,773,774,781],{},[107,775,776,780],{},[763,777,779],{"href":778},"\u002Fguides\u002Fsql-formatting-guide","SQL Formatting Guide: Writing Clean and Readable Queries"," — detailed rules for indentation, capitalization, and clause alignment",[107,782,783,787],{},[763,784,786],{"href":785},"\u002Fguides\u002Fsql-query-optimization","SQL Query Optimization: Performance Tips for Faster Queries"," — how structure and formatting help spot performance problems",[789,790,791],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":39,"searchDepth":53,"depth":53,"links":793},[794,795,799,804,809,815,816,817],{"id":12,"depth":53,"text":13},{"id":23,"depth":53,"text":24,"children":796},[797,798],{"id":28,"depth":59,"text":29},{"id":162,"depth":59,"text":163},{"id":250,"depth":53,"text":251,"children":800},[801,802,803],{"id":257,"depth":59,"text":258},{"id":288,"depth":59,"text":289},{"id":338,"depth":59,"text":339},{"id":383,"depth":53,"text":384,"children":805},[806,807,808],{"id":387,"depth":59,"text":388},{"id":461,"depth":59,"text":462},{"id":490,"depth":59,"text":491},{"id":517,"depth":53,"text":518,"children":810},[811,812,813,814],{"id":524,"depth":59,"text":525},{"id":581,"depth":59,"text":582},{"id":628,"depth":59,"text":629},{"id":677,"depth":59,"text":678},{"id":736,"depth":53,"text":737},{"id":757,"depth":53,"text":758},{"id":770,"depth":53,"text":771},"2026-05-28","Adopt professional SQL coding standards used by top engineering teams. Covers naming conventions, alias rules, comments, and cross-dialect differences.","md",{"immutable":74,"keywords":822},[823,824],"sql-formatter","sql-best-practices","\u002Fguides\u002Fsql-best-practices",{"title":5,"description":819},"guides\u002Fsql-best-practices","g00xQXTeG3nq7csHCM0NnEqq1LZcYvkdy7WMVNIlXwQ",1780401328333]