What is CTE in Oracle SQL
Question
Share
Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
CTE stands for Common Table Expression. Another acronym is subquery factory; It’s simply a query that you can define in another query. It starts with WITH clause that lets you assign a name to a subquery block. Then you can reference this subquery block in multiple places within your main query by giving that query a name.
Oracle database traits that query name as either an inline view or as a temporary table that will be dropped automatically after the execution of your main query is finished.
Let’s take a simple example to showcase the use of CTE in the Oracle database
Let’s say we need to get the employees that are hired on the first day of each month in the current year.
Output:
| FIREST_NAME | LAST_NAME | HIRE_DATE
| ——————- | —————- | —————-|
| Samuel | McCain | 01/07/2006 |
In the above query, we first start our query with a WITH clause to define a temporary table that has a date of the first day of each month and give that temp table or inline view a name which is “hiring_period” and then uses this name in the main query by joining it with the employee’s table using the hire date column.