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.
Related
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;
Table Name: Worker,
Fields : worker_id | first_name | last_name | department
I have a table name worker and i wanted to write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length. So i tried running this : (Database- Mysql)
select length(distinct(department)) from worker;
But it is giving an error saying:
ERROR 1064 (42000): 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 'distinct(department)) from worker' at line 1
But when i ran below query, it works perfectly fine:
select distinct(length(department)) from worker;
Can somebody please explain me why interchanging distinct and length function makes query works?
Thanks in advance!
Try not to use distinct like function but clause otherwise it will give syntax error.
Below sql statement will execute as shown below:
select distinct (length('xyz')) ---- length('xyz') : 3
select distinct (3) ---- output : 3
Distinct is not properly a function but a clause
select distinct length(department) from worker;
Anyway in MySQL work also with function syntax
select distinct( length(department)) from worker;
The code with the exchanged token don't work because DISTINCT produce an aggregated result removing the duplicated values,this implies that the outer length() function work on not correct set of rows or better the db engine see that there an improper use of the DISTINCT clause and raise the syntax error
select length( distinct 'A' ) this raise an error
If you want use the outer length() function you should code this way
select length(my_col) from (
select distinct department my_col from worker
) ;
correct answer :
select distinct <column_dept> as department***,*** (len(<column_dept>) as length_column_dept from xyz_table
select distinct( length(department)),department from worker group by department;
select distinct (Department) as 'Unique department', len(Department) as 'length of name' from Worker;
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;
This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
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 '* from my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
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 '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).
Query issues, for the life of me i cant figure out what is wrong with this query
HAVING distance ='10'
GROUP BY c.ancient,
c.ant_name,
p.upper,
p.name,
ERROR 1064 (42000): 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 'GROUP BY c.city_id, c.city_name,
p.prop_ynow_id, p.Name, pr.PRE_Ident
SELECT dis' at line 1
HAVING goes after GROUP BY. MySQL is picky this way.
A little late to the party but Queries in any standard SQL from my experience generally have to go:
INSERT,
DELETE,
SELECT,
FROM,
WHERE,
GROUP BY,
HAVING, ORDER BY
Order.
I don't do MySQL, but in the SQL I'm used to, the HAVING clause needs to go after the GROUP BY clause.