Question: I have an MySQL statement that performs an inner `SELECT` and returns the result as a pseudo column. I’d like to use the result of this pseudo column in my `WHERE` clause. My current SQL statement looks like this:

```
SELECT
    product.product_id,
    product.range_id,
    product.title,
    product.image,
    product.image_text,
    product.friendly_url,
    attribute.comfort_grade_id,
    category.category_id,
    category.category AS category_name,
    category.friendly_url AS category_friendly_url,
    (SELECT price_now FROM product_bedding_sizes AS size
        WHERE size.product_id = product.product_id
        ORDER BY size.price_now ASC LIMIT 1) AS price
FROM
    products AS product
LEFT JOIN
    categories AS category ON product.category_id = category.category_id
LEFT JOIN
    product_bedding_attributes AS attribute
         ON product.product_id = attribute.product_id
$where
$order
LIMIT
    ?,?

```

However, I get the following error message when running the query:

> 
> #1054 - Unknown column 'price' in 'where clause'
> 
> 
> 

How can I get around this and actually use the value of `price` in my `WHERE` clause?

Answer: 
The `WHERE` clause is evaluated before the `SELECT` clause, so it doesn't say the alias name. You have to do the filter by the `WHERE` clause in an outer query like this:

```
SELECT *
FROM
(
    SELECT
        product.product_id,
        product.range_id,
        product.title,
        product.image,
        product.image_text,
        product.friendly_url,
        attribute.comfort_grade_id,
        category.category_id,
        category.category AS category_name,
        category.friendly_url AS category_friendly_url,
        (SELECT price_now 
         FROM product_bedding_sizes AS size 
         WHERE size.product_id = product.product_id 
         ORDER BY size.price_now ASC 
         LIMIT 1) AS price
    FROM
     ...
) AS sub
WHERE price = ... <--- here it can see the price alias.

```

See this for more details:

* [**My SQL Query Order of Operations**](http://www.bennadel.com/blog/70-SQL-Query-Order-of-Operations.htm).

---

**Or:** You can join that table, instead of a correlated subquery like this:

```
SELECT
    product.product_id,
    product.range_id,
    product.title,
    product.image,
    product.image_text,
    product.friendly_url,
    attribute.comfort_grade_id,
    category.category_id,
    category.category AS category_name,
    category.friendly_url AS category_friendly_url,
    size.price_now 
FROM
    products AS product
LEFT JOIN
(
   SELECT product_id, MIN(price_now) AS price
   FROM product_bedding_sizes
   GROUP BY product_id
) AS size ON size.product_id = product.product_id
LEFT JOIN
    categories AS category ON product.category_id = category.category_id
LEFT JOIN
    product_bedding_attributes AS attribute ON product.product_id = attribute.product_id
$where price = ----;

```
Try using a variable:

```
@price:= (SELECT price_now 
          FROM product_bedding_sizes AS size 
          WHERE size.product_id = product.product_id 
          ORDER BY size.price_now ASC LIMIT 1) AS price;

```

Then reference it as

```
WHERE @price > 9000;

```