{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-json-functions","conceptKey":"json-functions","subjectId":"subject-json-functions","title":"JSON Functions","summary":"JSON Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>JSON Functions</h2><p>JSON Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>SELECT UPPER(name), ROUND(price, 2), CURRENT_DATE FROM products;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>A sales manager needs a monthly report showing revenue, order counts, rankings, and changes over time without exporting raw data to a spreadsheet.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the JSON Functions operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The query returns only the intended rows and columns, with deterministic ordering where order matters.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>SELECT category,\n       COUNT(*) AS products,\n       ROUND(AVG(price), 2) AS average_price,\n       SUM(stock) AS units_available\nFROM products\nGROUP BY category\nHAVING SUM(stock) &gt; 0;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: JSON Functions</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Standard/common JSON operations</h3><ul><li><code>JSON_OBJECT</code> — construct object</li><li><code>JSON_ARRAY</code> — construct array</li><li><code>JSON_ARRAYAGG</code> — aggregate array</li><li><code>JSON_OBJECTAGG</code> — aggregate object</li><li><code>JSON_VALUE</code> — extract scalar</li><li><code>JSON_QUERY</code> — extract object or array</li><li><code>JSON_EXISTS</code> — test path existence</li><li><code>JSON_TABLE</code> — turn JSON into relational rows; MySQL/standard support</li></ul><h3>MySQL JSON functions</h3><ul><li><code>JSON_EXTRACT / -&gt;</code> — extract path</li><li><code>-&gt;&gt;</code> — extract unquoted scalar</li><li><code>JSON_SET</code> — set or replace path</li><li><code>JSON_INSERT</code> — insert missing path</li><li><code>JSON_REPLACE</code> — replace existing path</li><li><code>JSON_REMOVE</code> — remove path</li><li><code>JSON_CONTAINS</code> — test containment</li><li><code>JSON_CONTAINS_PATH</code> — test path</li><li><code>JSON_KEYS</code> — list object keys</li><li><code>JSON_LENGTH</code> — count members</li><li><code>JSON_TYPE</code> — report JSON type</li><li><code>JSON_VALID</code> — validate JSON text</li><li><code>JSON_MERGE_PATCH</code> — merge documents</li><li><code>JSON_PRETTY</code> — pretty-print document</li><li><code>JSON_SEARCH</code> — find value paths</li></ul><h3>PostgreSQL JSON/JSONB functions and operators</h3><ul><li><code>-&gt; / -&gt;&gt;</code> — extract JSON or text</li><li><code>#&gt; / #&gt;&gt;</code> — extract by path</li><li><code>@&gt;</code> — JSONB containment</li><li><code>?</code> — ?| / ?&amp;</li><li><code>||</code> — concatenate JSONB</li><li><code>jsonb_set</code> — set path</li><li><code>jsonb_insert</code> — insert value</li><li><code>jsonb_path_query</code> — query SQL/JSON path</li><li><code>jsonb_array_elements</code> — expand array to rows</li><li><code>jsonb_each</code> — expand object to rows</li><li><code>jsonb_object_keys</code> — list keys</li><li><code>jsonb_build_object / jsonb_build_array</code> — construct values</li><li><code>jsonb_agg / jsonb_object_agg</code> — aggregate JSONB</li><li><code>jsonb_typeof</code> — report type</li><li><code>jsonb_pretty</code> — pretty-print document</li><li><code>to_jsonb</code> — convert SQL value to JSONB</li><li><code>jsonb_strip_nulls</code> — remove null object fields</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div>PostgreSQL usually favors JSONB and its operators. MySQL has a native JSON type and functions such as JSON_EXTRACT.</div></section>","detailId":"lesson-json-functions-58d846dd","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-json-functions","conceptKey":"json-functions","subjectId":"subject-json-functions","title":"JSON Functions","summary":"JSON Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>JSON Functions</h2><p>JSON Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>SELECT UPPER(name), ROUND(price, 2), CURRENT_DATE FROM products;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>A sales manager needs a monthly report showing revenue, order counts, rankings, and changes over time without exporting raw data to a spreadsheet.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the JSON Functions operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The query returns only the intended rows and columns, with deterministic ordering where order matters.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>SELECT category,\n       COUNT(*) AS products,\n       ROUND(AVG(price), 2) AS average_price,\n       SUM(stock) AS units_available\nFROM products\nGROUP BY category\nHAVING SUM(stock) &gt; 0;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: JSON Functions</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Standard/common JSON operations</h3><ul><li><code>JSON_OBJECT</code> — construct object</li><li><code>JSON_ARRAY</code> — construct array</li><li><code>JSON_ARRAYAGG</code> — aggregate array</li><li><code>JSON_OBJECTAGG</code> — aggregate object</li><li><code>JSON_VALUE</code> — extract scalar</li><li><code>JSON_QUERY</code> — extract object or array</li><li><code>JSON_EXISTS</code> — test path existence</li><li><code>JSON_TABLE</code> — turn JSON into relational rows; MySQL/standard support</li></ul><h3>MySQL JSON functions</h3><ul><li><code>JSON_EXTRACT / -&gt;</code> — extract path</li><li><code>-&gt;&gt;</code> — extract unquoted scalar</li><li><code>JSON_SET</code> — set or replace path</li><li><code>JSON_INSERT</code> — insert missing path</li><li><code>JSON_REPLACE</code> — replace existing path</li><li><code>JSON_REMOVE</code> — remove path</li><li><code>JSON_CONTAINS</code> — test containment</li><li><code>JSON_CONTAINS_PATH</code> — test path</li><li><code>JSON_KEYS</code> — list object keys</li><li><code>JSON_LENGTH</code> — count members</li><li><code>JSON_TYPE</code> — report JSON type</li><li><code>JSON_VALID</code> — validate JSON text</li><li><code>JSON_MERGE_PATCH</code> — merge documents</li><li><code>JSON_PRETTY</code> — pretty-print document</li><li><code>JSON_SEARCH</code> — find value paths</li></ul><h3>PostgreSQL JSON/JSONB functions and operators</h3><ul><li><code>-&gt; / -&gt;&gt;</code> — extract JSON or text</li><li><code>#&gt; / #&gt;&gt;</code> — extract by path</li><li><code>@&gt;</code> — JSONB containment</li><li><code>?</code> — ?| / ?&amp;</li><li><code>||</code> — concatenate JSONB</li><li><code>jsonb_set</code> — set path</li><li><code>jsonb_insert</code> — insert value</li><li><code>jsonb_path_query</code> — query SQL/JSON path</li><li><code>jsonb_array_elements</code> — expand array to rows</li><li><code>jsonb_each</code> — expand object to rows</li><li><code>jsonb_object_keys</code> — list keys</li><li><code>jsonb_build_object / jsonb_build_array</code> — construct values</li><li><code>jsonb_agg / jsonb_object_agg</code> — aggregate JSONB</li><li><code>jsonb_typeof</code> — report type</li><li><code>jsonb_pretty</code> — pretty-print document</li><li><code>to_jsonb</code> — convert SQL value to JSONB</li><li><code>jsonb_strip_nulls</code> — remove null object fields</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div>PostgreSQL usually favors JSONB and its operators. MySQL has a native JSON type and functions such as JSON_EXTRACT.</div></section>","detailId":"lesson-json-functions-58d846dd","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}