'The Query is not working when I am adding a CTE command to it , although the Inside Select Query is giving the coorect result

enter image description here

In First Image Only Select Statement is run , it works fine .

but In Second Image when we added CTE and executed the query , it shows error.

Can you please help me with this ???



Solution 1:[1]

It says Incorrect syntax near ';' because you have a WITH statement ended with a ; and no further SELECT after it.

The query itself is fine, that's why it works up top. But when using WITH, you have to follow it up with more querying.

Delete the exiting ; and ddd SELECT * FROM cte1; after it.

Solution 2:[2]

A CTE generate a image of a table, so you need to make a select afterwards as well.

with cte1 (...) select * from cte1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mike.k
Solution 2 Rodrigo Cava