Open In App

How to Compare Two Queries in SQL

Last Updated : 08 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 to create a new SQL database.

Syntax –

CREATE DATABASE db_name;

Creating a Table into a created Database :
We use the CREATE TABLE command to create a new SQL database.

Syntax  –

CREATE TABLE table_name (
   col1 datatype,
   col2 datatype, 
   col3 datatype,
 );

Inserting the values into created Table :
We use INSERT INTO command to create a new SQL database.

Syntax –

INSERT INTO table_name
VALUES (value1, value2, value3);

Example Code to create a database and a table into it –

PHP




CREATE DATABASE myDatabase;
  
CREATE TABLE myTable
(
Pid int,
FName varchar(255),
LName varchar(255),
Adrs varchar(255),
District varchar(255)
);
  
INSERT INTO myTable (Pid, FName, LName, Adrs, District)
VALUES ('1','Krishna','Kripa','Jansa','Varanasi');


Output –

myDatabase: myTable

Pid

FName

LName

Adrs

District

1

Krishna

Kripa

Jansa

Varanasi

Comparison of Queries :
For example, we’ve 2 similar tables in completely different databases and we wish to understand what’s different. Here are the scripts that make sample databases, tables, and information.

PHP




CREATE DATABASE myDatabase1;
GO
USE myDatabase1;
GO
  
CREATE TABLE myTable
(
Aid int,
Atype varchar(10),
Acost varchar(10)
);
  
GO
  
INSERT INTO myTable (Aid, Atype, Acost)
  VALUES ('001', '1', '40'),
  ('002', '2', '80'),
  ('003', '3', '120')
GO
  
CREATE DATABASE myDatabase2;
GO
USE myDatabase2;
GO
  
CREATE TABLE myTable
(
Aid int,
Atype varchar(10),
Acost varchar(10)
);
  
GO
  
INSERT INTO myTable (Aid, Atype, Acost)
  VALUES ('001', '1', '40'),
  ('002', '2', '80'),
  ('003', '3', '120'),
  ('004', '4', '160')
  
GO


Output –
For myDatabse1 –

Aid

Atype

Acost

001

1

40

002

2

80

003

3

120

For myDatabase2 –

Aid

Atype

Acost

001

1

40

002

2

80

003

3

120

004

4

160

Compare SQL Queries in Tables by using the EXCEPT keyword :
EXCEPT shows the distinction between 2 tables. it’s wont to compare the variations between 2 tables.

Now run this query where we use the EXCEPT keyword over DB2 from DB1 –

PHP




SELECT * FROM myDatabase2.myTable
EXCEPT
SELECT * FROM myDatabase1.myTable


Output –

Aid

Atype

Acost

004

4

160



Next Article
Article Tags :

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. Overv...
15+ min read
Joining 4 Tables in SQL
The purpose of this article is to make a simple program to Join two tables using Join and Where clause in SQL. Below is the implementation for the same using MySQL. The pr...
15+ min read
Boutique Management System using Python-MySQL Connectivity
In this article, we are going to make a simple project on a boutique management system using Python MySql connectivity. Introduction This is a boutique management system m...
15+ min read
How to Convert SQL to LINQ Query?
A group of technologies known as Language-Integrated Query (LINQ) is built on the direct integration of query functionality into the C# language. The query expression is t...
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 inser...
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 compar...
2 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...
5 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 betwee...
2 min read
SQL | Sub queries in From Clause
From clause can be used to specify a sub-query expression in SQL. The relation produced by the sub-query is then used as a new relation on which the outer query is applied...
2 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 align their efforts effectively. Whether we are a sales...
15+ min read