Solved

Subquery doesn't work, syntax is correct???

Posted on 2004-04-03
9
360 Views
Last Modified: 2008-03-17
I just installed Mysql 4.0.18

I tried to run ths sql statement

Select a.price, a.name
from rooms
where a.price = (Select max(price) from rooms);


I don't believe the syntax is wrong.  Can anyone help me?  I checked the data in the [Rooms] table which are all valid.

When I run:
Select max(price) from rooms;

It works, but when its in a subquery it doesn't.

Any help is greatly appreciated
0
Comment
Question by:phake123
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Author Comment

by:phake123
ID: 10750209
I get this error:
Error 1064:  You have an error in your SQL syntax.  Check the manual that correspond to your MYSQL server version for the right syntax to use near 'Select max(price) from rooms' at line 3
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 10750764
unless you use mysql 4.1, mysql doesn't support subqueries...
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 10755076
An alternative is this:

Select @max := max(price) from rooms;

Select a.price, a.name
from rooms
where a.price = @max;

Execute the two in series within the same database connection.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Accepted Solution

by:
Dossy earned 50 total points
ID: 10756530
What you want is this:

SELECT   a.price, a.name
FROM     rooms AS a
WHERE   a.price IN (
    SELECT   MAX(price)
    FROM     rooms
);

However, from this particular query I have a feeling what you REALLY want is this:

SELECT   price, name
FROM      rooms
HAVING   price = MAX(price);

The first query using the subquery will do two scans of the table -- once for the outer query, and once for the inner subquery.  The second query (with the HAVING clause) should only scan the table once, which should be somewhat faster.

-- Dossy
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 10756818
Dossy: We have already established that the user cannot use subqueries, so the first example os out.  As for the second one: did you try this? You are combining an aggregate function with non-aggregate functions, which will cause MySQL to error out on illegal syntax.
0
 
LVL 1

Expert Comment

by:Dossy
ID: 10757365
Squeebee:  You're right, phake123 did say MySQL 4.0 -- oops.  And yes, the second query doesn't do what I thought.

Creating a simple test:

mysql> create table rooms (price int, name varchar(100));
mysql> insert into rooms values (10, "ten");
mysql> insert into rooms values (11, "eleven");        
mysql> insert into rooms values (15, "fifteen");        
mysql> insert into rooms values (5, "five");

This isn't optimal (as the ORDER BY still causes MySQL to do a full table scan and a filesort) but it works:

SELECT   price, name
FROM     rooms
ORDER   BY price DESC
LIMIT     1;

Returns the "15, fifteen" row.

However, your suggestion of using a variable to store MAX(price) and using that in a subsequent WHERE clause is definitely the most optimal solution since it CAN take advantage of indexes, etc.

0
 
LVL 17

Expert Comment

by:Squeebee
ID: 10757440
Hi Dossy;

Sorry to nit-pick but there is one scenario you need to consider: What happens when more than one product exists at the highest price? In such a situation you will only return one of the highest priced items, but if you had two items that each cost $15 only one would be returned. I'm not criticizing, just giving your something else to think about.
0
 
LVL 1

Expert Comment

by:Dossy
ID: 10759036
Yup, you're right.
0
 

Author Comment

by:phake123
ID: 10760662
Thanks everyone.  All your inputs were great!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question