The Wayback Machine - https://web.archive.org/web/20240904214047/https://www.geeksforgeeks.org/how-to-compare-two-columns-for-equality-in-sql-server/
Open In App

How to Compare Two Columns For Equality in SQL Server?

Last Updated : 31 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 tables in SQL. We will understand each concept wit
4 min read
Compare SQL Server Results of Two Queries
SQL Server is a versatile database, and it is the most used Relational Database that is used across many software industries. In this article, let us see the comparison of SQL Server Results of Two Queries briefly. By using Azure Data Studio, let us see the concepts by starting with creating the database, table creation, locks, etc., Azure Data Stu
5 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 have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to Compare Rows and Columns in the Same Table in SQL
In this article, we are going to see how we can compare the rows and columns of the same table in SQL. We will be taking a few examples to see how we can do this in different ways. To create the database: Syntax : CREATE database_name;CREATE Shopping To use this database: Syntax : USE database_name;USE Shopping Creating Table for making queries: Sy
2 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 database named "geeks" in MS SQL Server and then com
3 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For t
3 min read
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 named “geeks” then we will create two tables “departme
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. Creating a Database :We use CREATE DATABASE command t
2 min read
SQL Query to Compare Two Strings
SQL stands for Structured Query Language. It is used to communicate with the database. There are some standard SQL commands like 'select', 'delete', 'alter' etc. To compare two strings in SQL Server, there is no direct way. In this article, we will learn how to compare two strings in an MS SQL server and we will provide some examples. A string func
2 min read
SQL Query to Compare Two Dates
In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. Here we will see, SQL Query to compare two dates. This can be easily done using equals
2 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
How to Compare Product Sales By Month in SQL?
A monthly sales report represents the state of sales activities in a company per month. It helps the sales team to move in the right direction. Whether you are a sales leader or manager, metrics are immensely important for your company's success. If your data is stored in a database, you can calculate the monthly sales report using SQL. In this art
3 min read
SQL Query to Compare Results With Today's Date
To compare query results with today's date, use the CASE() function with the SQL GETDATE() function. SQL GETDATE() function is used to return the present date and time of the database system and the CASE() function can used to compare two dates in SQL. Query to check the current date in SQL: SELECT GETDATE();Output: SQL Compare Results With Today's
1 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
How to Select Data Between Two Dates and Times in SQL Server?
In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database. Note: Here, we will use the two dates and times given in the query and separate them using the BETWEEN keyword. This i
2 min read
How to Update Two Tables in One Statement in SQL Server?
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server. SyntaxUpdating two tables in one statement in SQL
3 min read
How to Compare Data in All Tables in Two Different Databases in Oracle?
Oracle is a versatile database and it is highly secured and hence to a large extent used in banking and insurance applications. Though NoSQL database dominates in many industries, still oracle has its own significance because most the legacy applications still like to go with oracle like RDBMS only. Distributed systems will have the usage of 2 or m
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 when the schema is on write. To make these systems pe
3 min read
Removing Duplicate Rows (Based on Values from Multiple Columns) From SQL Table
In SQL, some rows contain duplicate entries in multiple columns(>1). For deleting such rows, we need to use the DELETE keyword along with self-joining the table with itself. The same is illustrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to crea
3 min read
SQL Query to Delete Duplicate Columns
Through this article, we will learn how to delete duplicate columns from a database table. As we know that duplicity in our database tends to be a waste of memory space. It records inaccurate data and is also unable to fetch the correct data from the database. To remove the duplicate columns we use the DISTINCT operator in the SELECT statement as f
2 min read
SQL Query to Find the Number of Columns in a Table
SQL stands for a structure query language, which is used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. Similarly when we want any data from the database t
4 min read
Using CASE in ORDER BY Clause to Sort Records By Lowest Value of 2 Columns in SQL
In this article, we will see how to use CASE in the ORDER BY clause to sort records by the lowest value of 2 columns in SQL. CASE statement: This statement contains one or various conditions with their corresponding result. When a condition is met, it stops reading and the corresponding result gets returned (similar to the IF-ELSE statement). It re
2 min read
How to Update Multiple Columns in Single Update Statement in SQL?
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required. UPDATE for Multiple ColumnsSyntax: U
3 min read
How to Select Individual Columns in SQL?
In SQL, sometimes we require to select individual columns from a table. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Select is the most commonly used statement in SQL. The SELECT Statement in SQL is used to retrieve or fetc
2 min read
Article Tags :