Data Manipulation 6 - Complex Queries - Flashcards

1
Q

What is a complex query in SQL?

A

A complex query involves multiple steps, conditions, or logical operations to manipulate or retrieve data, handling intricate data relationships, performing conditional logic, and aggregating data across multiple tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some keywords and concepts used in constructing complex queries?

A
  1. CASE
  2. EXISTS
  3. WINDOW FUNCTION
  4. WITH (CTE)
  5. RECURSIVE QUERY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for a CASE statement in SQL?

A

CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE resultN
END

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the EXISTS operator work in SQL?

A

EXISTS checks for the existence of any record in a subquery and returns true if the subquery finds one or more matching rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Common Table Expression (CTE) in SQL?

A

A CTE allows the creation of temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement, making complex queries more readable and modular.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a window function in SQL?

A

A window function performs calculations across a set of table rows that are related to the current row, useful for running totals, moving averages, and other complex statistical calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does a recursive query work in SQL?

A

Recursive queries handle hierarchical or tree-structured data by calling themselves repeatedly, processing rows in a loop until a certain condition is met, using the WITH RECURSIVE syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide an example of using the CASE statement to assign a salary category.

A

SELECT staff_name, staff_last_name, staff_salary,
CASE
WHEN staff_salary > 50000 THEN ‘High’
WHEN staff_salary BETWEEN 40000 AND 50000 THEN ‘Medium’
ELSE ‘Low’
END AS Salary_Category
FROM staff;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you use EXISTS and CASE together in a query?

A

SELECT s.staff_name, d.dept_name,
CASE
WHEN EXISTS (
SELECT 1
FROM project p
WHERE p.dept_id = s.dept_id AND p.budget > 75000
) THEN ‘Involved in High-Budget Project’
ELSE ‘Not Involved in High-Budget Project’
END AS Project_involvement
FROM staff s
JOIN department d ON s.dept_id = d.dept_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the advantage of using CTEs in SQL?

A

CTEs improve query readability and modularity by breaking down complex queries into simpler parts, which can make maintenance and debugging easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Provide an example of a recursive query in SQL.

A

WITH RECURSIVE ManagerHierarchy AS (
SELECT s.staff_id, s.staff_name, s.manager_id, 1 AS level
FROM staff s
WHERE s.manager_id IS NULL
UNION ALL
SELECT s.staff_id, s.staff_name, s.manager_id, mh.level + 1
FROM staff s
JOIN ManagerHierarchy mh ON s.manager_id = mh.staff_id
)
SELECT mh.staff_id, mh.staff_name, m.staff_name AS manager_name, mh.level
FROM ManagerHierarchy mh
LEFT JOIN staff m ON mh.manager_id = m.staff_id
ORDER BY mh.level, mh.staff_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a window function’s OVER clause used for?

A

The OVER clause specifies how to partition the data, allowing calculations across sets of rows related to the current query row without collapsing the result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does PostgreSQL handle CTEs in terms of optimization?

A

PostgreSQL historically treated CTEs as optimization fences, executing them separately from the main query. From version 12 onwards, the planner can inline CTEs when possible, reducing the performance difference compared to subqueries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the performance considerations when choosing between CTEs and subqueries?

A
  1. Readability vs. Execution Plan
  2. Optimization capabilities
  3. Materialization overhead
  4. Specific use case and PostgreSQL version
How well did you know this?
1
Not at all
2
3
4
5
Perfectly