MySQL except clause doesnt work

319 views Asked by At

Given the customer table and loan table, I wish to find the customers who have not taken a loan. The schema definition is as follows:

mysql> select  * from customer;
+---------------+-----------------+---------------+
| customer_name | customer_street | customer_city |
+---------------+-----------------+---------------+
| Adams         | Spring          | Pittsfield    |
| Brooks        | Senator         | Brooklyn      |
| Curry         | North           | Rye           |
| Glenn         | Sand Hill       | Woodside      |
| Green         | Walnut          | Stamford      |
| Hayes         | Main            | Harrison      |
| Johnson       | Alma            | Palo Alto     |
| Jones         | Main            | Harrison      |
| Lindsay       | Park            | Pittsfield    |
| Smith         | North           | Rye           |
| Turner        | Putnam          | Stamford      |
| Williams      | Nassau          | Princeton     |
+---------------+-----------------+---------------+
12 rows in set (0.00 sec)

mysql> select  * from borrower;
+---------------+---------+
| customer_name | loan_id |
+---------------+---------+
| Adams         | L16     |
| Curry         | L93     |
| Hayes         | L15     |
| Jackson       | L14     |
| Jones         | L17     |
| Smith         | L11     |
| Smith         | L23     |
| Williams      | L17     |
| Adams         | L19     |
| Adams         | L15     |
| Jones         | L15     |
| Williams      | L23     |
+---------------+---------+
12 rows in set (0.00 sec)

Now, I tried the query : select customer_name from customer except select customer_name from borrower;

But I get the 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 'select customer_name from borrower' at line 1

I tried difference also but the same kind of error pops out. But union works well.

mysql> select  customer_name from customer union select customer_name from borrower;
+---------------+
| customer_name |
+---------------+
| Adams         |
| Brooks        |
| Curry         |
| Glenn         |
| Green         |
| Hayes         |
| Johnson       |
| Jones         |
| Lindsay       |
| Smith         |
| Turner        |
| Williams      |
| Jackson       |
+---------------+
13 rows in set (0.00 sec)

Any suggestions where I might be wrong?

2

There are 2 answers

1
ZaoTaoBao On

Can you try:

select customer_name from customer where customer_name not in (select customer_name from borrower);

so you select customer_name from customer that haven't take loan.

I haven't check but maybe is easier way.

0
Zafar Malik On

except and difference keyword does not work in mysql.

Further below query provide you results faster even with bulky tables.

SELECT cust.customer_name 
FROM customer cust 
LEFT JOIN borrower brw ON brw.customer_name=cust.customer_name 
WHERE brw.customer_name IS NULL;

Note: There should be cust_id in both tables to join data instead of customer_name and should be indexed.