Five SQL Tricks Every DBA Should Know
Five SQL Tricks Every DBA Should Know
Asfaw Gedamu
Tired of writing complex loops to concatenate strings? LISTAGG is your new best friend! It
aggregates strings into a single value, perfect for reporting.
Example 1:
1. SELECT
2. CustomerID,
3. LISTAGG(ProductID, ', ') WITHIN GROUP (ORDER BY ProductID) AS Products
4. FROM
5. OrderDetails
6. JOIN
7. Orders ON OrderDetails.OrderID = Orders.OrderID
8. GROUP BY
9. CustomerID;
10.
Output:
1. | CustomerID | Products |
2. |------------|------------------------|
3. | 101 | 201, 202, 203 |
4. | 102 | 201 |
5. | 103 | 202 |
6. | 104 | 201 |
7. | 105 | 204 |
8. | 106 | 205 |
9. | 107 | 201 |
10. | 108 | 202 |
11. | 109 | 203 |
12. | 110 | 204 |
13.
Why It Helps:
Example 2:
1. SELECT department_id,
2. LISTAGG(employee_name, ', ') WITHIN GROUP (ORDER BY employee_name) AS employee_names
3. FROM employees
4. GROUP BY department_id;
5.
1 Alice 10
2 Bob 10
3 Charlie 20
employee_id employee_name department_id
4 David 20
Output Data
department_id employee_names
10 Alice, Bob
20 Charlie, David
Benefits
Complex queries can be a nightmare to maintain. The WITH clause breaks them into
manageable chunks, improving readability and performance.
CTEs simplify querying hierarchical data by allowing recursion. This is particularly useful for
organizational structures.
Example 1:
1. WITH CustomerSales AS (
2. SELECT
3. CustomerID,
4. SUM(TotalAmount) AS TotalSales
5. FROM
6. Orders
7. GROUP BY
8. CustomerID
9. )
10. SELECT
11. CustomerID,
12. TotalSales
13. FROM
14. CustomerSales
15. WHERE
16. TotalSales > 200;
17.
Output:
1. | CustomerID | TotalSales |
2. |------------|------------|
3. | 101 | 500.00 |
4. | 104 | 250.00 |
5. | 105 | 400.00 |
6. | 107 | 300.00 |
7. | 108 | 200.00 |
8. | 110 | 500.00 |
9.
Why It Helps:
Example 2:
Summary of Changes
- The output shows the complete hierarchy starting from the top-level manager (Alice)
down to her direct reports (Bob and Charlie), and then to their reports (David and Emily).
Why write separate INSERT and UPDATE statements when you can do both in one go?
MERGE is a game-changer for data synchronization.
The `MERGE` statement performs an "upsert" operation (update if exists, insert if not) in a
single query.
Example 1:
Output:
- Updates `CustomerID = 101` with the new name `John Doe Updated`.
- Inserts a new row for `CustomerID = 111` with the name `Ivy Clark`.
Why It Helps:
Example 2.
MERGE Statement
1. MERGE INTO employees e
2. USING (SELECT * FROM new_employees) n
3. ON (e.employee_id = n.employee_id)
4. WHEN MATCHED THEN
5. UPDATE SET e.salary = n.salary
6. WHEN NOT MATCHED THEN
7. INSERT (employee_id, first_name, last_name, salary)
8. VALUES (n.employee_id, n.first_name, n.last_name, n.salary);
9.
Summary of Changes
• Employees with employee_id 1 and 2 had their salaries updated from 70000 to
75000 and 80000, respectively.
• Two new employees (Mark Taylor and Alice Green) were inserted into the table.
Need to generate summary reports? PIVOT transforms rows into columns, making your life
so much easier to generate summary reports.
Output:
Why It Helps:
1. BEGIN
2. DBMS_PARALLEL_EXECUTE.CREATE_TASK('apply_discount_task');
3.
4. DBMS_PARALLEL_EXECUTE.CREATE_CHUNKS_BY_ROWID(
5. TASK_NAME => 'apply_discount_task',
6. TABLE_OWNER => 'YOUR_SCHEMA',
7. TABLE_NAME => 'Orders',
8. BY_ROW => TRUE,
9. CHUNK_SIZE => 100
10. );
11.
12. DBMS_PARALLEL_EXECUTE.RUN_TASK(
13. TASK_NAME => 'apply_discount_task',
14. SQL_STATEMENT => 'UPDATE Orders SET TotalAmount = TotalAmount * 0.9 WHERE rowid BETWEEN
:start_id AND :end_id',
15. LANGUAGE_FLAG => DBMS_SQL.NATIVE
16. );
17.
18. DBMS_PARALLEL_EXECUTE.DROP_TASK('apply_discount_task');
19. END;
20.
Output:
Why It Helps:
2. Improves Query Readability: `WITH` clause and `MERGE` make queries easier to
understand and maintain.
The MERGE statement consolidates updates and inserts into one operation, while the
recursive CTE effectively reveals hierarchical relationships within employee data. Both
techniques enhance the efficiency and clarity of database management tasks for DBAs.
4. Reduces Code Complexity: These tricks eliminate the need for verbose PL/SQL or
external scripts.
Let me know if you need further clarification! If you found these tips helpful, give this post a
and share it with your network!