Nested Queries Flashcards

1
Q

What is a subquery in SQL?

A

A subquery, also known as a nested query, is a SQL query embedded within another SQL query. It is used to retrieve data from one or more tables and is enclosed within parentheses.

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

What are the advantages of using subqueries?

A

The advantages include:
- Complexity Management: Breaking down complex problems into simpler parts.
- Reusability: Subqueries can be reused in different parts of the outer query.
- Logical Grouping: Allows logical grouping of data retrieval in a single query.

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

What are the disadvantages of using subqueries?

A

The disadvantages include:
- Performance Issues: Can lead to performance degradation if not optimized.
- Readability: Can become difficult to read and maintain.
- Limited Flexibility: Less flexible than JOIN operations in some cases.

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

What is the difference between a subquery and a JOIN?

A

A subquery is a query nested inside another query, ideal for single-value returns. A JOIN clause combines rows from two or more tables based on a related column and is generally faster for straightforward data retrieval.

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

When is it better to use a subquery instead of a JOIN?

A

Use a subquery for single-value calculations or conditions dependent on the result of another query. Use JOINs for direct data correlations and operations involving multiple tables.

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

Provide an example of a simple subquery.

A

SELECT col_a, col_b
FROM table_1
WHERE col_b > (
SELECT AVG(col_c) FROM table_2
);

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

What is the execution order of a subquery?

A
  1. Execute the subquery to get the result.
  2. Use the subquery’s result in the outer query condition.
  3. Execute the outer query with the condition applied.
  4. Return the final result set.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a query to find staff members who earn more than the average salary.

A

SELECT staff_name, staff_salary
FROM staff
WHERE staff_salary > (
SELECT AVG(staff_salary) FROM staff
);

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

Write a query to find projects with a budget higher than the IT department’s average projects budget.

A

SELECT project_name, budget
FROM project
WHERE dept_id = (SELECT dept_id FROM department WHERE dept_name = ‘IT’)
AND budget > (
SELECT AVG(budget) FROM project
WHERE dept_id = (SELECT dept_id FROM department WHERE dept_name = ‘IT’)
);

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

When should you test both subqueries and JOINs for performance?

A

Test both methods if performance is a concern, as SQL engines optimize differently based on the query structure and database schema.

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

What should you consider when choosing between subqueries and JOINs for readability and maintenance?

A

Use the approach that makes the SQL easier to understand and maintain, especially in the context of practical sessions or company policies.

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