The following query works:
SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL)
But as soon as I wrap the query with parentheses it doesn't work anymore:
(SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL))
Causing syntax error:
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 'UNION
Wrapping other select queries in parentheses doesn't cause problems, this works for example:
(SELECT DISTINCT number FROM johnson.employee);
What is the difference between these?
This has to do with the way MySQL implemented its SQL grammar.
A <query> can be:
SELECT ... [ UNION <query>]
or
( SELECT ... ) [ UNION <query> ]
But apparently not
( SELECT ... UNION <query> )
Read sql/sql_yacc.yy in the MySQL source code if you want the details.
As a workaround, you can do this:
SELECT * FROM (SELECT ... UNION SELECT ... ) AS t;
Parentheses around queries are subqueries -- think of this almost like a subroutine, where the inner query is executed first, then its result is evaluated in the context of the outer query. In your second example, you are defining a subquery where no outer query exists, hence a syntax error.
You might be able to transform the second example into valid sql simply by putting a SELECT before the first parenthesis and 'FROM DUMMY' after the closing parenthesis; try it.
Related
I want to use the result of a WITH clause to filter a query like below:
WITH Max_Dates AS (
SELECT MAX(created_date) AS maxdate
FROM transactions
GROUP BY DATE (created_date)
)
SELECT *
FROM transactions
WHERE created_date IN Max_Dates -- can I somehow reference column maxdate as a list here?
The syntax is incorrect. I know I can use the content of the WITH clause as a subquery in the WHERE below to get the result I want, but I want to know specifically if the WITH result can be used.
My question is, is there a general syntax for using a column from a WITH clause to filter as a list in a WHERE ... IN {list}?
As per this blog it seems it should be possible to use WHERE created_at IN Max_Dates.maxdate, but I'm using MySQL 8.0.29 and it doesn't like that syntax - Error: (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 'Max_Dates.maxdate' at line 8")
WITH Max_Dates AS
(
SELECT MAX(created_date) AS maxdate
FROM transactions
GROUP BY DATE (created_date)
)
SELECT *
FROM transactions
WHERE created_date IN (select maxdate from Max_Dates)
The CTE (Common Table Expression) Max_Dates is a resultset that potentially has multiple columns, so you must specify the specific column from Max_Dates that should be used to build the list of values for the IN expression.
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;
What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
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 '* from mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference.
I'm using phpmyadmin to test out some MySQL queries. I'm trying to write a larger, nested query, which is failing due to an unrecognized table alias, so I'm trying to debug smaller parts of it. However, I'm getting confusing errors when I try to use table aliases sometimes.
Can you explain why some of these queries throw errors?
SELECT * FROM table1 AS tablealias1 (works)
SELECT * FROM table1 GROUP BY userid (works)
SELECT * FROM table1 GROUP BY userid AS tablealias1 (error: #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 'AS tablealias1
LIMIT 0, 25' at line 1 )
SELECT * FROM table1 WHERE userid=1 (works)
SELECT * FROM table1 WHERE userid=1 AS tablealias1 (same error as above)
(SELECT * FROM table1 WHERE userid=1) AS tablealias1 (same error as above)
You alias things to:
rename the column display's name
give it a reference name for later use elsewhere in the query statement (whether you use it explicitly or implicitly--as long as it could be used elsehere)
If you're not doing either, an alias makes no sense. You can't alias a result set unless it's used inside a subquery, then you need an alias to reference it.
This will work:
Select * FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
as it implies
Select tablealias1.* FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
Alone, this is garbage:
(SELECT * FROM table1 WHERE userid=1) AS tablealias1
select *
from ( select * from table ) 'table1';
I cannot see why I am getting this error:
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 ''table1'' at line 2
I have checked the manual (MySQL subquery in FROM clause) and I can't see any difference between the examples and my little statement.
Table names/aliases must either be surrounded with backticks or nothing
select *
from ( select * from table1 ) table1;
I think you want back quotes rather than forward quotes:
select *
from ( select * from table ) `table1`;
Forward quotes specify a string constant. Back quotes delimit a name.
when you have a subquery that is a table, you need to declare it with a name.
select * from (select * from table1) as x
As well as not putting quotes around the alias, I believe you also need backticks around "table" in the subquery because it's a reserved word in MySQL (assuming you did actually name your table "table"):
select * from ( select * from `table` ) table1;