Guna Rakulan
Guna Rakulan

Reputation: 37

How to Select data from two tables which are not identical on another table using WHERE condition in MySQL

I have two tables, those are "Student" and "User". I want to select the first_name and Last_name from "Student" table which does not match with "Student_ID" of the user table.

SELECT student.student_id, 
       student.first_name, 
       student.last_name 
FROM   student, 
       USER 
WHERE  student.student_id != USER.student_id 

Upvotes: 1

Views: 61

Answers (2)

Andriy Shevchenko
Andriy Shevchenko

Reputation: 1131

Same as NOT IN version but perform a little bit better. According to this: https://www.eversql.com/sql-syntax-check-validator/

SELECT
  student.student_id,
  student.first_name,
  student.last_name
FROM
  student
WHERE
  NOT EXISTS (
    SELECT
      student_id
    from
      user
    where
      user.student_id = student.student_id
  )

Upvotes: 2

Onkar Musale
Onkar Musale

Reputation: 909

Try using this query:

SELECT firstname,lastname 
FROM Student 
WHERE ID NOT IN(SELECT Student_ID from User);

Upvotes: 1

Related Questions