Search code examples
sqlt-sqljoinauto-populating

SQL joining two tables


I have 2 tables employees(id and name) and salary(id, salary), the 1st has 4 rows and 2nd has 2 rows.

table 1            Table 2
id   Name         id    salary
1     Sue          1    10000 
2    Sarah         3     9000
3    Nick 
4    james 

I want a join as follows

id   Name     Salary
1     Sue     10000
2    Sarah    No Salary
3    Nick     9000
4    james    No salary

Solution

  • This should do the trick.

    SELECT e.id, e.name , s.salary FROM employees e 
    LEFT JOIN salary s
    ON e.id=s.id
    ORDER BY e.id ASC