Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when using except in a query

This query works:

mysql> SELECT s.sno FROM students s;  
+------+  
| sno  |   
+------+   
|    1 |   
|    2 |   
|    3 |   
|    4 |   
|    5 |    
|    6 |    
|    7 |   
|    8 |   
|    9 |   
|   10 |   
+------+   
10 rows in set (0.00 sec)  

This query also works:

mysql> SELECT t.sno FROM take t WHERE t.cno = 'CS112';  
+------+  
| sno  |  
+------+  
|    1 |  
|    2 |  
|    3 |  
|    4 |  
+------+   
4 rows in set (0.00 sec)  

BUT this query:

SELECT s.sno FROM students s    
EXCEPT    
SELECT t.sno FROM take t WHERE t.cno = 'CS112';  

fails with the error:

mysql> SELECT s.sno FROM students s  
    -> EXCEPT  
    -> SELECT t.sno FROM take t WHERE t.cno = 'CS112';  
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 n   
ear 'EXCEPT   
SELECT t.sno FROM take t WHERE t.cno = 'CS112'' at line 2 

What am I doing wrong here?

like image 953
Cratylus Avatar asked Apr 18 '13 20:04

Cratylus


People also ask

Why am I getting a syntax error in SQL?

Misspellings are the most common cause for error in SQL. Unfortunately, SQL will not autocorrect mistyped keywords, tables, columns, or values. Check keyword spelling by referring to the documentation for the type of SQL you are using.

What are the syntax related errors in this query?

This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: Using a database-specific SQL for the wrong database (eg BigQuery supports DATE_ADD, but Redshift supports DATEADD) Typo in the SQL (missing comma, misspelled word, etc)

What is except query in SQL?

The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows from the first SELECT statement that are not returned by the second SELECT statement. This means EXCEPT returns only rows, which are not available in the second SELECT statement.

What can I use instead of except in MySQL?

MySQL does not support MINUS/EXCEPT, the workaround is to use LEFT JOIN.


2 Answers

I don't believe MySQL supports EXCEPT syntax. Try using NOT IN or a LEFT JOIN:

SELECT s.sno 
FROM students s    
WHERE s.sno NOT IN 
(
    SELECT t.sno 
    FROM take t 
    WHERE t.cno = 'CS112'
);

OR

SELECT s.sno 
FROM students s    
    LEFT JOIN take t ON s.sno = t.sno
WHERE IFNULL(t.cno, '') != 'CS112'

UPDATE

I mocked up your data as such and it correctly returns 5 through 10:

create temporary table temp_students (sno int)

insert into temp_students values (1)
insert into temp_students values (2)
insert into temp_students values (3)
insert into temp_students values (4)
insert into temp_students values (5)
insert into temp_students values (6)
insert into temp_students values (7)
insert into temp_students values (8)
insert into temp_students values (9)
insert into temp_students values (10)

create temporary table temp_take (sno int, cno varchar(50))

insert into temp_take values (1, 'CS112')
insert into temp_take values (2, 'CS112')
insert into temp_take values (3, 'CS112')
insert into temp_take values (4, 'CS112')

SELECT s.sno 
FROM temp_students s    
        LEFT JOIN temp_take t ON s.sno = t.sno
WHERE IFNULL(t.cno, '') != 'CS112'
like image 174
McCee Avatar answered Sep 22 '22 07:09

McCee


Query:

SQLFIDDLEExample

SELECT s.sno 
FROM students s
WHERE NOT EXISTS (SELECT 0 
                  FROM take t 
                  WHERE t.sno = s.sno 
                  AND t.cno = 'CS112') 
like image 45
Justin Avatar answered Sep 22 '22 07:09

Justin