While running the following query
SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY), CITY) AS T1
LIMIT 1
UNION
SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY) AS T2
LIMIT 1;
I get
ERROR 1064 (42000) at line 4: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'UNION
I can't understand what is the error here
Quoting the docs,
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:
(SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY), CITY) AS T1
LIMIT 1)
UNION
(SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY) AS T2
LIMIT 1)
It could be that you are using ORDER BY in both parts of union (SQL Server does not allow this). Try removing and / or ordering the final result set
Related
So I want to get the median of a numeric column using mySQL.
Here is my code:
SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT SELECT FLOOR(COUNT(*)/2) FROM STATION,1
Where LAT_N is the column name that I want to check and STATION is the table name. I got the following error, But I could not understand which syntax is wrong.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT FLOOR(COUNT(*)/2) FROM STATION),1' at line 3
Could you help me to find the syntax error? If possible I would like to calculate the median using the same way.
You can use window functions instead:
SELECT LAT_N
FROM (SELECT S.*,
COUNT(*) OVER () AS CNT,
ROW_NUMBER() OVER (ORDER BY LAT_N) AS SEQNUM
FROM STATION S
) S
WHERE SEQNUM = FLOOR(CNT / 2);
you can use prepared statements, as Limit only accepts Numbers
SELECT FLOOR(COUNT(*)/2) INTO #a FROM STATION;
SET #s = 'SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT ?,1';
PREPARE stmt1 FROM #s;
EXECUTE stmt1 USING #a;
DEALLOCATE PREPARE stmt1;
This query is throwing error, it seems correct but i am getting an error
WITH Rows AS (SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as [Row]
FROM users
WHERE 1=1
)
SELECT * FROM Rows
WHERE Row >= 1 and Row <= 10
error i am getting while running the above statement is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Rows AS (SELECT *, ROW_NUMBER() OVER (ORDER BY userid asc)
FROM users
' at line 1
Your error is on Rows because MySQL does not recognize the CTE.
Then, MySQL also does not recognize [, so you want to use a more reasonable name. In MySQL 8+, that would be something like:
WITH Rows AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as seqnum
FROM users
)
SELECT *
FROM Rows
WHERE seqnum <= 10;
You don't need to compare to 1. That is definitional.
Of course, this, in turn, is overkill. The CTE is unnecessary:
SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as seqnum
FROM users
ORDER BY userid
LIMIT 10;
But this will still fail, because MySQL versions started recognizing CTEs and window functions in the same version.
So, you can just use variables:
SELECT u.*, (#rn := #rn + 1) as seqnum
FROM users u CROSS JOIN
(SELECT #rn := 0) params
ORDER BY userid
LIMIT 10;
I need to create a table from the union of two queries.
This query works exactly as I need it to:
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3
Once I add the create statement I begin to get errors
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5)
union
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)
The error is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER ' at line 1
You can try below query:-
CREATE TABLE portligh_lotteryTest.cTop8 (team int) AS
(SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count DESC LIMIT 5
union all
SELECT portligh_lotteryTest.scores.team FROM portligh_lotteryTest.scores ORDER BY portligh_lotteryTest.scores.count ASC LIMIT 3)
I'm new to SQL thus the question.
So I've the following table with Id, City, State named Station.
And I need to Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
Can someone help me get started with this. I tried the len() function on city but it doesn't seem to work.
City with the shortest name:
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1
City with the longest name:
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1
You can combine them into a single statement with UNION ALL:
SELECT *
FROM (
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1
) shortest
UNION ALL (
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1
) longest
This worked for me:
(SELECT CITY, CHAR_LENGTH(CITY) FROM STATION ORDER BY CHAR_LENGTH(CITY) ASC LIMIT 1) UNION (SELECT CITY, CHAR_LENGTH(CITY) FROM STATION ORDER BY CHAR_LENGTH(CITY) DESC LIMIT 1)
Here is a single query which you can try:
SELECT City, CHAR_LENGTH(City) AS length, 'max char length' AS description
FROM yourTable
WHERE CHAR_LENGTH(City) = (SELECT MIN(CHAR_LENGTH(City)) FROM yourTable)
ORDER BY City
LIMIT 1
UNION
SELECT City, CHAR_LENGTH(City) AS length, 'min char length' AS description
FROM yourTable
WHERE CHAR_LENGTH(City) = (SELECT MAX(CHAR_LENGTH(City)) FROM yourTable)
ORDER BY City
LIMIT 1
Here's my crack at it. This is untested, but it will give you something to start off of. (Edit: Now it is tested and it works. The union idea is probably better, but this would be the alternative using subqueries).
SELECT
(SELECT City
FROM table
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1) AS longest,
(SELECT CHAR_LENGTH(City)
FROM table
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1) AS longest_length,
(SELECT City
FROM table
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1) AS shortest,
(SELECT CHAR_LENGTH(City)
FROM table
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1) AS shortest_length
FROM table
LIMIT 1
Hey got the same question of Hacker Rank :-P
Here is the solution:
select * from
(select city, length(city) from station where length(city) = (select min(length(city)) from station) order by 1 limit 1) as tmp1
union all
select * from
(select city, length(city) from station where length(city) = (select max(length(city)) from station) order by 1 limit 1) as tmp2
we cannot use limit and order by clause with UNION clause. Thus we need to take alias of the result from both query and apply UNION clause.
Hope this will help :-)
(select CITY,length(CITY)
from STATION
where length(CITY)=(select min(length(CITY)) from STATION)
order by city
LIMIT 1)
union
(select CITY,length(CITY)
from STATION
where length(CITY)=(select max(length(CITY)) from STATION)
order by city
LIMIT 1);
This may work also -
select first(CITY) ,len(first(CITY)) from STATION where CITY in (select CITY from STATION where len(CITY) in (SELECT MAX(LEN(CITY)) FROM STATION) order by CITY asc)
UNION
select first(CITY) ,len(first(CITY)) from STATION where CITY in (select CITY from STATION where len(CITY) in (SELECT MIN(LEN(CITY)) FROM STATION) order by CITY asc);
SELECT City, CHAR_LENGTH(City)
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1;
SELECT City, CHAR_LENGTH(City)
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1;
Try the above solution I reckon should solve your problem.
(select CITY,length(CITY)
from STATION
where length(CITY)=(select min(length(CITY)) from STATION)
order by city
LIMIT 1)
union
(select CITY,length(CITY)
from STATION
where length(CITY)=(select max(length(CITY)) from STATION)
order by city
LIMIT 1)
I'm doing this challenge on Hackerrank:
https://www.hackerrank.com/challenges/weather-observation-station-5
I'm a beginner on SQL and I'm trying to query all the rows that have a maximum value for a column, maximum that I can only obtain via MAX(). So I'm trying this:
SELECT CITY, LENGTH(CITY) AS citylength
FROM STATION
WHERE LENGTH(CITY) = (SELECT MIN(CITY) FROM STATION)
and I get errors.
I've looked up on google about sub-queries but I'm not accustomed enough to know exactly how it works, so I need your help guys.Thanks.
So to sum up, I need a query that can get the rows on a table that has a maximum value obtained via MAX() clause.
You are requested to find two different results:
The city with maximum length (and the first in the alphabet in case of a tie)
The city with minimum length (and the first in the alphabet in case of a tie)
This means two different queries, which you glue together with UNION ALL.
(
select concat(city, ' ', length(city))
from station
order by length(city), city limit 1
)
union all
(
select concat(city, ' ', length(city))
from station
order by length(city) desc, city limit 1
);
As Strawberry pointed out: You need the parentheses in order to place two ORDER BY clauses, one per query part. (Otherwise you can only place one ORDER BY clause at the end for the whole query.)
In your query you are comparing LENGTH(CITY), i.e. an integer holding the name's length and MIN(CITY), i.e. the city name itself, which cannot work of course. You would have to compare with MIN(LENGTH(CITY)). Then do the same for the maximum and then use UNION ALL. This doesn't solve the problem with ties, however, which the LIMIT query does.
This works without sub-queries
SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY 2,1
LIMIT 1
This works :
SELECT CITY,LENGTH(CITY)
FROM STATION
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) M FROM STATION);
This works correctly:
select city, length(city)
from station
where length(city) = (select min(length(city)) as m from station)
order by city ASC
limit 1;
(SELECT CONCAT(city,' ',LENGTH(city)) city, 'shortest' category FROM stations ORDER BY LENGTH(city),city LIMIT 1)
UNION ALL
(SELECT CONCAT(city,' ',LENGTH(city)), 'longest' FROM stations ORDER BY LENGTH(city) DESC,city LIMIT 1);
select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select min(len(city)) from station) order by city asc;
select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select max(len(city)) from station) order by city desc;
SELECT * FROM
(SELECT City, LENGTH(City) FROM STATION
ORDER BY LENGTH(City),city ASC)
WHERE ROWNUM <= 1
UNION ALL
SELECT * FROM
(SELECT City, LENGTH(City) FROM STATION
ORDER BY LENGTH(City) DESC)
WHERE ROWNUM <= 1;