How to Compare Two Columns For Equality in SQL Server?
Last Updated :
31 Mar, 2023
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 * FROM TABLE_NAME WHERE COLUMN1_NAME=COLUMN2_NAME;
There is a table called COLLEGE. The table consists of professor name, the subject that the professor is teaching currently and the subject in which the professor specializes. Then director of the college decides to promote some of the professors to the post of dean but on the condition that their current subject should match with the specialization subject of the professor. Create a table and write an SQL query to demonstrate this.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table COLLEGE inside the database GeeksForGeeks. This table has 3 columns namely PROF_NAME, CURR_SUBJ and SPEC_SUBJ containing professor name, current subject that he/she is teaching and the subject in which he/she specializes.
Query:
CREATE TABLE COLLEGE(
PROF_NAME VARCHAR(20),
CURR_SUBJ VARCHAR(20),
SPEC_SUBJ VARCHAR(20));
Output:

Step 4: Display the structure of the COLLEGE table.
Query:
EXEC SP_COLUMNS COLLEGE;
Output:

Step 5: Insert 5 rows into the COLLEGE table.
Query:
INSERT INTO COLLEGE VALUES('BHARGAV','ELECTRO','FLUIDS');
INSERT INTO COLLEGE VALUES('ABHISHEK','SOFTWARE','SOFTWARE');
INSERT INTO COLLEGE VALUES('SUDHARSHAN','TRANSFORMERS','CIRCUITS');
INSERT INTO COLLEGE VALUES('RAKESH','ORGANIC','ORGANIC');
INSERT INTO COLLEGE VALUES('DEEPAK','OOPS','ALGORITHMS');
Output:

Step 6: Display all the rows of the COLLEGE table.
Query:
SELECT * FROM COLLEGE;
Output:

Step 7: Display the details of the professor who can be promoted to the position of the dean i.e. the current subject should match the specialization subject.
Query:
SELECT * FROM COLLEGE WHERE CURR_SUBJ=SPEC_SUBJ;
Output:

Thus, in the above-stated ways, we can compare any two columns for equality in SQL as and when needed.
Similar Reads
How to compare columns in two different tables in SQL 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
4 min read
SQL Query to Convert Rows to Columns in SQL Server In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we h
2 min read
How to Get the Data Type of Columns in SQL Server? SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. SQL Server offers the SQL Server Management Studio which defines the database development and administration. In this article, we will learn how to retrieve the dat
4 min read
How to Compare Two Columns in Excel : Easy and Quick Methods In the world of data analysis, knowing how to compare two columns in Excel is a fundamental skill that can unveil valuable insights and ensure data accuracy. If you're looking to spot differences, find duplicates, or identify similarities, mastering Excel's compare columns functionality is key. This
8 min read
How to Compare Time in MS SQL Server? To compare time in MS SQL Server, use the comparison operators (=,<,>, etc.). In this article, we will be making use of the Microsoft SQL Server as our database. and comparing times using both pre-defined dates and the current date and time with the GETDATE() function.First, let's create a dat
3 min read
How to Compare Two Queries in SQL Queries in SQL :A query will either be an invitation for data results from your info or for action on the info, or each. a question will provide you with a solution to a straightforward question, perform calculations, mix data from totally different tables, add, change, or delete data from info. Cre
2 min read