0

I have the following sql query, which runs fine on MYSQL but gives error with Oracle and SQL Server because of the last limit 1 clause. My question is just - Is this limit 1 clause valid as per PostgreSQL and can I expect this to run successfully on Postgres?

select customer_number 
from
(select 
customer_number,
count(*) 
from
orders
group by 
customer_number 
order by count(*) desc limit 1) a

;

2 Answers 2

2

limit is supported by some databases, but not all. The SQL standard would be:

select customer_number
from orders
group by customer_number 
order by count(*) desc 
offset 1 row fetch first 1 row only;
Sign up to request clarification or add additional context in comments.

Comments

0

For sql server you need to use top

select top 1 customer_number,count(*) 
from orders
group by customer_number 
order by count(*) desc 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.