{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-string-functions","conceptKey":"string-functions","subjectId":"subject-string-functions","title":"String Functions","summary":"String Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>String Functions</h2><p>String 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 String 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: String 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>Portable/common string functions</h3><ul><li><code>UPPER</code> — uppercase text</li><li><code>LOWER</code> — lowercase text</li><li><code>CHAR_LENGTH / CHARACTER_LENGTH</code> — number of characters</li><li><code>OCTET_LENGTH</code> — number of bytes</li><li><code>TRIM</code> — remove leading/trailing characters</li><li><code>LTRIM</code> — remove leading whitespace</li><li><code>RTRIM</code> — remove trailing whitespace</li><li><code>SUBSTRING / SUBSTR</code> — extract part of text</li><li><code>POSITION</code> — find substring position</li><li><code>REPLACE</code> — replace matching text</li><li><code>CONCAT</code> — join values</li><li><code>NULLIF</code> — return NULL when values match</li><li><code>COALESCE</code> — first non-null value</li></ul><h3>MySQL string functions</h3><ul><li><code>LENGTH</code> — byte length</li><li><code>LOCATE / INSTR</code> — find substring</li><li><code>LEFT / RIGHT</code> — take characters from an end</li><li><code>LPAD / RPAD</code> — pad text</li><li><code>REPEAT</code> — repeat text</li><li><code>REVERSE</code> — reverse text</li><li><code>SPACE</code> — produce spaces</li><li><code>STRCMP</code> — compare strings</li><li><code>FIND_IN_SET</code> — find value in comma-separated text</li><li><code>FIELD</code> — position in a value list</li><li><code>FORMAT</code> — locale-aware number formatting</li><li><code>GROUP_CONCAT</code> — aggregate strings</li><li><code>REGEXP_REPLACE / REGEXP_SUBSTR</code> — regular-expression operations</li></ul><h3>PostgreSQL string functions</h3><ul><li><code>LENGTH</code> — character length</li><li><code>STRPOS</code> — find substring</li><li><code>LEFT / RIGHT</code> — take characters from an end</li><li><code>LPAD / RPAD</code> — pad text</li><li><code>REPEAT</code> — repeat text</li><li><code>REVERSE</code> — reverse text</li><li><code>SPLIT_PART</code> — select a delimited part</li><li><code>STRING_TO_ARRAY</code> — split into an array</li><li><code>ARRAY_TO_STRING</code> — join an array</li><li><code>STRING_AGG</code> — aggregate strings</li><li><code>FORMAT</code> — formatted text</li><li><code>INITCAP</code> — capitalize words</li><li><code>REGEXP_REPLACE / REGEXP_MATCH</code> — regular-expression operations</li><li><code>TRANSLATE</code> — character-by-character replacement</li><li><code>ASCII / CHR</code> — character code conversion</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><h3>String Functions: MySQL and PostgreSQL</h3><p>Prefer standard SQL functions when portability matters. MySQL and PostgreSQL differ most in date formatting, type conversion, string aggregation, and error handling for invalid values.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-string-functions-c5b61152","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-string-functions","conceptKey":"string-functions","subjectId":"subject-string-functions","title":"String Functions","summary":"String Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>String Functions</h2><p>String 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 String 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: String 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>Portable/common string functions</h3><ul><li><code>UPPER</code> — uppercase text</li><li><code>LOWER</code> — lowercase text</li><li><code>CHAR_LENGTH / CHARACTER_LENGTH</code> — number of characters</li><li><code>OCTET_LENGTH</code> — number of bytes</li><li><code>TRIM</code> — remove leading/trailing characters</li><li><code>LTRIM</code> — remove leading whitespace</li><li><code>RTRIM</code> — remove trailing whitespace</li><li><code>SUBSTRING / SUBSTR</code> — extract part of text</li><li><code>POSITION</code> — find substring position</li><li><code>REPLACE</code> — replace matching text</li><li><code>CONCAT</code> — join values</li><li><code>NULLIF</code> — return NULL when values match</li><li><code>COALESCE</code> — first non-null value</li></ul><h3>MySQL string functions</h3><ul><li><code>LENGTH</code> — byte length</li><li><code>LOCATE / INSTR</code> — find substring</li><li><code>LEFT / RIGHT</code> — take characters from an end</li><li><code>LPAD / RPAD</code> — pad text</li><li><code>REPEAT</code> — repeat text</li><li><code>REVERSE</code> — reverse text</li><li><code>SPACE</code> — produce spaces</li><li><code>STRCMP</code> — compare strings</li><li><code>FIND_IN_SET</code> — find value in comma-separated text</li><li><code>FIELD</code> — position in a value list</li><li><code>FORMAT</code> — locale-aware number formatting</li><li><code>GROUP_CONCAT</code> — aggregate strings</li><li><code>REGEXP_REPLACE / REGEXP_SUBSTR</code> — regular-expression operations</li></ul><h3>PostgreSQL string functions</h3><ul><li><code>LENGTH</code> — character length</li><li><code>STRPOS</code> — find substring</li><li><code>LEFT / RIGHT</code> — take characters from an end</li><li><code>LPAD / RPAD</code> — pad text</li><li><code>REPEAT</code> — repeat text</li><li><code>REVERSE</code> — reverse text</li><li><code>SPLIT_PART</code> — select a delimited part</li><li><code>STRING_TO_ARRAY</code> — split into an array</li><li><code>ARRAY_TO_STRING</code> — join an array</li><li><code>STRING_AGG</code> — aggregate strings</li><li><code>FORMAT</code> — formatted text</li><li><code>INITCAP</code> — capitalize words</li><li><code>REGEXP_REPLACE / REGEXP_MATCH</code> — regular-expression operations</li><li><code>TRANSLATE</code> — character-by-character replacement</li><li><code>ASCII / CHR</code> — character code conversion</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><h3>String Functions: MySQL and PostgreSQL</h3><p>Prefer standard SQL functions when portability matters. MySQL and PostgreSQL differ most in date formatting, type conversion, string aggregation, and error handling for invalid values.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-string-functions-c5b61152","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}