In MySQL, we can compare two columns to identify the records they have in common and also, the records one table has but the other does not.
In this article, we will learn how to compare two columns to find common records, and we will also find their differences.
To illustrate the examples in this article, we are going to create two tables (contacts and clients) with the following structure and data:
- CREATE TABLE `clients` (
- `id` bigint(20) UNSIGNED NOT NULL,
- `name` varchar(255) DEFAULT NULL,
- `address` text
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `clients` (`id`, `name`, `address`) VALUES
- (1, 'Mark Johnson', '2113 Jackson Street,Apt. B,Erie,CA,510'),
- (2, 'Sasha Smith', '123 Main Street,,Erie,CA,512'),
- (3, 'Charles Walls', '979 Drummond Street, Newark'),
- (4, 'Susan Lee', '1572 Hanover Street'),
- (5, 'Carol Parker', '4834 Blackwell Street');
- CREATE TABLE `contacts` (
- `id` int(11) NOT NULL,
- `name` varchar(20) NOT NULL,
- `phone` varchar(20) NOT NULL,
- `email` varchar(20) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- INSERT INTO `contacts` (`id`, `name`, `phone`, `email`) VALUES
- (1, 'John Grey', '19005264555', 'john@gmail.com'),
- (2, 'Mary Rose', '19008504587', 'mary@gmail.com'),
- (3, 'Matt Crow', '19006238747', 'matt@gmail.com'),
- (4, 'Susan Lee', '19006781563', 'susan@gmail.com'),
- (5, 'Carol Parker', '19007426336', 'carol@gmail.com'),
- (6, 'Mark Johnson', '19008881234', 'mark@gmail.com'),
- (7, 'Sasha Smith', '19001527788', 'sasha@gmail.com'),
- (8, 'Isaac Peterson', '19008795678', 'isaac@gmail.com'),
- (9, 'Ana Brown', '19006961425', 'ana@gmail.com'),
- (10, 'Alex Robertson', '19003693791', 'alex@gmail.com');
We can execute the following queries to verify the content of each table:
- SELECT * FROM `clients`
Result:
Clients table content
- SELECT * FROM `contacts`
Result:
Contacts table content
Compare two columns in sql
Let’s compare the “NAME” column of both tables to find the names they have in common, and the names that one table has and the other does not.
To find the names that both tables have in common, we will execute the following query:
- SELECT a.name
- FROM (SELECT distinct b.name FROM `contacts` b UNION ALL
- SELECT distinct c.name FROM `clients` c) a
- GROUP BY a.name
- HAVING count(*) = 2
- ORDER BY a.name
Result:
Common names between both tables
As we can see, there are four names in common between both tables. The logic of the query we use to find them is very simple.
First, we request the names in the contacts table, to avoid repeated names we included the DISTINCT clause. Then we made a union with the names in the clients table. Finally, we filter the names that have two occurrences, that is, the names that are in the two tables.
Now, to calculate the differences between the two name columns, we can execute a very similar query, but we will filter the names that have a single occurrence, that is, the names that are in only one table:
- SELECT a.name
- FROM (SELECT distinct b.name FROM `contacts` b UNION ALL
- SELECT distinct c.name FROM `clients` c) a
- GROUP BY a.name
- HAVING count(*) = 1
- ORDER BY a.name
Result:
Name differences
As we can see, there are seven names that are not common between the two tables.
0 comments:
Post a Comment