WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

SQL & Databases · Intermediate · question 29 of 100

How do you use common table expressions (CTEs) in SQL, and what benefits do they provide?

📕 Buy this interview preparation book: 100 SQL & Databases questions & answers — PDF + EPUB for $5

A common table expression (CTE) is a temporary named result set that is defined within the context of a single SQL statement. CTEs are similar to subqueries, but provide some additional benefits, such as improved readability and reusability of the code.

Here is an example of a simple CTE:

WITH sales_data AS (
  SELECT *
  FROM sales
  WHERE sales_date BETWEEN '2022-01-01' AND '2022-12-31'
)
SELECT product_name, SUM(sales_amount) AS total_sales
FROM sales_data
GROUP BY product_name;

In this example, a CTE named "sales_data" is defined to retrieve all sales data for the year 2022. The CTE is then used in the main query to group the sales data by product name and calculate the total sales amount.

The benefits of using CTEs include:

Improved readability: By defining complex or frequently used subqueries as CTEs, the main query becomes more readable and easier to understand.

Code reuse: CTEs can be defined once and then reused in multiple queries, making it easier to maintain and update complex SQL code.

Optimization: Depending on the database engine, CTEs can sometimes be optimized for performance, leading to faster query execution times.

Recursive queries: CTEs can also be used to define recursive queries, which can be useful for querying hierarchical data structures.

Here is an example of a recursive CTE:

WITH RECURSIVE manager_hierarchy AS (
  SELECT employee_id, employee_name, manager_id, 0 AS depth
  FROM employee
  WHERE manager_id IS NULL
  UNION ALL
  SELECT e.employee_id, e.employee_name, e.manager_id, 
  mh.depth + 1 AS depth
  FROM employee e
  JOIN manager_hierarchy mh ON e.manager_id = mh.employee_id
)
SELECT employee_name, depth
FROM manager_hierarchy;

In this example, a recursive CTE named "manager_hierarchy" is defined to retrieve information about the hierarchical relationships between employees and managers. The CTE uses a UNION ALL operator to combine the results of the base query (which retrieves the top-level managers) with the results of the recursive query (which retrieves the employees who report to each manager). The resulting query retrieves the name of each employee and their depth in the hierarchy.

Overall, CTEs are a powerful tool for improving the readability and reusability of SQL code, and can be used in a variety of contexts to optimize query performance and simplify complex data manipulations.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic SQL & Databases interview — then scores it.
📞 Practice SQL & Databases — free 15 min
📕 Buy this interview preparation book: 100 SQL & Databases questions & answers — PDF + EPUB for $5

All 100 SQL & Databases questions · All topics