Mastering Long SQL Queries

πŸ“œ Mastering Long SQL Queries: Write Like a Pro! πŸš€

Writing long SQL queries can be daunting, but with the right techniques and functions, you can craft efficient, readable, and powerful queries. Whether you’re working with MySQL, PostgreSQL, SQL Server, or Oracle, mastering these key functions will take your SQL skills to the next level.

sql-flowchart


πŸ”Ÿ Essential SQL Functions for Long Queries

Here are 10 powerful functions (available in major databases) that can simplify complex queries:

1. Window Functions (OVER, PARTITION BY) – PostgreSQL, SQL Server, Oracle

πŸ“Œ Use Case: Calculate running totals, rankings, or moving averages.

SELECT 
    employee_id, 
    salary, 
    AVG(salary) OVER (PARTITION BY department_id) AS avg_dept_salary
FROM employees;

2. Common Table Expressions (CTEs) – PostgreSQL, SQL Server, MySQL (8.0+), Oracle

πŸ“Œ Use Case: Break complex queries into readable chunks.

WITH high_earners AS (
    SELECT * FROM employees WHERE salary > 100000
)
SELECT * FROM high_earners WHERE department = 'Engineering';

3. STRING_AGG / GROUP_CONCAT – PostgreSQL (STRING_AGG), MySQL (GROUP_CONCAT)

πŸ“Œ Use Case: Concatenate strings from multiple rows.

-- PostgreSQL
SELECT department_id, STRING_AGG(name, ', ') AS employees 
FROM employees 
GROUP BY department_id;

-- MySQL
SELECT department_id, GROUP_CONCAT(name SEPARATOR ', ') AS employees 
FROM employees 
GROUP BY department_id;

4. PIVOT / CROSSTAB – SQL Server, Oracle, PostgreSQL (crosstab)

πŸ“Œ Use Case: Transform rows into columns for reports.

-- SQL Server
SELECT * FROM (
    SELECT year, product, sales FROM sales_data
) AS src
PIVOT (SUM(sales) FOR product IN ([Laptop], [Phone], [Tablet])) AS pvt;

5. COALESCE / NULLIF – All Databases

πŸ“Œ Use Case: Handle NULL values gracefully.

SELECT 
    name, 
    COALESCE(bonus, 0) AS bonus -- Replace NULL with 0
FROM employees;

6. LAG / LEAD – PostgreSQL, SQL Server, Oracle

πŸ“Œ Use Case: Compare current row with previous/next row.

SELECT 
    date, 
    revenue, 
    LAG(revenue, 1) OVER (ORDER BY date) AS prev_day_revenue
FROM daily_sales;

7. JSON Functions – PostgreSQL, MySQL (5.7+), SQL Server (2016+)

πŸ“Œ Use Case: Query and manipulate JSON data.

-- PostgreSQL
SELECT 
    user_id, 
    json_data->>'email' AS email 
FROM users 
WHERE json_data->>'status' = 'active';

8. Dynamic SQL (EXECUTE) – SQL Server, PostgreSQL, Oracle

πŸ“Œ Use Case: Build queries dynamically for flexibility.

-- SQL Server
DECLARE @sql NVARCHAR(MAX) = 'SELECT * FROM ' + @table_name;
EXEC sp_executesql @sql;

9. FILTER Clause (PostgreSQL)

πŸ“Œ Use Case: Apply aggregate functions conditionally.

SELECT 
    department_id,
    AVG(salary) FILTER (WHERE tenure > 5) AS avg_senior_salary
FROM employees
GROUP BY department_id;

10. Recursive CTEs – PostgreSQL, SQL Server, Oracle

πŸ“Œ Use Case: Query hierarchical data (org charts, folders).

WITH RECURSIVE org_tree AS (
    SELECT id, name, manager_id FROM employees WHERE id = 1 -- CEO
    UNION ALL
    SELECT e.id, e.name, e.manager_id 
    FROM employees e
    JOIN org_tree ot ON e.manager_id = ot.id
)
SELECT * FROM org_tree;

πŸ’‘ Pro Tips for Writing Long SQL Queries Efficiently

βœ… Use CTEs for Readability – Break down complex logic into named blocks.
βœ… Format Consistently – Indent, align, and comment for clarity.
βœ… Optimize Joins – Use EXPLAIN to check query performance.
βœ… Avoid SELECT *** – Fetch only necessary columns.
βœ… **Index Strategically
– Ensure joins and WHERE clauses use indexes.
βœ… Test Incrementally – Build your query step-by-step.
βœ… Use Temporary Tables – For extremely complex queries, stage intermediate results.


🎯 Final Thoughts

Writing long SQL queries doesn’t have to be painful! By leveraging CTEs, window functions, and advanced aggregation, you can write clean, efficient, and maintainable SQL.

πŸ”Ή Which SQL function do you use most often? Drop a comment below! πŸ‘‡

#SQL #Database #DataEngineering #DataScience #TechTips πŸš€

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.