---
title: "How To Use Common Table Expressions"  
description: "How To Use Common Table Expressions"  
author: "Anonymous User"  
published: 2016-03-09  
updated: 2016-03-09  
canonical: https://www.mindstick.com/forum/34046/how-to-use-common-table-expressions  
category: "database"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How To Use Common Table Expressions

We want to To Use [Common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) [Table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) Expressions. How will do this please help me.

## Replies

### Reply by Anonymous User

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.

```
 CREATE TABLE [CUSTOMER](	[CUST_ID] [bigint] IDENTITY(1,1)PRIMARY KEY,	[CUST_NAME] [varchar](200) NULL,	[CUST_EMAILID] [varchar](100) NULL,	[CUST_PHONE] [varchar](20) NULL,	[CUST_ADDRESS] [varchar](800) NULL)  SELECT * FROM [CUSTOMER] 
```

```
CREATE TABLE [ORDER](
	[ORD_ID] [bigint] IDENTITY(1,1) PRIMARY KEY,	[CUST_ID] [bigint] NULL,	[PRODUCT_ID] [bigint] NULL,	[ORD_DATE] [date] NULL )
 SELECT * FROM [ORDER] 
```

```

CREATE TABLE [PRODUCT](
	[PRODUCT_ID] [bigint] IDENTITY(1,1) PRIMARY KEY,
	[PRODUCT_NAME] [varchar](200) NULL,
	[PRICE] [decimal](10, 2) NULL
)

SELECT * FROM [PRODUCT]
 
　
```

```
;With CTE(ORD_ID, ORD_DATE, CUST_NAME,CUST_EMAILID,CUST_PHONE,PRODUCT_NAME,PRICE)      AS	   (		 SELECT O.ORD_ID,O.ORD_DATE,C.CUST_NAME,C.CUST_EMAILID,C.CUST_PHONE,P.PRODUCT_NAME,P.PRICE FROM
				[ORDER] AS [O] INNER JOIN CUSTOMER AS [C] ON O.CUST_ID=C.CUST_ID
				INNER JOIN PRODUCT AS [P] ON P.PRODUCT_ID=O.PRODUCT_ID
	   )	   SELECT * FROM CTE WHERE PRODUCT_NAME='COMPUTER' --Using CTE 
```


---

Original Source: https://www.mindstick.com/forum/34046/how-to-use-common-table-expressions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
