Window function syntax error at over clause - mysql

I'm solving a hackerrank SQL problem which you can see [here][1] and I have already solved it using some other method but I want to try the window function for the same, so I have run a basic query to understand the window function.
SELECT salary, SUM(salary) OVER (ORDER BY salary) AS running_total
FROM Employee;
but I got the below error.
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 '(ORDER BY salary) AS running_total FROM
employee' at line 2
I'm pretty confused why it's throwing an error.
P.S: the query I posted above is not solving the problem, it's just a try to understand the window function, if you can solve the whole problem using the window function and write it in the answer that also will help me learn more. if you need any more details please mention them in the comments.
Table name- Employee
columns:
employee_id-> integer
name-> string
months-> integer
salary-> integer
Mysql version - 8.0.20
[1]: https://www.hackerrank.com/challenges/earnings-of-employees/problem?isFullScreen=true

Most likely, your MySQL version is less than 8+, and therefore does not support window functions. Here is an alternative to your current query which should run on your MySQL version:
SELECT
salary,
(SELECT SUM(e2.salary) FROM Employee e2
WHERE e2.salary <= e1.salary) AS running_total
FROM Employee e1
ORDER BY salary;

Related

Why am I getting a syntax Error on hackerrank SQL problem using window functions (partition by)

Hi I am trying to figure out why hackerrank is returning a syntax error on my query ran on their platform but when I run the same query I get no issues on mysql workbench.
Here is a snippet of the larger query I have written. I get an error 1064 (42000):
select
lag(submission_date, 1,'2016-03-01') OVER(Partition by hacker_Id order by submission_date)
from submissions ;

dense_rank function error and not able to proceed further

I have used below query in Fiddle application for learning purpose but its throwing error:
with result as
(
select salary, dense_rank() over (order by salary desc) as 'dense_rank'
from salary
)
select salary from result where result.denserank = 3
Error:
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 '(order by salary desc) as 'DENSE_RANK' from salary' at line 1
What to do further.? Also is Mysql and postgreSql same
Apparently you're using a MySQL version that supports CTEs but does not support Window functions. MySQL 8.0.1 is such version (8.0.1 introduces CTEs for the first time and 8.0.2 introduces window functions).
You need to use a more recent version of MySQL and (ii) change where result.denserank = 3 to where result.dense_rank = 3.

Totals and Runningtotals with SQL "sum" and "over" Syntax

In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
MySQL said: Documentation
#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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country

SELECT LAST(CustomerName) AS LastCustomer FROM Customers;

I am beginner to mysql, while I practice syntax of LAST() i.e
SELECT LAST(CustomerName) AS LastCustomer FROM CUSTOMERS; I am getting an error.
Error : ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for
the right syntax to use near '(CustomerName) AS LastCustomer FROM
CUSTOMERS' at line 1
the thing is there is nothing on mysql like LAST() function. You can see the comment here where it is said.
You can find the list of mysql functions visiting mysql aggregated functions
It is better to use simple query like the following -
SELECT CustomerName as LastCustomer ORDER BY id DESC LIMIT 1
Hope it helps...:)
Last() is not supported in Mysql so try
SELECT TOP 1 CustomerName FROM Customers ORDER BY CustomerID DESC;
try this
SELECT * FROM CUSTOMERS ORDER BY customer_id DESC LIMIT 1;

mysql error code 1064 where statement

I am very new to SQL/MYSQL and I am having a problem…I created a database with tables and such and I can search it no problem…however when I add in a "WHERE" statement, I get the error code 1064. any help would be appreciated.
Error Code: 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 'WHERE salary =>60000' at line 1
My query is this…
SELECT
salary
FROM
instructor;
WHERE
salary => 60000
If I run the query without the "WHERE" line it works just fine, but with it i get that error above. also, just to note, the word "WHERE" IS UNDERLINED IN RED, BUT NO OTHER WORDS ARE.
thanks for any help!
also, I'm using mysql workbench version 5.2.47 on a mac
SELECT salary FROM instructor; WHERE salary =>60000
you have an added ';' before the WHERE. That could be causing the issue.
It should instead be:
SELECT salary from instructor WHERE salary => 60000;
Semicolon will terminate the SQL statement so anything typed after the semicolon is considered to be a new statement.
Since starting a query with a WHERE keyword is syntactically incorrect, the WHERE clause was underlined.
SELECT salary FROM instructor; WHERE salary => 60000
SELECT salary FROM instructor;
is a statement by itself without the where clause.
However,
WHERE salary => 60000
does not make any sense to the system hence is marked by the editor.