﻿ SQL Challenges-1: Combine two tables - w3resource

# SQL Challenges-1: Combine two tables

## SQL Challenges-1: Exercise-1 with Solution

From the following tables, write a SQL query to find the information on each salesperson of ABC Company. Return name, city, country and state of each salesperson.

Input:

Table: salespersons

Structure:

FieldTypeNullKeyDefaultExtra
salesperson_idint(11)YES
first_namevarchar(255)YES
last_namevarchar(255)YES

Data:

salesperson_idfirst_namelast_name
1GreenWright
2Jones Collins
3BryantDavis

Sturucture:

FieldTypeNullKeyDefaultExtra
salesperson_idint(11)YES
cityvarchar(255)YES
statevarchar(255)YES
countryvarchar(255)YES

Data:

12Los AngelesCaliforniaUSA
34AtlantaGeorgiaUSA

Sample Solution:

SQL Code(MySQL:

``````CREATE TABLE IF NOT EXISTS salespersons(salesperson_id int, first_name varchar(255), last_name varchar(255));
CREATE TABLE IF NOT EXISTS address (address_id int, salesperson_id  int, city varchar(255), state varchar(255), country varchar(255));
INSERT INTO salespersons (salesperson_id, first_name, last_name) VALUES ('1', 'Green', 'Wright');
INSERT INTO salespersons (salesperson_id, first_name, last_name) VALUES ('2', 'Jones', 'Collins');
INSERT INTO salespersons (salesperson_id, first_name, last_name) VALUES ('3', 'Bryant', 'Davis');

INSERT INTO address (address_id, salesperson_id, city, state, country) VALUES ('1', '2', 'Los Angeles','California', 'USA');
INSERT INTO address (address_id, salesperson_id, city, state, country) VALUES ('3', '4', 'Atlanta', 'Georgia','USA');

select * from salespersons;

SELECT first_name, last_name, city, state
```
```

Sample Output:

```first_name|last_name|city       |state     |
----------|---------|-----------|----------|
Jones     |Collins  |Los Angeles|California|
Green     |Wright   |           |          |
```

SQL Code Editor: