How to compare columns in two different tables in SQL
Last Updated :
28 Apr, 2022
Here we are going to see how we can compare the columns of two different tables in SQL. We will be taking a few examples to see how we can do this in different ways.
Overview :
In this, we will understand overview of SQL query for required operation to perform How to compare columns in two different tables in SQL. We will understand each concept with the help of examples.
Step-1: Creating database :
To create the database use the following SQL query as follows.
Syntax -
create database_name;
Example -
create STUDENTS_DATA
Step-2: Using the database :
To use this database as follows.
Syntax -
use database_name;
Example -
use STUDENT_DATA
Step-3: Creating table :
Creating Table for making queries as follows.
Table-1: Syntax -
create table table_name
(
column1 type,
column2 type,
...
);
Example -
create table studentData1
(
roll_number int primary key,
firstname varchar(100),
lastname varchar(100),
marks int
);
Step-4: Inserting records :
Inserting records in table studentData1 as follows.
Syntax -
insert into table_name(column1, column2 ...) values (value1, value2 ...);
Inserting into studentData1 table -
insert into studentData1 (roll_number, firstname, lastname, marks)
values (1, 'albert', 'einstein',356);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (2, 'isaac', 'newton',412);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (3, 'marie', 'curie',436);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (4, 'philip', 'jsam',389);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (5, 'tom', 'jsam',452);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (6, 'tucker', 'jose',412);
insert into studentData1 (roll_number, firstname, lastname, marks)
values (7, 'drawn', 'caste',389);
Step-5: Creating table2 :
Here, we will create the studentData2 using SQL query as follows.
create table studentData2
(
id int primary key,
firstname varchar(100),
lastname varchar(100),
marks int
);
Step-6: Inserting records :
Inserting records in the table studentData2 as follows.
insert into studentData2 (id, firstname, lastname, marks)
values (2, 'isaac', 'newton',412);
insert into studentData2 (id, firstname, lastname, marks)
values (3, 'marie', 'curie',436);
insert into studentData2 (id, firstname, lastname, marks)
values (6, 'tucker', 'jose',412);
insert into studentData2 (id, firstname, lastname, marks)
values (4, 'philip', 'jsam',389);
Example-1 :
Using the where clause to compare columns of two different tables. It cannot handle the null values.
Syntax :
(to select all the records with all columns)
select * from table1, table
where
table1.col1 = table2.col2 and table1.col1 > someValue;
Syntax(Alternative) :
(to select specific columns from the tables)
select t1.col1, t2.col2,t3. col3 ...., t2.col1, t2.col2, t2.col3....
from table1 t1, table t2 where t1.col1 = t2.col2 and t1.col1 <> t2.col2;
Query -
select * from studentData1, studentData2
where studentData1.roll_number = studentData2.id;
Output :
Result based on the comparison of roll_number and id as follows.
roll_number | firstname | lastname | marks | id |
---|
2 | isaac | newton | 412 | 2 |
3 | marie | curie | 436 | 3 |
4 | philip | jsam | 389 | 4 |
6 | tucker | jose | 412 | 6 |
Example-2 :
Using joins to compare columns by priority among the table. For example, left join returns all values from the first table and null value for the not-matched records from the second table. Similarly, we can use right join, inner join, full join and self join as per our requirements. In the below example we have compared two tables based on the columns roll_number and id using the left join.
Syntax -
select t1.col1, t1.col2... , t2.col1, t2.col2... ,
from table1 as t1 left
join table2 as t2 on
tabe1.col1 = table2.col1;
Query -
select a.roll_number, a.firstname, b.id
from studentData1 as a left
join
studentData2 as b on
a.roll_number = b.id;
Output :
Join based on left table i.e. sutdentData1 as follows.
roll_number | firstname | id |
---|
1 | albert | |
2 | isaac | 2 |
3 | marie | 3 |
4 | philip | 4 |
5 | tom | |
6 | tucker | 6 |
7 | drawn | |
Example-3 :
UNION allows us to compare two same types of tables or datasets. We can use union to compare the columns once we can have the union of both the tables. It can quickly check what are the data missing or changed in either table. It is capable of handling null values which cannot be handled by where clause.
Note -
This is only used when we have the same type of tables.
Syntax -
select col1, col2, col3 ....
from (select col1, col2, col3 ...
from Table1 union all select col1, col2, col3 ...
from Table2)
cmpr order by ID;
Example -
select * from
(select * from studentData1
where roll_number > 4
union all
select * from studentData2 where id < 6)
cmpr order by marks;
Output :
Compared same records where roll_number > 4 and id < 6 as follows.
roll_number | firstname | lastname | marks |
---|
7 | drawn | caste | 389 |
4 | philip | jsam | 389 |
6 | tucker | jose | 412 |
3 | marie | curie | 436 |
5 | tom | jsam | 452 |
Similar Reads
Compare and Find Differences Between Two Tables in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. Here we are going to see how to Compare and Find Differences Between Two Tables in SQL Here, we will first create a database name
3 min read
How to Compare Rows and Columns in the Same Table in SQL
In SQL, comparing rows and columns in the same table is a common task for data analysis, identifying relationships, and calculating differences. By using techniques like self-joins and conditional queries, we can extract meaningful insights from a single table. This enables efficient querying, allow
4 min read
How to Compare Two Columns For Equality in SQL Server?
In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared. For this article, we will be using the Microsoft SQL Server as our database. Syntax: SELECT * F
2 min read
How to Compare Indexes of Tables From Two Different Databases in Oracle?
Structured Query Language supported by RDBMS(Relation Database Management Systems) such as Oracle, Mysql, PostgreSQL, and SQL Server. All relational database system support objects such as databases, tables, schema, indexes, views, functions, procedures, and many more. RDBMS system performs best whe
3 min read
How to Get the Datatype of Table Columns in MySQL?
When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designin
4 min read
How to Efficiently Convert Rows to Columns in SQL?
In SQL, rows and columns are the fundamental building blocks of a database. Rows represent individual records, while columns represent the attributes or characteristics of those records. However, there may be instances where we need to convert rows to columns in order to better analyze and manipulat
5 min read
How to Get the Type of Columns in SQL
In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
4 min read
How to Copy Data From One Column to Another in the Same Table in SQL?
Efficiency in data manipulation is crucial while using Structured Query Language (SQL). To manage a wide range of tasks, including organizing, retrieving, updating, and deleting data, SQL provides a comprehensive set of instructions. Among these, copying data between columns in the same table is a c
4 min read
How to Fetch Data From Two Tables Based on Date in SQL?
In SQL, fetching data from multiple tables based on a specific date range is a common task. By using certain SQL operations, we can combine data from two different tables and filter the results based on a date condition. This method is particularly useful when working with multiple datasets and we n
3 min read
How to Create a Table With a Foreign Key in SQL?
A foreign key is a column or a set of columns in one table that references the primary key of another table. Foreign keys are used to establish and enforce a link between the data in two tables, ensuring referential integrity in the relational database system. In this article, we will explain how to
5 min read