Dingus
Dingus

Reputation: 65

Compare 2 different tables and select values based in a condition

Hey I've recently started learning SQL and I'm very new to the language. The question I can't really figure out is how I should be able to compare two tables with each other and select values based on a condition.

For example if we originally have a table with the columns:

name | paycheck

and I then copy that table, before I do some manipulation on the original table. How can i then compare them and select them on lets say, if the paycheck integer has increased 5% above it's original value.

The names are the exact same in both tables, so are the amount of rows. What i want to compare, or select are the rows where the manipulated table's paychecks are bigger than the original paycheck * 1.05. Is this possible to do in SQL, specifically mariaDB?

Edit:

Some sample data:

If we have the original table:

Name   |Paycheck
Steve  |5
Hubert |10
Bjorn  |15

Then we change it's paycheck values to (given we have copied the original one):

Name   |Paycheck
Steve  |7
Hubert |20
Bjorn  |12

Then I would want to SELECT names where their paycheck has increased by over 50%, other words new-paycheck > original-paycheck * 1.5. In this example the results would for example be:

Name   |Paycheck
Hubert |20

Upvotes: 0

Views: 1025

Answers (2)

The Impaler
The Impaler

Reputation: 48770

A simple join would do. Assuming your original table is called o and the latest table is called l the query could look like:

select l.*
from o
join l on l.name = o.name
where l.paycheck > 1.05 * o.paycheck

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269753

You seem to want join with filtering:

select *
from original o join
     changed c
     on o.name = c.name
where c.paycheck > o.paycheck * 1.5

Upvotes: 1

Related Questions