68. Employees With or Without Salary Records

An HR system stores employee details and salary information in separate tables.

Due to synchronization issues:

  • Some employees may be missing salary records.
  • Some salary records may not have matching employee details.

Given two tables

Employees

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘ employee_id ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘    name     ā•‘ varchar  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • employee_id is the primary key.
  • Each row represents an employee.

Salaries

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘ employee_id ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘   salary    ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • employee_id is the primary key.
  • Each row represents a salary record.

Write an SQL query to return all employee IDs, including:

  • Employees who have a salary
  • Employees who do not have a salary
  • Salary records that do not have a matching employee

For each row, return:

  • employee_id
  • name
  • salary

Return the result ordered by employee_id in ascending order.

Example 1:

Input:

Employees:

╔═════════════╦══════════╗
ā•‘ employee_id ā•‘   name   ā•‘
╠═════════════╬══════════╣
ā•‘      1      ā•‘  Aarav   ā•‘
║─────────────┼──────────║
ā•‘      2      ā•‘   Neha   ā•‘
║─────────────┼──────────║
ā•‘      3      ā•‘  Rohan   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Salaries:

╔═════════════╦══════════╗
ā•‘ employee_id ā•‘  salary  ā•‘
╠═════════════╬══════════╣
ā•‘      1      ā•‘  50000   ā•‘
║─────────────┼──────────║
ā•‘      4      ā•‘  65000   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Output:

╔═════════════╦══════════╦══════════╗
ā•‘ employee_id ā•‘   name   ā•‘  salary  ā•‘
╠═════════════╬══════════╬══════════╣
ā•‘      1      ā•‘  Aarav   ā•‘  50000   ā•‘
║─────────────┼──────────┼──────────║
ā•‘      2      ā•‘   Neha   ā•‘   NULL   ā•‘
║─────────────┼──────────┼──────────║
ā•‘      3      ā•‘  Rohan   ā•‘   NULL   ā•‘
║─────────────┼──────────┼──────────║
ā•‘      4      ā•‘   NULL   ā•‘  65000   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation

  • Employee 1 has both records, full data
  • Employees 2 and 3 have no salary, salary is NULL
  • Salary record for employee 4 has no employee, name is NULL

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Employees
Salaries