What will happen if I natural join both these tables? - mysql

I have two tables. Employee and Manager. Employee table stores the information of all the employees of the company. Manager table shows which employees work under which manager. Manager is also an employee of the company. The tables are given below.
employee(name, city, street)
manager(name, employee_name)
Bold means they are the primary key. Now the thing is that, in the manager table, both name and employee name is a foreign key referencing employee. So what will happen if I natural join them. Wouldn't there be any problem? After natural joining them, whose city and street will I see in the final table? manager's or employee's?

You'd most likely nothing if you're lucky, or junk if you're unlucky. Natural join would do something like this:
SELECT * FROM managers LEFT JOIN employees
WHERE managers.name = employees.name
Natural join attempts to use COMMONLY NAMED COLUMNS.
When what I assume you want something more like this:
SELECT * FROM managers LEFT JOIN employees
WHERE managers.employee_name = employees.name

You will get a list of all the managers city, street, and employee_name
Name, City, Street, Employee_Name

Otherwise the join stuff, I suggest to review the arquitecture of your DB.
If in the manager table, the name is PK, you can't have more then one employee with the same manager..

Related

Simple SQL 2 Tables data grouping

I need to complete the following task in SQL:
Show the employment status in individual offices of the company in the following layout: office code, country, number of employees.
Table employees:
Table offices:
My idea was:
SELECT officeCode, (SELECT country FROM offices), COUNT(*)
FROM employees
GROUP BY officeCode
But it seems it doesn't work... Can you help?
Your main data set would be the office. Join to the employee's table by the office code. Then from your main table, get the data you need and then do a count( employee number)
Remember to add all the fields that you are displaying to the group by.

Inner join in mysql take a long time

