SQL Challenge #2

Let's take a look at some information requirements and how the expected output information could be generated using SQL. For this challenge, let's use the snapshot of a database design (shown below)

Suppose we were asked to generate this information -- "List the employees who are 1-2 years older/younger than their spouse. The report should contain the name of the employee and the name of the spouse"

When we work on the SQL statement that will generate this requirement, what is the first thing we generate mentally? Imagine the output? That's right. Imagining the output end in mind will help you in writing SQL statements. For more details about the steps that we will be doing here, you can read the blog post on Writing SQL statements.

Going back to our problem, if you are imagining the output information shown below, then you are doing just fine so far.

How did you get that mentally? Definitely it involved you extracting, comparing and all sorts of processing in your head to get this output information. That logic needs to be translated to some structure like SQL.