aiden87
aiden87

Reputation: 969

NOT EXISTS - SQL Query

I have two tables in my sql database

enter image description here

I need to write SQL query, that will find customers that didn't rent any movie. Looking at the table, those customers have id of 3 and 4

I've tried this, but it's not working. I seem to be missing simple logic in creating queries

SELECT name, last_Name, movie_name
FROM Customers, Rented_movies
WHERE Rented_movies.customer_id = Customers.customer_id
AND NOT EXISTS(customer_id);

Any help? Much appreciated!

Thank you

Upvotes: 0

Views: 48

Answers (2)

demopix
demopix

Reputation: 169

try this way fellow

SELECT name, last_Name, movie_name FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Rented_movies)

Upvotes: 2

h0r53
h0r53

Reputation: 3219

Try this:

 SELECT * FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Rented_movies);

If you MUST use a NOT EXIST statement, then this should also work:

SELECT * FROM Customers C
WHERE NOT EXISTS(SELECT * FROM Rented_movies WHERE customer_id = C.customer_id);

Upvotes: 1

Related Questions