I have table contacts with more than 1,000,000 and other table cities which have about 20,000 records. Need to fetch all cities which have used in contacts table.
Contacts table have following columns
Id, name, phone, email, city, state, country, postal, address, manager_Id
cities table have
Id, city
I used Inner join for this, but its taking a long time to go. Query takes more than 2 minutes to execute.
I used this query
SELECT cities.* FROM cities
INNER JOIN contacts ON contacts.City = cities.city
WHERE contacts.manager_Id= 1
created index on manager_Id as well. But still its very slow.
for better performance you could add index
on table cities column city
on table contacts a composite index on columns (manager_id, city)
Filter contacts first and then join to cities:
SELECT ct.*
FROM cities ct INNER JOIN (
SELECT city FROM contacts
WHERE manager_Id = 1
) cn ON cn.city = ct.city
You need indexes for city in both tables and for manager_id in contacts.
As others have pointed out about having proper index, I am taking it a bit more for clarification. You are specifically looking for contacts where the MANAGER ID = 1. This is not expected to be one person, but could be many people. So having the MANAGER ID in the first position will optimize get me all people for that manager. By having the city as part of the index via (manager_id, city), you are pulling the two data elements you need to optimize as part of the index. This way the engine does not have to go to the raw data pages to get the other part of interest.
Now, From that, you want all the city information (hence the join to city table on that ID).
Since you are only querying the CITIES and not the actual contact people information, you probably want to have DISTINCT City ID. Lets say a manager is responsible for 50 people and most of them live in the same city or neighboring. You may have 5 distinct cities? That too will limit your result set of joining.
Having said that, I would do a follows, and with MySQL, using STRAIGHT_JOIN can help optimize by "do the query as I wrote it, don't think for me".
select STRAIGHT_JOIN
cty.*
from
( select distinct c.City
from Contacts c
where c.Manager_ID = 1 ) PQ
JOIN Cities cty
on PQ.City = Cty.City
The "PQ" is an alias representing my "pre-query" of just DISTINCT cities for a given manager.
Again, have one index on Contacts table on (manager_id, city). On the city table, I would expect and index on (city).
You need two indexes, one on each table.
On the contacts table, first index manager_Id, then City
CREATE INDEX idx_contacts_mgr_city ON contacts(manager_Id, City);
On the cities table, just index `City.
Is the 'City' field from the table 'Contacts' a VARCHAR?
If that's the case, I see multiple things here.
First of all, since you have already have the 'Id' for the corresponding city in your 'cities' tables, I don't see why not to use the same 'Id' from the 'cities' table for the 'Contacts' table.
You can add the 'IdCity' field to the 'Contacts' table so you don't have to modify your existing records.
You'll have to insert the 'IdCity' manually though for each of your records, or you can create a Query using 'cities' table and then compare the 'idCity' but insert the 'city' (city name) in your 'Contacts' table.
Returning to your query:
Then, use an INT JOIN instead of a VARCHAR JOIN. Since you have many records, this can show up an important significance in performance.
It looks like you need to add two indexes, one on cities.city and one on (contacts.manager_Id, contacts.city). That should speed things up significantly.

How to join employees to orders?

I am simply not understanding joins very well at all!
My exercise calls for:
Display the employee’s last name and first name, city, country and ship city for orders where the employees live in the city where the order is delivered.
I have started my answer thus far here:
SELECT
Employees.LastName,
Employees.FirstName,
Employees.City,
Employees.Country,
Orders.ShipCity
FROM
Employees,
Orders
But then I realise that I am stuck and do not know where to go from here. Do I need to find a table that has common entries in both in order to find primary keys and foreign keys? What is my solution here?
For reference, here is an image of where I am taking data from for my solutions:
SELECT
Employees.LastName,
Employees.FirstName,
Employees.City,
Employees.Country,
Orders.ShipCity
FROM
Employees inner join Orders
on Employees.EmployeeID= Orders.EmployeeID;

Comparing corresponding rows from the same column and same table

My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name

Database schema & pulling info

I'm far from being a database expert, so please feel free to let me know I'm doing it entirely wrong. I'm trying to create a set of tables that has the following basic rules:
There are companies, managers and employees. Managers can only belong to one company, but employees can belong to more then one manager. The table structure I've come up with is something like this:
# Companies
company_id
company_name
# Managers
manager_id
company_id
# Employees
employee_id
company_id
employee_name
# Managed By
employee_id
manager_id
Does this structure seem reasonable? I thought I need something like "Managed By" since an employee can have multiple managers.
What I'm boggling on is now how do I manage to grab the records I'd want. For example:
Get all employee names belonging to a certain manager
Get all employee names belonging to two certain managers
All employees to the same company
edit: I think I'm getting the queries figured out but this still feels unweidy, so any help on the table structure would be appreciated.
The fact I'm having problems writing queries for this information makes me think I've done something fundamentally wrong with the table structure. Hopefully someone here can set me right?
Generally, the scheme is correct. The one possibility that does exist, however, is that you could have data where an employee is managed by managers at more than two companies.
For your queries:
select distinct Employees.employee_name as name from Employees, Managers, ManagedBy where Managers.manager_id = X and ManagedBy.manager_id = Managers.manager_id and Employees.employee_id = ManagedBy.employee_id;
and
select distinct Employees.employee_name as name from Employees, Managers, ManagedBy where (Managers.manager_id = X or Managers.manager_id = Y) and ManagedBy.manager_id = Managers.manager_id and Employees.employee_id = ManagedBy.employee_id;
where X and Y are the manager IDs you want.
edit: I know you crossed out the queries you wanted, but the third one is this:
select distinct Employees.employee_name as name from Employees where Employees.company_id = Z;
where Z is the company ID you want.
All employees to the same company
SELECT employee_id, employee_name, company_name
FROM Employees
LEFT JOIN Companies ON Employees.company_id = Companies.company_id
WHERE Companies.company_id = ????
or if you pulling by name
WHERE Companies.company_name = 'ABC'
Get all employee names belonging to a certain manager
SELECT employee_id, employee_name, manager_id
FROM Employees
LEFT JOIN Managed_By ON Employees.employee_id = Managed_By.employee_id
WHERE Managed_By.manager_id = ????
manager_name in Managers table would be nice to have