How to use SQL to find columns with different values in two tables

Source: Internet
Author: User

The following two tables have the same structure. use SQL to find columns with different values.

Student_1

NAME AGE SCORE
Peter 26 100
Jack 25 96
Daniel 26 48
Bark 21 69

 

 

Student_2

NAME AGE SCORE
Peter 26 89
Jack 25 96
Daniel 26 48
Bark 21 69

 

Method 1 -- not exists:

Copy codeThe Code is as follows:
SELECT *
FROM Student_1 S1
WHERE NOT EXISTS
(SELECT *
FROM Student_2 S2
WHERE S1.name = S2.name
AND S1.age = S2.age
AND S1.score = S2.score
)
UNION ALL
SELECT *
FROM STUDENT_2 S2
WHERE NOT EXISTS
(SELECT *
FROM STUDENT_1 S1
WHERE S1.name = S2.name
AND S1.age = S2.age
AND S1.score = S2.score
);

Method 2-MINUS
Copy codeThe Code is as follows:
(SELECT * FROM Student_1
MINUS
SELECT * FROM Student_2)
UNION ALL
(SELECT * FROM Student_2
MINUS
SELECT * FROM Student_1)

Method 3-HAVING GROUP
Copy codeThe Code is as follows:
Select distinct name, age, score FROM (
SELECT * FROM Student_1
UNION ALL
SELECT * FROM Student_2
) Group by name, age, score having count (*) = 1;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.