Complete the code to define a CTE named 'cte_example' that selects all columns from 'employees'.
WITH cte_example AS (SELECT * FROM [1]) SELECT * FROM cte_example;The CTE 'cte_example' selects all columns from the 'employees' table. Using 'employees' in the FROM clause is correct.
Complete the code to prevent CTE materialization by using the 'MATERIALIZED' or 'NOT MATERIALIZED' keyword.
WITH cte_data AS [1] (SELECT id, name FROM employees) SELECT * FROM cte_data;Using 'NOT MATERIALIZED' tells PostgreSQL to inline the CTE instead of materializing it.
Fix the error in the query by choosing the correct keyword to force materialization of the CTE.
WITH cte_sales AS [1] (SELECT * FROM sales WHERE amount > 100) SELECT * FROM cte_sales;
The 'MATERIALIZED' keyword forces PostgreSQL to store the CTE result temporarily, which can be useful for performance reasons.
Fill both blanks to write a CTE that is not materialized and filters employees with salary greater than 50000.
WITH cte_high_salary AS [1] (SELECT * FROM employees WHERE salary [2] 50000) SELECT * FROM cte_high_salary;
The CTE is declared as 'NOT MATERIALIZED' to avoid storing intermediate results. The filter uses '>' to select salaries greater than 50000.
Fill all three blanks to create a materialized CTE that selects project names and filters projects started after 2020.
WITH cte_projects AS [1] (SELECT name AS [2] FROM projects WHERE start_date [3] '2020-12-31') SELECT * FROM cte_projects;
The CTE is forced to materialize with 'MATERIALIZED'. The column 'name' is renamed to 'project_name'. The filter uses '>' to select projects started after December 31, 2020.