AboutBlogContact
DatabaseApril 12, 2003 2 min read 17

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.

Read Also

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

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.

How to Scale MySQL with Read Replicas When Your App Slows Down (2015)aunimeda
Database

How to Scale MySQL with Read Replicas When Your App Slows Down (2015)

When a single MySQL server handles both reads and writes, reads win and writes stall. Adding a read replica splits the load: writes go to master, reads go to replica. This is the exact setup — my.cnf, replication config, PHP connection routing — we used when our app hit 50k daily users.

MongoDB 1.6: Scaling Out with Sharding and Replica Sets (2010)aunimeda
Database

MongoDB 1.6: Scaling Out with Sharding and Replica Sets (2010)

The NoSQL revolution is in full swing. With MongoDB 1.6, horizontal scaling and automated failover are finally production-ready. Let's configure a sharded cluster.

Need IT development for your business?

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

Get Consultation All articles