{"id":4324,"date":"2024-10-28T06:56:16","date_gmt":"2024-10-27T22:56:16","guid":{"rendered":"https:\/\/five.co\/?p=4324"},"modified":"2024-10-28T06:56:18","modified_gmt":"2024-10-27T22:56:18","slug":"using-string-agg-in-sql","status":"publish","type":"post","link":"https:\/\/preview-php85.dzine.org\/five\/blog\/using-string-agg-in-sql\/","title":{"rendered":"Using STRING_AGG in SQL: A Practical Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Breakdown of STRING_AGG in SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">STRING_AGG turns multiple rows into a single concatenated string &#8211; a task that&#8217;s essential for reporting, data analysis, and API responses. Let&#8217;s explore how to use it effectively with real examples you can apply to your own databases.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"root-eb-toc-vezkp wp-block-essential-blocks-table-of-contents\"><div class=\"eb-parent-wrapper eb-parent-eb-toc-vezkp \"><div class=\"eb-toc-container eb-toc-vezkp  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;Breakdown of STRING_AGG in SQL&quot;,&quot;text&quot;:&quot;Breakdown of STRING_AGG in SQL&quot;,&quot;link&quot;:&quot;breakdown-of-string_agg-in-sql&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Basic STRING_AGG Usage&quot;,&quot;text&quot;:&quot;Basic STRING_AGG Usage&quot;,&quot;link&quot;:&quot;basic-string_agg-usage&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Simple Concatenation&quot;,&quot;text&quot;:&quot;Simple Concatenation&quot;,&quot;link&quot;:&quot;simple-concatenation&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Custom Separators&quot;,&quot;text&quot;:&quot;Custom Separators&quot;,&quot;link&quot;:&quot;custom-separators&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Advanced STRING_AGG Features&quot;,&quot;text&quot;:&quot;Advanced STRING_AGG Features&quot;,&quot;link&quot;:&quot;advanced-string_agg-features&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Ordering Results&quot;,&quot;text&quot;:&quot;Ordering Results&quot;,&quot;link&quot;:&quot;ordering-results&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Filtering NULL Values&quot;,&quot;text&quot;:&quot;Filtering NULL Values&quot;,&quot;link&quot;:&quot;filtering-null-values&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Real-World Applications&quot;,&quot;text&quot;:&quot;Real-World Applications&quot;,&quot;link&quot;:&quot;real-world-applications&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Building Tag Systems&quot;,&quot;text&quot;:&quot;Building Tag Systems&quot;,&quot;link&quot;:&quot;building-tag-systems&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Customer Order History&quot;,&quot;text&quot;:&quot;Customer Order History&quot;,&quot;link&quot;:&quot;customer-order-history&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Activity Logs&quot;,&quot;text&quot;:&quot;Activity Logs&quot;,&quot;link&quot;:&quot;activity-logs&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Performance Optimization&quot;,&quot;text&quot;:&quot;Performance Optimization&quot;,&quot;link&quot;:&quot;performance-optimization&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Using DISTINCT&quot;,&quot;text&quot;:&quot;Using DISTINCT&quot;,&quot;link&quot;:&quot;using-distinct&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Handling Large Text&quot;,&quot;text&quot;:&quot;Handling Large Text&quot;,&quot;link&quot;:&quot;handling-large-text&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Cross-Database Compatibility&quot;,&quot;text&quot;:&quot;Cross-Database Compatibility&quot;,&quot;link&quot;:&quot;cross-database-compatibility&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;MySQL Alternative&quot;,&quot;text&quot;:&quot;MySQL Alternative&quot;,&quot;link&quot;:&quot;mysql-alternative&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;SQLite Alternative&quot;,&quot;text&quot;:&quot;SQLite Alternative&quot;,&quot;link&quot;:&quot;sqlite-alternative&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Common Mistakes to Avoid&quot;,&quot;text&quot;:&quot;Common Mistakes to Avoid&quot;,&quot;link&quot;:&quot;common-mistakes-to-avoid&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Special Use Cases&quot;,&quot;text&quot;:&quot;Special Use Cases&quot;,&quot;link&quot;:&quot;special-use-cases&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;JSON Creation&quot;,&quot;text&quot;:&quot;JSON Creation&quot;,&quot;link&quot;:&quot;json-creation&quot;}]\" data-visible=\"[true,true,true,true,true,true]\" data-delete-headers=\"[{&quot;label&quot;:&quot;Breakdown of STRING_AGG in SQL&quot;,&quot;value&quot;:&quot;breakdown-of-string_agg-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Basic STRING_AGG Usage&quot;,&quot;value&quot;:&quot;basic-string_agg-usage&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Simple Concatenation&quot;,&quot;value&quot;:&quot;simple-concatenation&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Custom Separators&quot;,&quot;value&quot;:&quot;custom-separators&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Advanced STRING_AGG Features&quot;,&quot;value&quot;:&quot;advanced-string_agg-features&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Ordering Results&quot;,&quot;value&quot;:&quot;ordering-results&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Filtering NULL Values&quot;,&quot;value&quot;:&quot;filtering-null-values&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Real-World Applications&quot;,&quot;value&quot;:&quot;real-world-applications&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Building Tag Systems&quot;,&quot;value&quot;:&quot;building-tag-systems&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Customer Order History&quot;,&quot;value&quot;:&quot;customer-order-history&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Activity Logs&quot;,&quot;value&quot;:&quot;activity-logs&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Performance Optimization&quot;,&quot;value&quot;:&quot;performance-optimization&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Using DISTINCT&quot;,&quot;value&quot;:&quot;using-distinct&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Handling Large Text&quot;,&quot;value&quot;:&quot;handling-large-text&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Cross-Database Compatibility&quot;,&quot;value&quot;:&quot;cross-database-compatibility&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;MySQL Alternative&quot;,&quot;value&quot;:&quot;mysql-alternative&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;SQLite Alternative&quot;,&quot;value&quot;:&quot;sqlite-alternative&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Common Mistakes to Avoid&quot;,&quot;value&quot;:&quot;common-mistakes-to-avoid&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Special Use Cases&quot;,&quot;value&quot;:&quot;special-use-cases&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;JSON Creation&quot;,&quot;value&quot;:&quot;json-creation&quot;,&quot;isDelete&quot;:true}]\" data-smooth=\"true\" data-top-offset=\"\"><div class=\"eb-toc__list-wrap\"><ul class=\"eb-toc__list\"><li><a href=\"#breakdown-of-string_agg-in-sql\">Breakdown of STRING_AGG in SQL<\/a><li><a href=\"#basic-string_agg-usage\">Basic STRING_AGG Usage<\/a><li><a href=\"#advanced-string_agg-features\">Advanced STRING_AGG Features<\/a><li><a href=\"#real-world-applications\">Real-World Applications<\/a><li><a href=\"#performance-optimization\">Performance Optimization<\/a><li><a href=\"#cross-database-compatibility\">Cross-Database Compatibility<\/a><li><a href=\"#common-mistakes-to-avoid\">Common Mistakes to Avoid<\/a><li><a href=\"#special-use-cases\">Special Use Cases<\/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\">Basic STRING_AGG Usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Simple Concatenation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start with a basic example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample table: products\n-- id | category | product_name\n-- 1  | fruits   | apple\n-- 2  | fruits   | banana\n-- 3  | fruits   | orange\n-- 4  | veggies  | carrot\n\n-- Get all products in each category as a comma-separated list\nSELECT \n    category,\n    STRING_AGG(product_name, ', ') AS product_list\nFROM products\nGROUP BY category;\n\n-- Output:\n-- category | product_list\n-- fruits   | apple, banana, orange\n-- veggies  | carrot\" 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\">-- Sample table: products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- id | category | product_name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1  | fruits   | apple<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2  | fruits   | banana<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 3  | fruits   | orange<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 4  | veggies  | carrot<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Get all products in each category as a comma-separated list<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> product_list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- category | product_list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- fruits   | apple, banana, orange<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- veggies  | carrot<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Separators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use any separator, not just commas:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Using different separators\nSELECT \n    category,\n    STRING_AGG(product_name, ' | ') AS product_list,\n    STRING_AGG(product_name, ' \u2192 ') AS product_path\nFROM products\nGROUP BY category;\n\n-- Output:\n-- category | product_list         | product_path\n-- fruits   | apple | banana | orange | apple \u2192 banana \u2192 orange\n-- veggies  | carrot               | carrot\" 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\">-- Using different separators<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39; | &#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> product_list,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39; \u2192 &#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> product_path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- category | product_list         | product_path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- fruits   | apple | banana | orange | apple \u2192 banana \u2192 orange<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- veggies  | carrot               | carrot<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced STRING_AGG Features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Ordering Results<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Control the order of concatenated items:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample table: sales\n-- product_name | sales_amount\n-- apple        | 1000\n-- banana       | 2000\n-- orange       | 1500\n\n-- Concatenate products ordered by sales\nSELECT STRING_AGG(product_name, ', ' ORDER BY sales_amount DESC) \n    AS products_by_sales\nFROM sales;\n\n-- Output:\n-- products_by_sales\n-- banana, orange, apple\" 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\">-- Sample table: sales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- product_name | sales_amount<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- apple        | 1000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- banana       | 2000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- orange       | 1500<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Concatenate products ordered by sales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> sales_amount <\/span><span style=\"color: #C678DD\">DESC<\/span><span style=\"color: #ABB2BF\">) <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> products_by_sales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> sales;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- products_by_sales<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- banana, orange, apple<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Filtering NULL Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Handle NULL values appropriately:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample data with nulls\n-- product | stock_status\n-- apple   | in stock\n-- banana  | NULL\n-- orange  | out of stock\n\nSELECT STRING_AGG(\n    CASE \n        WHEN stock_status IS NOT NULL THEN product \n    END, \n    ', '\n) AS available_products\nFROM inventory\nWHERE stock_status = 'in stock';\n\n-- Output:\n-- available_products\n-- apple\" 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\">-- Sample data with nulls<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- product | stock_status<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- apple   | in stock<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- banana  | NULL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- orange  | out of stock<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/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\"> stock_status <\/span><span style=\"color: #C678DD\">IS NOT NULL<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">THEN<\/span><span style=\"color: #ABB2BF\"> product <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">END<\/span><span style=\"color: #ABB2BF\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> available_products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> inventory<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> stock_status <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;in stock&#39;<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- available_products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- apple<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Applications<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Building Tag Systems<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a tag management system:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample tables\n-- posts\n-- id | title\n-- 1  | First Post\n-- 2  | Second Post\n\n-- post_tags\n-- post_id | tag\n-- 1       | sql\n-- 1       | database\n-- 2       | sql\n-- 2       | tutorial\n\n-- Get posts with their tags\nSELECT \n    p.title,\n    STRING_AGG(pt.tag, ', ' ORDER BY pt.tag) AS tags\nFROM posts p\nLEFT JOIN post_tags pt ON p.id = pt.post_id\nGROUP BY p.id, p.title;\n\n-- Output:\n-- title       | tags\n-- First Post  | database, sql\n-- Second Post | sql, tutorial\" 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\">-- Sample tables<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- posts<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- id | title<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1  | First Post<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2  | Second Post<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- post_tags<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- post_id | tag<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | sql<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | database<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2       | sql<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2       | tutorial<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Get posts with their tags<\/span><\/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\">    <\/span><span style=\"color: #D19A66\">p<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">title<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">pt<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">tag<\/span><span style=\"color: #ABB2BF\">, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">pt<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">tag<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> tags<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> posts p<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">LEFT JOIN<\/span><span style=\"color: #ABB2BF\"> post_tags pt <\/span><span style=\"color: #C678DD\">ON<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">p<\/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\">pt<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">post_id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">p<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">id<\/span><span style=\"color: #ABB2BF\">, <\/span><span style=\"color: #D19A66\">p<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">title<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- title       | tags<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- First Post  | database, sql<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Second Post | sql, tutorial<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Customer Order History<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Summarize order history:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample tables\n-- orders\n-- id | customer_id | order_date\n-- 1  | 101        | 2024-01-01\n-- 2  | 101        | 2024-01-15\n\n-- order_items\n-- order_id | product\n-- 1        | laptop\n-- 1        | mouse\n-- 2        | keyboard\n\nSELECT \n    o.customer_id,\n    STRING_AGG(\n        oi.product || ' (' || o.order_date || ')',\n        '; '\n        ORDER BY o.order_date\n    ) AS order_history\nFROM orders o\nJOIN order_items oi ON o.id = oi.order_id\nGROUP BY o.customer_id;\n\n-- Output:\n-- customer_id | order_history\n-- 101        | laptop (2024-01-01); mouse (2024-01-01); keyboard (2024-01-15)\" 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\">-- Sample tables<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- orders<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- id | customer_id | order_date<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1  | 101        | 2024-01-01<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2  | 101        | 2024-01-15<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- order_items<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- order_id | product<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1        | laptop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1        | mouse<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 2        | keyboard<\/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\">    <\/span><span style=\"color: #D19A66\">o<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">customer_id<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #D19A66\">oi<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">product<\/span><span style=\"color: #ABB2BF\"> || <\/span><span style=\"color: #98C379\">&#39; (&#39;<\/span><span style=\"color: #ABB2BF\"> || <\/span><span style=\"color: #D19A66\">o<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">order_date<\/span><span style=\"color: #ABB2BF\"> || <\/span><span style=\"color: #98C379\">&#39;)&#39;<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #98C379\">&#39;; &#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">o<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">order_date<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> order_history<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> orders o<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">JOIN<\/span><span style=\"color: #ABB2BF\"> order_items oi <\/span><span style=\"color: #C678DD\">ON<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">o<\/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\">oi<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">order_id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">o<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">customer_id<\/span><span style=\"color: #ABB2BF\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- customer_id | order_history<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 101        | laptop (2024-01-01); mouse (2024-01-01); keyboard (2024-01-15)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Activity Logs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create readable activity summaries:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample table: user_activities\n-- user_id | activity      | timestamp\n-- 1       | login        | 2024-03-21 09:00\n-- 1       | view_profile | 2024-03-21 09:05\n-- 1       | logout       | 2024-03-21 09:30\n\nSELECT \n    user_id,\n    STRING_AGG(\n        activity || ' at ' || \n        TO_CHAR(timestamp, 'HH24:MI'),\n        ' \u2192 '\n        ORDER BY timestamp\n    ) AS activity_timeline\nFROM user_activities\nGROUP BY user_id;\n\n-- Output:\n-- user_id | activity_timeline\n-- 1       | login at 09:00 \u2192 view_profile at 09:05 \u2192 logout at 09:30\" 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\">-- Sample table: user_activities<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- user_id | activity      | timestamp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | login        | 2024-03-21 09:00<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | view_profile | 2024-03-21 09:05<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | logout       | 2024-03-21 09:30<\/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\">    user_id,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        activity || <\/span><span style=\"color: #98C379\">&#39; at &#39;<\/span><span style=\"color: #ABB2BF\"> || <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        TO_CHAR(<\/span><span style=\"color: #C678DD\">timestamp<\/span><span style=\"color: #ABB2BF\">, <\/span><span style=\"color: #98C379\">&#39;HH24:MI&#39;<\/span><span style=\"color: #ABB2BF\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #98C379\">&#39; \u2192 &#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">timestamp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> activity_timeline<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> user_activities<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> user_id;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- user_id | activity_timeline<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | login at 09:00 \u2192 view_profile at 09:05 \u2192 logout at 09:30<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Performance Optimization<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using DISTINCT<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Remove duplicates while aggregating:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Sample table: page_views\n-- page_id | visitor_id\n-- 1       | 101\n-- 1       | 101  -- duplicate\n-- 1       | 102\n\nSELECT \n    page_id,\n    STRING_AGG(DISTINCT CAST(visitor_id AS VARCHAR), ', ') AS unique_visitors\nFROM page_views\nGROUP BY page_id;\n\n-- Output:\n-- page_id | unique_visitors\n-- 1       | 101, 102\" 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\">-- Sample table: page_views<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- page_id | visitor_id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | 101<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | 101  -- duplicate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | 102<\/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\">    page_id,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #C678DD\">DISTINCT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">CAST<\/span><span style=\"color: #ABB2BF\">(visitor_id <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">), <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> unique_visitors<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> page_views<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> page_id;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- page_id | unique_visitors<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- 1       | 101, 102<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Large Text<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manage long concatenated strings:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Set maximum length for concatenated strings\nSELECT \n    category,\n    LEFT(STRING_AGG(description, ' '), 200) || '...' AS category_description\nFROM products\nGROUP BY category;\n\n-- Using subquery for better performance with large datasets\nSELECT \n    category,\n    (\n        SELECT STRING_AGG(product_name, ', ')\n        FROM (\n            SELECT product_name \n            FROM products p2 \n            WHERE p2.category = p1.category\n            LIMIT 5\n        ) sub\n    ) AS top_products\nFROM products p1\nGROUP BY category;\" 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\">-- Set maximum length for concatenated strings<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #56B6C2\">LEFT<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #C678DD\">description<\/span><span style=\"color: #ABB2BF\">, <\/span><span style=\"color: #98C379\">&#39; &#39;<\/span><span style=\"color: #ABB2BF\">), <\/span><span style=\"color: #D19A66\">200<\/span><span style=\"color: #ABB2BF\">) || <\/span><span style=\"color: #98C379\">&#39;...&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> category_description<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Using subquery for better performance with large datasets<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">            <\/span><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> product_name <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">            <\/span><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products p2 <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">            <\/span><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">p2<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">category<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">p1<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #D19A66\">category<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">            <\/span><span style=\"color: #C678DD\">LIMIT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #D19A66\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        ) sub<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> top_products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products p1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Cross-Database Compatibility<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL Alternative<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For MySQL users:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- MySQL equivalent using GROUP_CONCAT\nSELECT \n    category,\n    GROUP_CONCAT(\n        product_name\n        ORDER BY product_name\n        SEPARATOR ', '\n    ) AS product_list\nFROM products\nGROUP BY category;\" 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\">-- MySQL equivalent using GROUP_CONCAT<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    GROUP_CONCAT(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        product_name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> product_name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        SEPARATOR <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> product_list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">SQLite Alternative<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For SQLite users:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);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=\"-- SQLite using GROUP_CONCAT\nSELECT \n    category,\n    GROUP_CONCAT(product_name, ', ') AS product_list\nFROM products\nGROUP BY category;\" 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\">-- SQLite using GROUP_CONCAT<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    GROUP_CONCAT(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> product_list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes to Avoid<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Not Handling NULLs Properly<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);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=\"-- Wrong way - NULLs can break concatenation\nSTRING_AGG(column_name, ', ')\n\n-- Right way - Handle NULLs explicitly\nSTRING_AGG(COALESCE(column_name, ''), ', ')\" 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\">-- Wrong way - NULLs can break concatenation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(column_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Right way - Handle NULLs explicitly<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #56B6C2\">COALESCE<\/span><span style=\"color: #ABB2BF\">(column_name, <\/span><span style=\"color: #98C379\">&#39;&#39;<\/span><span style=\"color: #ABB2BF\">), <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Forgetting Order By<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);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=\"-- Wrong way - Undefined order\nSTRING_AGG(product_name, ', ')\n\n-- Right way - Explicit ordering\nSTRING_AGG(product_name, ', ' ORDER BY product_name)\" 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\">-- Wrong way - Undefined order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Right way - Explicit ordering<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(product_name, <\/span><span style=\"color: #98C379\">&#39;, &#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> product_name)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Not Setting Maximum Lengths<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);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=\"-- Potential memory issues with large datasets\nSTRING_AGG(long_text, ' ')\n\n-- Better - Limit output length\nLEFT(STRING_AGG(long_text, ' '), 1000)\" 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\">-- Potential memory issues with large datasets<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(long_text, <\/span><span style=\"color: #98C379\">&#39; &#39;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Better - Limit output length<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56B6C2\">LEFT<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(long_text, <\/span><span style=\"color: #98C379\">&#39; &#39;<\/span><span style=\"color: #ABB2BF\">), <\/span><span style=\"color: #D19A66\">1000<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Special Use Cases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">JSON Creation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build JSON arrays from rows:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" 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;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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=\"-- Create JSON array of products\nSELECT \n    category,\n    '[' || STRING_AGG(\n        '&quot;' || product_name || '&quot;',\n        ','\n        ORDER BY product_name\n    ) || ']' AS products_json\nFROM products\nGROUP BY category;\n\n-- Output:\n-- category | products_json\n-- fruits   | [&quot;apple&quot;,&quot;banana&quot;,&quot;orange&quot;]\n-- veggies  | [&quot;carrot&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: #7F848E; font-style: italic\">-- Create JSON array of products<\/span><\/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\">    category,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #98C379\">&#39;[&#39;<\/span><span style=\"color: #ABB2BF\"> || <\/span><span style=\"color: #56B6C2\">STRING_AGG<\/span><span style=\"color: #ABB2BF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #98C379\">&#39;&quot;&#39;<\/span><span style=\"color: #ABB2BF\"> || product_name || <\/span><span style=\"color: #98C379\">&#39;&quot;&#39;<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #98C379\">&#39;,&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">        <\/span><span style=\"color: #C678DD\">ORDER BY<\/span><span style=\"color: #ABB2BF\"> product_name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ) || <\/span><span style=\"color: #98C379\">&#39;]&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> products_json<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">GROUP BY<\/span><span style=\"color: #ABB2BF\"> category;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Output:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- category | products_json<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- fruits   | [&quot;apple&quot;,&quot;banana&quot;,&quot;orange&quot;]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- veggies  | [&quot;carrot&quot;]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">By understanding these patterns and potential issues, you can effectively use STRING_AGG to create meaningful string aggregations in your SQL queries. Remember to consider performance and data volume when working with large datasets.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Breakdown of STRING_AGG in SQL STRING_AGG turns multiple rows into a single concatenated string &#8211; a task that&#8217;s essential for reporting, data analysis, and API responses. Let&#8217;s explore how to use it effectively with real examples you can apply to your own databases. Basic STRING_AGG Usage Simple Concatenation Start with a basic example: Custom Separators [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":4325,"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-4324","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\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide.jpg",1280,720,false],"thumbnail":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide-150x150.jpg",150,150,true],"medium":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide-300x169.jpg",300,169,true],"medium_large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide-768x432.jpg",768,432,true],"large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide.jpg",1280,720,false],"2048x2048":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/10\/Five.Co-Using-STRING_AGG-in-SQL-A-Practical-Guide.jpg",1280,720,false]},"uagb_author_info":{"display_name":"Ryan Forrester","author_link":"https:\/\/preview-php85.dzine.org\/five\/author\/f1v5_ed9_au\/"},"uagb_comment_info":0,"uagb_excerpt":"Breakdown of STRING_AGG in SQL STRING_AGG turns multiple rows into a single concatenated string &#8211; a task that&#8217;s essential for reporting, data analysis, and API responses. Let&#8217;s explore how to use it effectively with real examples you can apply to your own databases. Basic STRING_AGG Usage Simple Concatenation Start with a basic example: Custom Separators&hellip;","_links":{"self":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/4324","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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/comments?post=4324"}],"version-history":[{"count":0,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/4324\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media\/4325"}],"wp:attachment":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media?parent=4324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/categories?post=4324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/tags?post=4324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}