Search code examples
mysqlsqlrdbms

sql commands to fetch the users who didn't offerd any rides in carpooling databasel schema


Carpooling Database Schema Carpooling Database Schema

The users in ride_users table are also the ride provider in RIDE table. I have to identify the users details from user_details table who didn't offered any ride i.e they are not ride provider.

tried this but didn't get desired results.

select r1.user_id, u.*
from ride_users r1, user_details u
where r1.user_id <> (select distinct r1.user_id 
                     from ride_users r1 , ride r2 
                     where r1.ride_id = r2.ride_id 
                     and r1.user_id = r2.ride_provider_id
                     group by r1.user_id);`

Solution

  • Use Left join and check for null to see whether the entity present in the second table.

    SELECT u.*
    FROM user_details u
    LEFT JOIN ride r ON u.user_id = r.ride_provider_id
    WHERE r.ride_provider_id IS NULL