Search code examples
mysqlsqlinner-join

SQL Retrieve info from two tables


I am really lost with sql.. i tried to read everything but no luck.

I have two tables and need to retrieve data from them:

First Table - Workers | Second Table - Stores

+--------+------------+-----------+
| Worker | First_Name | Last_name |
+--------+------------+-----------+
|     10 | John       | Smith     |
|     20 | Robert     | Henry     |
|    100 | Jessica    | Bailey    |
|    120 | Kelly      | Bates     |
+--------+------------+-----------+

+--------+---------+--+
| Worker |  Store  |  |
+--------+---------+--+
|     10 | Shoe    |  |
|     20 | Clothes |  |
|    100 | Shoe    |  |
|    120 | HR      |  |
|    120 | Shoe    |  |
+--------+---------+--+

Select workers first name that belong to more than one store?

Select all workers first name that are in the shoe store?

Hope you can help me.

Thanks


Solution

  • Select workers first name that belong to more than one store

    select Workers.First_Name
    from Workers
    join Stores on Stores.Worker = Workers.Worker
    group by Workers.First_name
    having count(*) > 1
    

    Select all workers first name that are in the shoe store

    select Workers.First_Name
    from Workers
    join Stores on Stores.Worker = Workers.Worker
    where Stores.Store = 'Shoe'