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 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 |
Contact Us