SQL CTE, ROW_NUMBER(), and Subqueries Flashcards

From GPT

1
Q

Q: What does CTE stand for in SQL?

A

A: Common Table Expression.

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

Q: What keyword is used to define a CTE?

A

A: WITH.

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

Q: True or False: A CTE is a temporary result set that can only be used within the query it’s defined in.

A

A: True.

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

Q: How do you structure a basic CTE in SQL?

A

WITH CTE_name AS (
SELECT column1, column2, …
FROM table_name
WHERE conditions
)
SELECT column1, column2
FROM CTE_name;

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

Q: True or False: A CTE can reference itself in a recursive query.

A

A: True.

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

Q: What is the purpose of the ROW_NUMBER() function in SQL?

A

A: It assigns a unique, sequential integer to rows within a partition of a result set.

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

Q: What SQL clause is required with ROW_NUMBER() to define how the rows should be numbered?

A

A: ORDER BY.

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

Q: What is the purpose of the PARTITION BY clause with ROW_NUMBER()?

A

A: It divides the result set into partitions, and the row numbers reset for each partition.

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

Q: Write a SQL query using ROW_NUMBER() to rank employees by salary in each department.

A

SELECT department, name, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
FROM employees;

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

Q: True or False: A subquery is a query embedded within another SQL query.

A

A: True.

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

Q: Name three places where you can use a subquery in SQL.

A

A: In the SELECT, FROM, and WHERE clauses.

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

Q: What’s a scalar subquery?

A

A: A subquery that returns a single value, often used in the SELECT or WHERE clause.

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

Q: Write an example of a subquery to find employees with salaries above the average salary.

A

SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

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

Q: What is a correlated subquery?

A

A: A subquery that references columns from the outer query, recalculating for each row in the outer query.

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

Q: True or False: A correlated subquery may run multiple times, impacting performance.

A

A: True.

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

Q: How do you find the top 3 salaries in each department using a CTE and ROW_NUMBER()?

A

WITH RankedEmployees AS (
SELECT department, name, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
FROM employees
)
SELECT department, name, salary
FROM RankedEmployees
WHERE row_num <= 3;

17
Q

Q: In what scenario would you use a subquery in the FROM clause?

A

A: When you need to treat the result of the subquery as a temporary table for further queries.

18
Q

Q: Explain the purpose of a table subquery.

A

A: A table subquery returns multiple rows and columns and is often used in the FROM clause as a temporary result set.

19
Q

Q: Write a SQL query with a correlated subquery that finds employees who earn more than the average salary of their department.

A

SELECT name, salary
FROM employees AS e1
WHERE salary > (SELECT AVG(salary) FROM employees AS e2 WHERE e2.department_id = e1.department_id);

20
Q

Q: What’s one major benefit of using CTEs over subqueries?

A

A: CTEs improve readability by allowing you to break down complex queries into smaller, manageable parts and reuse them in the main query.