{"id":2535,"date":"2024-03-05T15:52:20","date_gmt":"2024-03-05T07:52:20","guid":{"rendered":"https:\/\/five.co\/?p=2535"},"modified":"2024-03-05T16:37:25","modified_gmt":"2024-03-05T08:37:25","slug":"sql-best-practices","status":"publish","type":"post","link":"https:\/\/preview-php85.dzine.org\/five\/blog\/sql-best-practices\/","title":{"rendered":"SQL Best Practices: 5 Tips for Writing Queries"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">SQL Best Practices: How to Use SQL Effectively<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Wherever there is a relational database, there is SQL or Structured Query Language. Used to interact with a database, SQL is one of the most widely used programming languages in the world. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s explore some of the best practices when using SQL, from indentation to writing readable SQL queries and formatting. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the post, we are also introducing you to a powerful tool that lets you create a SQL database and create SQL queries that follow the best practices introduced here! Make sure to watch the video at the end of the post.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Your SQL and Build a Data-Driven Web App<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s get hands-on with SQL: follow our free code-along tutorial to build a database from scratch and query it in SQL!<\/p>\n\n\n\n<div style=\"background-color: #001524;\"><hr style=\"height: 5px;\" \/>\n<pre style=\"text-align: center; overflow: hidden; white-space: pre-line;\"><span style=\"color: #f1ebda; background-color: #4588d8; font-size: calc(18px + 0.390625vw);\"><strong>Code Along: Build a SQL Database and Query It<\/strong><br \/><span style=\"font-size: 14pt;\">Follow our FREE step-by-step code-along tutorial<\/span><\/span><\/pre>\n<p style=\"text-align: center;\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/code-along\/acs-masterclass\" target=\"_blank\" rel=\"noopener\"><button style=\"background-color: #f8b92b; border: none; color: black; padding: 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 18px; cursor: pointer; margin: 4px 2px; border-radius: 5px;\"><strong>Learn More<\/strong><\/button><br \/><\/a><\/p>\n<hr style=\"height: 5px;\" \/><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"root-eb-toc-fja4xw6 wp-block-essential-blocks-table-of-contents\"><div class=\"eb-parent-wrapper eb-parent-eb-toc-fja4xw6 \"><div class=\"eb-toc-container eb-toc-fja4xw6  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none\" data-scroll-top=\"false\" data-scroll-top-icon=\"fas fa-angle-up\" data-collapsible=\"false\" data-sticky-hide-mobile=\"false\" data-sticky=\"false\" data-scroll-target=\"scroll_to_toc\" data-copy-link=\"false\" data-editor-type=\"\" data-hide-desktop=\"false\" data-hide-tab=\"false\" data-hide-mobile=\"false\" data-itemcollapsed=\"false\" data-highlight-scroll=\"false\"><div class=\"eb-toc-header\"><h2 class=\"eb-toc-title\">Table of Contents<\/h2><\/div><div class=\"eb-toc-wrapper \" data-headers=\"[{&quot;level&quot;:2,&quot;content&quot;:&quot;SQL Best Practices: How to Use SQL Effectively&quot;,&quot;text&quot;:&quot;SQL Best Practices: How to Use SQL Effectively&quot;,&quot;link&quot;:&quot;sql-best-practices-how-to-use-sql-effectively&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Practice Your SQL and Build a Data-Driven Web App&quot;,&quot;text&quot;:&quot;Practice Your SQL and Build a Data-Driven Web App&quot;,&quot;link&quot;:&quot;practice-your-sql-and-build-a-data-driven-web-app&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;5 Best Practices for Writing SQL&quot;,&quot;text&quot;:&quot;5 Best Practices for Writing SQL&quot;,&quot;link&quot;:&quot;5-best-practices-for-writing-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Capitalization and Case Sensitivity in SQL&quot;,&quot;text&quot;:&quot;Capitalization and Case Sensitivity in SQL&quot;,&quot;link&quot;:&quot;capitalization-and-case-sensitivity-in-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Comments in SQL&quot;,&quot;text&quot;:&quot;Comments in SQL&quot;,&quot;link&quot;:&quot;comments-in-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Quotes in SQL: &apos;&apos; or \\&quot;\\&quot;&quot;,&quot;text&quot;:&quot;Quotes in SQL: &apos;&apos; or \\&quot;\\&quot;&quot;,&quot;link&quot;:&quot;quotes-in-sql-or&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Using Aliases in SQL&quot;,&quot;text&quot;:&quot;Using Aliases in SQL&quot;,&quot;link&quot;:&quot;using-aliases-in-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Indentation&quot;,&quot;text&quot;:&quot;Indentation&quot;,&quot;link&quot;:&quot;indentation&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;SQL Tools: Applying SQL Best Practices&quot;,&quot;text&quot;:&quot;SQL Tools: Applying SQL Best Practices&quot;,&quot;link&quot;:&quot;sql-tools-applying-sql-best-practices&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1. Create Your Database Visually&quot;,&quot;text&quot;:&quot;1. Create Your Database Visually&quot;,&quot;link&quot;:&quot;1-create-your-database-visually&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2. Build SQL Queries Visually&quot;,&quot;text&quot;:&quot;2. Build SQL Queries Visually&quot;,&quot;link&quot;:&quot;2-build-sql-queries-visually&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;SQL Best Practices: Writing Clean, Concise, and Readable SQL Queries&quot;,&quot;text&quot;:&quot;SQL Best Practices: Writing Clean, Concise, and Readable SQL Queries&quot;,&quot;link&quot;:&quot;sql-best-practices-writing-clean-concise-and-readable-sql-queries&quot;}]\" data-visible=\"[true,true,true,true,true,true]\" data-delete-headers=\"[{&quot;label&quot;:&quot;SQL Best Practices: How to Use SQL Effectively&quot;,&quot;value&quot;:&quot;sql-best-practices-how-to-use-sql-effectively&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Practice Your SQL and Build a Data-Driven Web App&quot;,&quot;value&quot;:&quot;practice-your-sql-and-build-a-data-driven-web-app&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;5 Best Practices for Writing SQL&quot;,&quot;value&quot;:&quot;5-best-practices-for-writing-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Capitalization and Case Sensitivity in SQL&quot;,&quot;value&quot;:&quot;capitalization-and-case-sensitivity-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Comments in SQL&quot;,&quot;value&quot;:&quot;comments-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Quotes in SQL: &apos;&apos; or \\&quot;\\&quot;&quot;,&quot;value&quot;:&quot;quotes-in-sql-or&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Using Aliases in SQL&quot;,&quot;value&quot;:&quot;using-aliases-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Indentation&quot;,&quot;value&quot;:&quot;indentation&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;SQL Tools: Applying SQL Best Practices&quot;,&quot;value&quot;:&quot;sql-tools-applying-sql-best-practices&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Create Your Database Visually&quot;,&quot;value&quot;:&quot;1-create-your-database-visually&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2. Build SQL Queries Visually&quot;,&quot;value&quot;:&quot;2-build-sql-queries-visually&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;SQL Best Practices: Writing Clean, Concise, and Readable SQL Queries&quot;,&quot;value&quot;:&quot;sql-best-practices-writing-clean-concise-and-readable-sql-queries&quot;,&quot;isDelete&quot;:false}]\" data-smooth=\"true\" data-top-offset=\"\"><div class=\"eb-toc__list-wrap\"><ul class=\"eb-toc__list\"><li><a href=\"#5-best-practices-for-writing-sql\">5 Best Practices for Writing SQL<\/a><ul class=\"eb-toc__list\"><li><a href=\"#capitalization-and-case-sensitivity-in-sql\">Capitalization and Case Sensitivity in SQL<\/a><li><a href=\"#comments-in-sql\">Comments in SQL<\/a><li><a href=\"#quotes-in-sql-or\">Quotes in SQL: &#8221; or &#8220;&#8221;<\/a><li><a href=\"#using-aliases-in-sql\">Using Aliases in SQL<\/a><li><a href=\"#indentation\">Indentation<\/a><\/li><\/ul><li><a href=\"#sql-tools-applying-sql-best-practices\">SQL Tools: Applying SQL Best Practices<\/a><ul class=\"eb-toc__list\"><li><a href=\"#1-create-your-database-visually\">1. Create Your Database Visually<\/a><li><a href=\"#2-build-sql-queries-visually\">2. Build SQL Queries Visually<\/a><\/li><\/ul><li><a href=\"#sql-best-practices-writing-clean-concise-and-readable-sql-queries\">SQL Best Practices: Writing Clean, Concise, and Readable SQL Queries<\/a><\/ul><\/div><\/div><\/div><\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5 Best Practices for Writing SQL<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Capitalization and Case Sensitivity in SQL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, let&#8217;s speak about capitalization and case sensitivity. Most SQL dialects, such as MySQL, PostgreSQL, or MS SQL Server, don&#8217;t care whether you write <code>SELECT<\/code>, <code>select <\/code>or <code>sELEct <\/code>in your query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But other people do care. The developers on your team or anyone else analysing your code will appreciate a well-written SQL query that follows commonly-used best practices. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> To make your queries look professional, be consistent in your capitalization. SQL best practice says that:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Use all caps for clause starters, such as <code>SELECT<\/code>, <code>FROM<\/code>, <code>WHERE <\/code>or functions, such as <code>SUM()<\/code><\/li>\n\n\n\n<li>Column names (i.e. the things are you <code>SELECT<\/code>-ing) should be all lowercase, and<\/li>\n\n\n\n<li>Table names should be in CamelCase, such as CustomerOrders or shortened to meaningful abbreviations.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">So, to sum this up, here&#8217;s what your query should look like:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT\n  u.id AS user_ID,\n  u.name AS user_name,\n  u.age AS user_age\nFROM\n  Users u\nINNER JOIN\n  UserProfiles p\nON\n  u.id = p.user_id;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #D19A66\">u<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">id<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> user_ID,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #D19A66\">u<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">name<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> user_name,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #D19A66\">u<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">age<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> user_age<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  Users u<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">INNER JOIN<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  UserProfiles p<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">ON<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #D19A66\">u<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">id<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">p<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">user_id<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Looks great, doesn&#8217;t it? It&#8217;s easy to read, cleanly formatted, and can easily be debugged if necessary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While looking at this query, you might have noticed something else: our <code>Users <\/code>table was renamed as <code>u<\/code> and our <code>UserProfiles <\/code>table as <code>p<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a fairly common practice in SQL, but not to everyone&#8217;s liking. On the one hand, it shortens your query and requires less typing. On the other hand, single letters are not very meaningful. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you decide to shorten your table names, consider using descriptive acronyms rather than single letters. If the table names are quite short and well-named already, keep them as they are.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a table named <code>related_party_transactions<\/code> could be shortened to <code>rpt<\/code>, which improves conciseness without sacrificing readability. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s move on to quotes: what&#8217;s the SQL best practice for using quotes?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Comments in SQL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First up, we recommend to comment your SQL queries, provided your comment adds explanatory value to your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, this comment does <em>not<\/em> have explanatory value. It is rather just restating the obvious and therefore redundant.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"COUNT(*) AS &quot;NumberOfUsers&quot; \n-- Count Number Of Users\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #56B6C2\">COUNT<\/span><span style=\"color: #ABB2BF\">(*) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&quot;NumberOfUsers&quot;<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Count Number Of Users<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Useful comments explain your code. For example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- This query retrieves the total sales amount for each product in the last quarter of the current fiscal year.\n\nSELECT \n    ProductID,\n    SUM(Amount) AS TotalSales\nFROM \n    Sales\nWHERE \n    OrderDate &gt;= '2024-01-01' AND OrderDate &lt; '2024-04-01'\nGROUP BY \n    ProductID;\n\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- This query retrieves the total sales amount for each product in the last quarter of the current fiscal year.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ProductID,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">SUM<\/span><span style=\"color: #ABB2BF\">(Amount) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> TotalSales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    Sales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    OrderDate <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;2024-01-01&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AND<\/span><span style=\"color: #ABB2BF\"> OrderDate <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;2024-04-01&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ProductID;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Only use comments when they add information or have explanatory value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, think about whether you are adding a single-line or multi-line comment. The correct syntax for each is this:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- This is a single-line comment\n\n\/*\nThis is a\nmulti-line\ncomment\n*\/\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- This is a single-line comment<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">\/*<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">This is a<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">multi-line<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">comment<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">*\/<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Anything marked as a comment will be ignored by the database engine. Using two dashes &#8212; for single line comments is compatible with all SQL dialects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Quotes in SQL: &#8221; or &#8220;&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Most SQL dialects accept both single and double quotes to mark a text as a string. For example, <code>'This is a string'<\/code> is as valid as <code>\"This is a string\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only times you need to pay attention to your quotes in SQL is when you are writing a word with an apostrophe or quotation mark inside a string. For example, consider this query:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\" SELECT \n   COUNTRY\n FROM\n   COUNTRIES\n WHERE \n   CAPITAL = Saint John's\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   COUNTRY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   COUNTRIES<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   CAPITAL <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> Saint John<\/span><span style=\"color: #98C379\">&#39;s<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Note that this <code>WHERE<\/code> will not produce the correct result, because the &#8216; in Saint John&#8217;s isn&#8217;t treated as an apostrophe. The correct query is:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\" SELECT \n   COUNTRY\n FROM\n   COUNTRIES\n WHERE \n   CAPITAL = &quot;Saint John's&quot;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   COUNTRY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   COUNTRIES<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">   CAPITAL <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&quot;Saint John&#39;s&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Now the query would produce the correct result: Antigua and Barbuda (but I guess you knew that already anyway).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What&#8217;s the rule here? Generally, go with single quotes &#8221;. Single quotes are accepted across almost all SQL dialects. Use double quotes &#8220;&#8221; when encapsulating a text that contains an apostrophe or quotation marks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using Aliases in SQL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using aliases can make queries easier to read and understand, especially when dealing with complex joins or subqueries. They provide shorthand references to database tables and column names, reducing the need for verbose or repetitive syntax. This improves the clarity and conciseness of your SQL query and makes them part and parcel of SQL best practices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Aliases are typically used to rename columns in the result set, providing more meaningful or descriptive names for presentation purposes without altering the underlying data model. Typically, snake_case is used for aliases. Note that the choice of naming convention for aliases often depends on the coding standards and preferences of the development team or organisation, and that using snake_case is not a hard rule.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT \n    COUNT(customers) AS total_customers\nFROM \n    customers;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">COUNT<\/span><span style=\"color: #ABB2BF\">(customers) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> total_customers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    customers;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Indentation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Correct indentation can greatly increase the readability of a SQL query. Here are a few helpful tips:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Keep the length of each line of code below 100 characters to improve readability.<\/li>\n\n\n\n<li>Use indentation as a tool to highlight the logic of your code.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider this query and note how the indentation helps with our understanding of its workings:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT \n    CASE \n        WHEN age &lt; 18 THEN 'Underage'\n        WHEN age &gt;= 18 AND age &lt; 65 THEN 'Adult'\n        ELSE 'Senior'\n    END AS age_group,\n    COUNT(*) AS count_of_people\nFROM \n    users\nGROUP BY \n    CASE \n        WHEN age &lt; 18 THEN 'Underage'\n        WHEN age &gt;= 18 AND age &lt; 65 THEN 'Adult'\n        ELSE 'Senior'\n    END;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">CASE<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Underage&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AND<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">65<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Adult&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ELSE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Senior&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">END<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> age_group,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">COUNT<\/span><span style=\"color: #ABB2BF\">(*) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> count_of_people<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    users<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">CASE<\/span><span style=\"color: #ABB2BF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Underage&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AND<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">65<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Adult&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ELSE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Senior&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">END<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this query:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The CASE expression categorizes users into different age groups (&#8216;Underage&#8217;, &#8216;Adult&#8217;, &#8216;Senior&#8217;).<\/li>\n\n\n\n<li>The GROUP BY clause groups the data by the age group.<\/li>\n\n\n\n<li>COUNT(*) calculates the count of people in each age group.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s re-write the query into one big chunk of code. Do you still understand it&#8217;s purpose? <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT CASE WHEN age &lt; 18 THEN 'Underage' WHEN age &gt;= 18 AND age &lt; 65 THEN 'Adult' ELSE 'Senior' END AS age_group, COUNT(*) AS count_of_people FROM users GROUP BY CASE WHEN age &lt; 18 THEN 'Underage' WHEN age &gt;= 18 AND age &lt; 65 THEN 'Adult' ELSE 'Senior' END;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">CASE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Underage&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AND<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">65<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Adult&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ELSE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Senior&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">END<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> age_group, <\/span><span style=\"color: #56B6C2\">COUNT<\/span><span style=\"color: #ABB2BF\">(*) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> count_of_people <\/span><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> users <\/span><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">CASE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Underage&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHEN<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">18<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AND<\/span><span style=\"color: #ABB2BF\"> age <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">65<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Adult&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ELSE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;Senior&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">END<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Tools: Applying SQL Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Following these SQL best practices can be difficult at the start. There&#8217;s a lot that database administrators or developers need to pay attention to. Luckily, some tools make this job easier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Five, for example, offers an easy-to-use interface to create an SQL database and write SQL queries. It can even translate queries designed in point-and-click into standard SQL. Here&#8217;s how.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create Your Database Visually<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Five&#8217;s Database Modeler gives developers a handy tool to create, manage, and understand their database schema. The Database Modeler can be used to perform all standard SQL tasks, such as adding fields, defining data types or creating relationships.  <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img width=\"1024\" height=\"596\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-1024x596.png\" alt=\"\" class=\"wp-image-2537\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-1024x596.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-300x175.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-768x447.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-1536x894.png 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/image-2048x1191.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><em>Five&#8217;s Visual Database Modeler Makes It Easy to Create a SQL Database<\/em><\/figcaption><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Build SQL Queries Visually<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once a database has been created or added to Five, Five can be used to write SQL in two ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Through Five&#8217;s Query Designer, a point-and-click interface to build SQL queries, or<\/li>\n\n\n\n<li>Through Five&#8217;s SQL Editor, a standard coding interface for SQL.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The best part is that any query that&#8217;s built inside the designer gets translated right into SQL. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Watch this video to better understand how Five can be used to write SQL queries.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"SQL Database &amp; Query Designer | Low-Code For Real Developers | Five.Co\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/op206jpTQzE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Best Practices: Writing Clean, Concise, and Readable SQL Queries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This sums up our guide to writing clean, concise, and readable SQL queries. By following these 5 simple SQL best practices, you can greatly improve your SQL. So, whenever you write SQL, following these standards should become second nature to you. Practice often and enjoy! <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more SQL and database knowledge, tips and tricks, and useful advice, visit our blog post on <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/how-to-create-many-to-many-relationships-in-sql\/\" data-type=\"post\" data-id=\"1824\">many-to-many relationships<\/a>, <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/how-to-create-one-to-many-relationships-in-sql\/\" data-type=\"post\" data-id=\"2373\">one-to-many relationships<\/a>, or <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/surrogate-key-vs-primary-key\/\" data-type=\"post\" data-id=\"2029\">surrogate vs primary keys<\/a> in SQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Best Practices: How to Use SQL Effectively Wherever there is a relational database, there is SQL or Structured Query Language. Used to interact with a database, SQL is one of the most widely used programming languages in the world. Let&#8217;s explore some of the best practices when using SQL, from indentation to writing readable [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2536,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[21],"tags":[],"class_list":["post-2535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices.png",960,540,false],"thumbnail":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices-150x150.png",150,150,true],"medium":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices-300x169.png",300,169,true],"medium_large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices-768x432.png",768,432,true],"large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices.png",960,540,false],"1536x1536":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices.png",960,540,false],"2048x2048":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/03\/Five.Co-SQL-Best-Practices.png",960,540,false]},"uagb_author_info":{"display_name":"Dominik Keller","author_link":"https:\/\/preview-php85.dzine.org\/five\/author\/f1v5_ed3_my\/"},"uagb_comment_info":0,"uagb_excerpt":"SQL Best Practices: How to Use SQL Effectively Wherever there is a relational database, there is SQL or Structured Query Language. Used to interact with a database, SQL is one of the most widely used programming languages in the world. Let&#8217;s explore some of the best practices when using SQL, from indentation to writing readable&hellip;","_links":{"self":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/2535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/comments?post=2535"}],"version-history":[{"count":0,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/2535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media\/2536"}],"wp:attachment":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media?parent=2535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/categories?post=2535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/tags?post=2535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}