Hidden SQL Functions

πŸ•΅οΈβ€β™‚οΈ Hidden SQL Functions You Probably Didn’t Know β€” But Should! πŸš€

When it comes to SQL, most developers stick to the basics: SELECT, JOIN, GROUP BY, maybe a CASE statement here and there. But SQL hides some powerhouse functions that can drastically simplify your queries, speed up performance, and make your code cleaner.

Today, we’ll uncover hidden gems across popular databases like MySQL, PostgreSQL, SQL Server, and Oracle β€” with examples, best use cases, and optimization tips. πŸ’‘

SQL


πŸ”Ή 1. GROUP_CONCAT() β€” Merge Rows into a Single String

πŸ“¦ Available in: MySQL, MariaDB (PostgreSQL uses STRING_AGG)

πŸ” What it does: Merges multiple rows into a single, comma-separated (or custom delimiter) string.

πŸ’» Example (MySQL):

SELECT department_id, GROUP_CONCAT(employee_name ORDER BY employee_name SEPARATOR ', ')
FROM employees
GROUP BY department_id;

πŸ›  Output:

department_id employee_names
1 Alice, Bob, Charlie
2 David, Eva

🎯 Best use case:

  • Generating readable reports
  • Exporting aggregated data without doing joins in application code

πŸ”Ή 2. STRING_AGG() β€” PostgreSQL’s Answer to GROUP_CONCAT

πŸ“¦ Available in: PostgreSQL, SQL Server, Oracle 19c+

πŸ’» Example (PostgreSQL):

SELECT department_id, STRING_AGG(employee_name, ', ' ORDER BY employee_name)
FROM employees
GROUP BY department_id;

🎯 Best use case:

  • Same as GROUP_CONCAT() but in databases that don’t support it
  • Can handle ordering and custom delimiters efficiently

πŸ”Ή 3. LAG() & LEAD() β€” Look Behind and Ahead in Data

πŸ“¦ Available in: PostgreSQL, MySQL 8+, SQL Server, Oracle

πŸ” What they do: Allow you to access the previous (LAG) or next (LEAD) row’s value without a self-join.

πŸ’» Example (PostgreSQL):

SELECT 
    employee_name, 
    hire_date, 
    LAG(hire_date) OVER (ORDER BY hire_date) AS prev_hire_date,
    LEAD(hire_date) OVER (ORDER BY hire_date) AS next_hire_date
FROM employees;

🎯 Best use case:

  • Calculating differences between rows
  • Trend analysis in time-series data

πŸ”Ή 4. COALESCE() β€” First Non-NULL Value Finder

πŸ“¦ Available in: MySQL, PostgreSQL, SQL Server, Oracle

πŸ’» Example:

SELECT employee_name, COALESCE(phone_number, 'No Phone') AS contact
FROM employees;

🎯 Best use case:

  • Replace NULL values with defaults
  • Useful in reporting and user-facing queries

πŸ”Ή 5. NULLIF() β€” Avoid Divide-by-Zero Errors

πŸ“¦ Available in: MySQL, PostgreSQL, SQL Server, Oracle

πŸ’» Example:

SELECT sales / NULLIF(target, 0) AS performance_ratio
FROM performance_data;

🎯 Best use case:

  • Safe mathematical operations
  • Cleaner alternative to CASE WHEN target = 0 THEN NULL ...

πŸ”Ή 6. WITH RECURSIVE β€” Hierarchical & Tree Data

πŸ“¦ Available in: PostgreSQL, MySQL 8+, SQL Server (as CTE), Oracle

πŸ’» Example (PostgreSQL):

WITH RECURSIVE employee_hierarchy AS (
    SELECT id, name, manager_id FROM employees WHERE manager_id IS NULL
    UNION ALL
    SELECT e.id, e.name, e.manager_id
    FROM employees e
    INNER JOIN employee_hierarchy eh ON e.manager_id = eh.id
)
SELECT * FROM employee_hierarchy;

🎯 Best use case:

  • Category trees
  • Employee management hierarchies

πŸ”Ή 7. RANK() & DENSE_RANK() β€” Smart Ranking Without Messy Subqueries

πŸ“¦ Available in: MySQL 8+, PostgreSQL, SQL Server, Oracle

πŸ’» Example:

SELECT 
    employee_name, department_id, 
    RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank_in_dept
FROM employees;

🎯 Best use case:

  • Leaderboards
  • Ranking performance per group

πŸ”Ή 8. JSON_TABLE() β€” Query JSON Like a Table

πŸ“¦ Available in: MySQL 8+, Oracle 12c+, SQL Server (OPENJSON), PostgreSQL (jsonb functions)

πŸ’» Example (MySQL):

SELECT * 
FROM JSON_TABLE(
  '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]',
  "$[*]" COLUMNS(
    id INT PATH "$.id",
    name VARCHAR(50) PATH "$.name"
  )
) AS jt;

🎯 Best use case:

  • Extract structured data from JSON directly in SQL
  • Useful for hybrid data storage models

🎁 Bonus Functions to Supercharge Your SQL

πŸ’‘ Performance Boosters & Time Savers

  1. EXPLAIN β€” Check query execution plan (All DBs)

    EXPLAIN SELECT * FROM employees;
    

    Use for: Optimizing queries before they hit production

  2. INDEX HINTS β€” Force specific index usage (MySQL)

    SELECT * FROM employees USE INDEX (idx_department) WHERE department_id = 5;
    
  3. FILTER() β€” Conditional aggregation (PostgreSQL)

    SELECT department_id, COUNT(*) FILTER (WHERE salary > 50000) AS high_salary_count
    FROM employees
    GROUP BY department_id;
    
  4. UPSERT (INSERT ... ON CONFLICT) β€” Insert or Update in one shot (PostgreSQL, MySQL)

    INSERT INTO employees (id, name) VALUES (1, 'Alice')
    ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;
    

πŸ† Final Thoughts

SQL is far more powerful than most developers realize. By mastering these hidden functions, you can: βœ… Write cleaner queries βœ… Avoid complex joins and subqueries βœ… Improve performance and maintainability

Remember β€” the less code you write in your app layer for data processing, the faster your app runs. πŸš€

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.