Skip to content
Advertisement

How do i write this without using ALL?

Write a query to find only those customers whose grade are, higher than every customer to the city New York

SELECT *
FROM customer
WHERE grade > ALL
   (SELECT grade
    FROM customer
    WHERE city='New York');

Advertisement

Answer

You can use MAX in your subquery instead:

SELECT *
FROM customer
WHERE grade > 
   (SELECT MAX(grade)
    FROM customer
    WHERE city='New York');

As @GordonLinoff points out, if there are no customers from New York, this query will fail. You can work around that by using COALESCE to convert the MAX value from NULL to -1 (basically you need a number which is lower than the lowest possible value of grade) e.g.

SELECT *
FROM customer
WHERE grade > 
   (SELECT COALESCE(MAX(grade), -1)
    FROM customer
    WHERE city='New York');

Demo on dbfiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement