Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
MySQL CTE not referenced in WHERE clause

I am having trouble understanding why a CTE can't be referenced from a WHERE clause. In the following code, the first query works fine, but in the second query, simply replacing the value in the WHERE clause with a CTE which calculates that value, produces the error "Unknown column 'cte' in 'where clause'".

# This query works

SELECT * FROM stock_prices
WHERE Country_Exchange_Code = 'T' AND Stock_Code IN ('1332');


# But this query produces an error

WITH
    cte AS (SELECT '1332')    
SELECT * FROM stock_prices
WHERE Country_Exchange_Code = 'T' AND Stock_Code IN (cte);


# Here is some data to test these two queries with

CREATE TABLE stock_prices (Country_Exchange_Code VARCHAR(2), Stock_Code VARCHAR(4), Date DATE, Close FLOAT);
INSERT INTO stock_prices VALUES
("T", "1301",   '2019-10-29',   75.2),
("T", "1301",   '2019-10-30',   76.6),
("T", "1301",   '2019-10-31',   77.6),
("T", "1301",   '2019-11-01',   77.2),
("T", "1332",   '2019-10-29',   52.5),
("T", "1332",   '2019-10-30',   49.7),
("T", "1332",   '2019-10-31',   50.8),
("T", "1332",   '2019-11-01',   50.4),
("T", "1333",   '2019-10-29',   13.9),
("T", "1333",   '2019-10-30',   13.8),
("T", "1333",   '2019-10-31',   14.3),
("T", "1333",   '2019-11-01',   14.4);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You may refer to cte or another table by the name (not using a subselect from it) using TABLE keyword.

WITH
    cte AS (SELECT '1332')    
SELECT * FROM stock_prices
WHERE Country_Exchange_Code = 'T' AND Stock_Code IN (TABLE cte);

fiddle

over 4 years ago · Santiago Trujillo Report

0

You can do:

WITH
    cte AS (SELECT '1332' as code)    
SELECT * FROM stock_prices
WHERE Country_Exchange_Code = 'T' AND Stock_Code IN (select code from cte);

Result:

 Country_Exchange_Code  Stock_Code  Date        Close 
 ---------------------- ----------- ----------- ----- 
 T                      1332        2019-10-29  52.5  
 T                      1332        2019-10-30  49.7  
 T                      1332        2019-10-31  50.8  
 T                      1332        2019-11-01  50.4  

Your query had two issues:

  • IN requires a formal subquery with the form SELECT ...
  • The cte needed named columns; that why I added as code.

See running example at DB Fiddle.

over 4 years ago · Santiago Trujillo Report

0

It sounds like you are thinking of a cte as some kind of variable instead of thinking of it as a virtual table.

You can't do IN (cte) just like you can't do IN (sometablename); it needs to be IN (select somecol from sometablename).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!