Fix "missing FROM-clause entry for table" in PostgreSQL
ERROR: missing FROM-clause entry for table "c"
Your query mentions a table or alias, like c.name, but that table was never brought into the query with FROM or JOIN. Postgres only knows about tables you've listed, so it has nowhere to look up that column.
Why it happens
- You copied a column list that uses c. or o. prefixes but forgot to add the JOIN.
- The alias in SELECT doesn't match the one in FROM, like cust.name with FROM customers c.
- An UPDATE or DELETE that refers to a second table without a FROM or USING clause.
How to fix it
1. Join the table you're using
Fails
SELECT c.name, o.amount FROM orders o WHERE o.amount > 200;
Works
SELECT c.name, o.amount FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.amount > 200;
Every alias you use must be defined in FROM or JOIN. Here c needs a JOIN to customers.
2. Use the alias you actually defined
Fails
SELECT cust.name, o.order_date FROM customers c JOIN orders o ON o.customer_id = c.id;
Works
SELECT c.name, o.order_date FROM customers c JOIN orders o ON o.customer_id = c.id;
Once you give a table an alias, use that alias everywhere in the query. Even the full table name (customers.name) stops working after FROM customers c.
3. Add FROM to an UPDATE that uses another table
Fails
UPDATE customers SET active_status = 'churned' WHERE orders.customer_id = customers.id AND orders.status = 'refunded';
Works
UPDATE customers c SET active_status = 'churned' FROM orders o WHERE o.customer_id = c.id AND o.status = 'refunded'; SELECT id, name, active_status FROM customers ORDER BY id;
Postgres UPDATE takes extra tables in a FROM clause (DELETE uses USING). A WHERE EXISTS (SELECT 1 FROM orders ...) subquery works too.
Try it yourself
This is the broken query, running on real PostgreSQL in your browser. Press Run to see the error explained, then fix it.
Starting Postgres in your browser…
Real Postgres in your browser. Edit the query and run it, or open it in the full playground.
Practice so it doesn't happen in an interview
- Orders with customer namesINNER JOIN
- Every user and their planINNER JOIN vs LEFT JOIN
- Earning more than their managerSelf-join
- Standardize country namesUPDATE · INITCAP · TRIM