AboutBlogContact
DatabasesApril 12, 2003 2 min read 171Updated: May 18, 2026

MySQL 4.1: Finally, Subqueries are Here! (2003)

AunimedaAunimeda
📋 Table of Contents

MySQL 4.1: Finally, Subqueries are Here!

For years, the high-end database crowd (Oracle, DB2, even PostgreSQL) has looked down on MySQL because we didn't have subqueries. We had to create temporary tables, run a query, store the IDs, and then run another query. It worked, but it was ugly and hard to optimize. With the alpha release of MySQL 4.1, those days are over.

The Old Way: Temporary Tables

If you wanted to find all customers who haven't placed an order, you'd do something like this:

CREATE TEMPORARY TABLE customers_with_orders
SELECT DISTINCT customer_id FROM orders;

SELECT * FROM customers 
LEFT JOIN customers_with_orders ON customers.id = customers_with_orders.customer_id
WHERE customers_with_orders.customer_id IS NULL;

DROP TABLE customers_with_orders;

It's three steps, it's slow, and it messes up your transaction logs.

The New Way: NOT IN and EXISTS

Now, you can do it in a single, elegant statement. The optimizer can actually look at this and decide the best way to execute it.

SELECT * FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);

Or even better, using EXISTS:

SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);

Performance Warning

While subqueries are a huge leap forward, don't go crazy. In early versions of 4.1, the optimizer still struggles with some IN clauses. If you're dealing with millions of rows, sometimes a LEFT JOIN is still faster. But for 90% of your daily work, subqueries will make your life significantly easier.

MySQL is finally growing up. It's not just a "fast key-value store with a SQL parser" anymore; it's becoming a real relational database.


Aunimeda builds backend systems with optimized database architectures - PostgreSQL, Redis, ClickHouse, and more.

Contact us for backend and database engineering. See also: Custom Software Development

Read Also

Postgres BML: Binary Model Loading and Vector Speed (2025)aunimeda
Databases

Postgres BML: Binary Model Loading and Vector Speed (2025)

Postgres is no longer just for rows. In 2025, BML allows us to load ML models directly into the database for ultra-low latency inference.

PostgreSQL: GIN and GiST Indices (2006)aunimeda
Databases

PostgreSQL: GIN and GiST Indices (2006)

B-Trees are fine for integers, but what about full-text search or geometric data? It's time to learn the power of GIN and GiST.

PostgreSQL 7.3: Utilizing Schemas for Better Database Isolation (2003)aunimeda
Databases

PostgreSQL 7.3: Utilizing Schemas for Better Database Isolation (2003)

The release of PostgreSQL 7.3 brings a feature we've been waiting for: Schemas. No more prefixing every table with 'app1_'. Let's organize our data properly.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles