Sql query to join two tables and display all records from 1 column

I have two tables in the sql server - one with 51 American states, and the other with a name, state. a table with the name state has different records, namely -

Seere -- AK
Seere -- LA
Seere -- CA
John  -- HI
John  -- MA

I want the query that selects one name to be called "Seere" and show all the states from the state table and the name attached to those states that are from the second table, therefore

null -- AR
Seere -- AK
Seere -- LA
Seere -- CA
null -- MA
null -- CO

same for all names, I just select one name and show all the states. any ideas?

+3
source share
2 answers
SELECT  *
FROM    states s
LEFT JOIN
        names n
ON      n.name = 'Seere'
        AND n.state = s.state
+1
source

Use an external connection:

select name_state.name, state_table.state
  from state_table
    left outer join name_state
      on (state_table.state = name_state.state)
  where name_state.name = "Seere"
0
source

Source: https://habr.com/ru/post/1733392/


All Articles