Mr. A Mr. A - 17 days ago 7
MySQL Question

Is there a way to shorten 'greater than' operator condition?

This is the code

SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name='Romania')


I want to know if the following part can be shortened.

(SELECT population FROM world
WHERE name='Romania')


I'm new to MYSQL. Sorry for such questions.

Thank You

Answer

While probably of little help in this query (as the sub query is only returning a single record), it is often faster to use a join rather than a sub query.

SELECT w1.name 
FROM world w1
INNER JOIN world w2 ON w1.population > w2.population
WHERE w2.name = 'Romania'

You would probably want an index on the name field, and another index on the population field for reasonable performance.

Comments