Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wanted to get the top 3 and bottom 3 of average growth from this table District_data,. However, I am getting error 1064 where the Union position is not right, please help me with the correct syntax of this product.

What I have tried:

Select state, avg(growth)*100 as "Top 3"  from district_data group by state order by 2 desc, 1; 
Union
Select state, avg(growth)*100 as "Bottom 3" from district_data group by state order by 2,  1 limit 3;
Posted
Updated 28-Jun-22 21:47pm
Comments
Richard MacCutchan 29-Jun-22 3:36am    
You have a semi-colon character after the 1, which makes “Union” the beginning of a new statement.

1 solution

In SQL, the semicolon is an (often optional but recommended) statement terminator: if you add one in the middle of a query statement, it ends the statement there and starst a new one.
Remove the semicolon here:
SQL
SELECT state, AVG(growth)*100 AS "Top 3" FROM district_data GROUP BY state ORDER BY 2 DESC, 1;  <<<-----
UNION
SELECT state, AVG(growth)*100 AS "Bottom 3" FROM district_data GROUP BY state ORDER BY 2,  1 LIMIT 3;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900