I'm currently learning SQL and came across an obstacle.
I have been looking around the web and found no case where the table is being "replicated" as is my case.
The case is based on a classical example where we want to have a result where we get the employee name and the next column with the supervisor name.
Desired result:
|Employee Name | Supervisor Name|
---------------------------------
|Dave | NULL |
|Rick | Dave |
|Andy | Rick |
The current table goes like this (employee):
|emp_id | first_name | last_name| super_id |
--------------------------------------------
|100 | Dave | Star | NULL |
|101 | Rick | Port | 100 |
|101 | Andy | Huds | 101 |
I'm trying to do this with a SELF JOIN in POPSQL (MySQL based) but first I will show how the employee table was created.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
super_id INT,
);
ALTER TABLE employee
ADD FOREIGN KEY (super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL;
This is the code I'm using to make the query:
SELECT m.first_name, s.first_name
FROM employee AS m
INNER JOIN employee AS s
ON m.super_id = s.emp_id;
And below the result I'm getting.
Notice how the table has two problems.
First, each row seems to be repeated (David appears in two rows).
Second, the emp_id is not matched to sup_id (as per the ON m.super_id = s.emp_id).
|emp_id | first_name |
----------------------
|David | David |
|David | David |
|Rick | Rick |
|Rick | Rick |
Could somebody help me?
Thanks
if you need to get all records (including Dave), you need to use OUTER JOIN
SELECT m.first_name AS EmployeeName,
s.first_name AS SupervisorName
FROM employee AS m
LEFT JOIN employee AS s
ON m.super_id = s.emp_id;
SELECT m.first_name, s.first_name
FROM employee AS m
left JOIN employee AS s
ON m.super_id = s.emp_id;
Related
I want to create a new table using the results from some queries. I might be looking at this the wrong way so please feel free to let me know. Because of this I will try to make this question simple without putting my code to match each employee number with each manager level column from table2
I have two tables, one has employee names and employee numbers example
table 1
+-------------+-----------+-------------+-------------+
| emplpyeenum | firstname | last name | location |
+-------------+-----------+-------------+-------------+
| 11 | joe | free | JE |
| 22 | jill | yoyo | XX |
| 33 | yoda | null | 9U |
+-------------+-----------+-------------+-------------+
and another table with employee numbers under each manager level so basically a hierarchy example
Table 2
+---------+----------+----------+
| manager | manager2 | manager3 |
+---------+----------+----------+
| 11 | 22 | 33 |
+---------+----------+----------+
I want to make a new table that will have the names besides the numbers, so for example but with employee number beside the names
+---------+--------+----------+
| level 1 | level2 | level3 |
+---------+--------+----------+
| jill | joe | yoda |
+---------+--------+----------+
How can I do this?
edit sorry guys I don't have permission to create a new table or view
Why not change your table2 to this?
+------------+----------+
| EmployeeId | ManagerId|
+------------+----------+
| 11 | NULL |
+------------+----------+
| 22 | 11 |
+------------+----------+
| 33 | 22 |
+------------+----------+
Then you can do what you want with the data. At least your data will be properly normalized. In your table2. What happen if employee 33 hire another employee below him? You will add another column?
Based on your available table, this should give you the result you want.
SELECT m1.firstname, m2.firstname, m3.firstname
FROM table2 t
LEFT JOIN table1 m1 ON m1.employeenum = t.manager
LEFT JOIN table1 m2 ON m2.employeenum = t.manager2
LEFT JOIN table1 m3 ON m3.employeenum = t.manager3
You can just do a basic create table, then do a insert select to that will fill the table the way you need it. All you have to do is replace the select statement that I provided with the one you used to create the levels table output.
create table Levels
(
level1 varchar(25),
level2 varchar(25),
level3 varchar(25)
)
insert into Levels(level1, level2, level3)
select * from tables --here you would put the select statement that you used to create the information. If you dont have this script then let me know
I have, let's say, two tables, employee and salary:
employee salary
|---------|---------| |--------|----------|----------|
| ID | Name | | E_ID | Amount | Status |
|-------------------| |------------------------------|
| 1 | Matt | | 1 | 100 | Past |
| 2 | John | | 1 | 120 | Current |
| 3 | David | | 2 | 150 | Current |
|---------|---------| |--------|----------|----------|
and I require a join between them that will return me the employee-salary info: null if it's not in the salary table, just one value if it's repeated (the one marked as 'current' in opposition to 'past').
In this example, my required return would be:
|---------|---------------|---------------|
| ID | Name | Amount |
|---------|---------------|---------------|
| 1 | Matt | 120 |
| 2 | John | 150 |
| 3 | David | NULL |
|---------|---------------|---------------|
I initially tried with a
WHERE employee.ID=salary.E_ID
but that would leave David out.
I tried a LEFT JOIN
...
employee
LEFT JOIN salary ON employee.ID=salary.E_ID
but that shows Matt twice, and if I add the where clause
...
employee
LEFT JOIN salary ON employee.ID=salary.E_ID
WHERE salary.status='Current'
that leaves David out again.
Not sure how to proceed.
Thank you for your help.
LEFT JOIN salary
ON employee.ID = salary.E_ID
AND salary.status = 'Current'
The given answer by Paparazzi is the way I'd do it too, but purely for information, another way to do this is is like this:
For your last example that ignores David again; if you use WHERE isnull(salary.status, 'Current') ='Current' instead like this:
...
employee
LEFT JOIN salary ON employee.ID=salary.E_ID
WHERE isnull(salary.status, 'Current')='Current'
Then, this will still include him, as we are saying, although the left join is not bringing back anything, treat the null as 'current' anyway, and match the where clause on this.
You should use the condition for salary.status in on clause
if you let this condition in where this work as a inner join and don't return the valye for all the employee
select employee.name, salary.amount
from employee
LEFT JOIN salary ON employee.ID=salary.E_ID AND salary.status='Current'
I have two tables I am using at work to help me gain experience in writing SQL queries. One table contains a list of Applications and has three columns -
Application_Name, Application_Contact_ID and Business_Contact_ID. I then have a separate table called Contacts with two columns - Contact_ID and Contact_Name. I am trying to write a query that will list the Application_Name and Contact_Name for both the Applications_Contact_ID and Business_Contact_ID columns instead of the ID number itself.
I understand I need to JOIN the two tables but I haven't quite figured out how to formulate the correct statement. Help Please!
APPLICATIONS TABLE:
+------------------+------------------------+---------------------+
| Application_Name | Application_Contact_ID | Business_Contact_ID |
+------------------+------------------------+---------------------+
| Adobe | 23 | 23 |
| Word | 52 | 14 |
| NotePad++ | 44 | 989 |
+------------------+------------------------+---------------------+
CONTACTS TABLE:
+------------+--------------+
| Contact_ID | Contact_Name |
+------------+--------------+
| 23 | Tim |
| 52 | John |
| 14 | Jen |
| 44 | Carl |
| 989 | Sam |
+------------+--------------+
What I am trying to get is:
+------------------+--------------------------+-----------------------+
| Application_Name | Application_Contact_Name | Business_Contact_Name |
+------------------+--------------------------+-----------------------+
| Adobe | Tim | Tim |
| Word | John | Jen |
| NotePad++ | Carl | Sam |
+------------------+--------------------------+-----------------------+
I've tried the below but it is only returning the name for one of the columns:
SELECT Application_Name, Application_Contact_ID, Business_Contact_ID, Contact_Name
FROM Applications
JOIN Contact ON Contact_ID = Application_Contact_ID
This is a pretty critical and 101 part of SQL. Consider reading this other answer on a different question, which explains the joins in more depth. The trick to your query, is that you have to join the CONTACTS table twice, which is a bit hard to visualize, because you have to go there for both the application_contact_id and business_contact_id.
There are many flavors of joins (INNER, LEFT, RIGHT, etc.), which you'll want to familiarize yourself with for the future reference. Consider reading this article at the very least: https://www.techonthenet.com/sql_server/joins.php.
SELECT t1.application_name Application_Name,
t2.contact_name Application_Contact_name,
t3.contact_name Business_Contact_name
FROM applications t1
INNER JOIN contacts ON t2 t1.Application_Contact_ID = t2.contact_id -- join contacts for appName
INNER JOIN contacts ON t3 t1.business_Contact_ID = t3.contact_id; -- join contacts for busName
Thanks in advance for any assistance you might be able to give. I think I may be asking a trivial question but I cannot determine how I would do this nor what exactly to search for. So my apologies if your time is wasted.
So I have two tables TableA and TableB and I need to join the two sets of data in a specific manner that I will explain below with reference to some example tables.
TableA:
Name | Contact_1 | Contact_2 | Contact_3
----------------------------------------
Joe | Anne | Sue | Phil
Dan | Tom | |
May | Jeff | Pete | Tim
etc.
TableB:
Name | Contact_No
-----------------
Anne | 123456789
Sue | 234567891
Phil | 345678912
Tom | 456789123
Jeff | 567891234
Pete | 678912345
Tim | 789123456
Resulting Table when Joining between TableA and TableB on:
TableA.Contact_1 = TableB.Name
AND
TableA.Contact_2 = TableB.Name
AND
TableA.Contact_3 = TableB.Name
gives:
Name | Contact_1 | ContactNo_1 | Contact_2 | ContactNo_2 | Contact_3 | ContactNo_3
----------------------------------------
Joe | Anne | 123456789 | Sue | 234567891 | Phil | 345678912
Dan | Tom | 456789123 | | | |
May | Jeff | 567891234 | Pete | 678912345 | Tim | 789123456
This is just a trivial example to explain my problem. Sorry for such a lengthy explanation but I would rather have a detailed example to limit confusion.
Thanks!
You have to join TableA with TableB three times:
select ta.name,
ta.contact_1, tb1.contact_no,
ta.contact_2, tb2.contact_no,
ta.contact_3, tb3.contact_no
from TableA ta
left join TableB tb1 on ta.contact_1 = tb1.name
left join TableB tb2 on ta.contact_2 = tb2.name
left join TableB tb3 on ta.contact_3 = tb3.name;
If there are null values allowed in TableA.contact_1 (_2, _3), then you need left joins.
EDIT
Notice, however, that it is usually better to have a separate table that keeps the fact that a pair (name, name) is connected, e.g.,
create table TableA(a_name varchar primary key);
create table TableB(b_name varchar primary key,
b_contact_no varchar);
create table Contacts(a_name varchar,
b_name varchar,
primary key(a_name,b_name),
foreign key(a_name) references TableA(a_name),
foreign key(b_name) references TableB(B_name));
That way, you can model an arbitray number of contacts without having to change your database schema.
Hi New comer to Stack overflow so if I do not present this correctly I am sorry.
I have used Google, W3schools and read the FQA on SQL.
I am running SQL using the SQL command line in WAMP2.0. I am currently doing a project where the aim is to create a min University DB. With students, grades, programmes, modules ect
One of the tasks is to to list all the students, there modules and there correspoding grades. To do this I am trying to use a JOIN command to select all the names from the Students table, with all there corresponding modules + grades from the records table.
+------------+-------+------------+-----------------+
| Student_id | Name | DOB | Address |
+------------+-------+------------+-----------------+
| 4665236 | Paddy | 1985-09-18 | 123 Fake Street |
| 5665236 | Paul | 1984-06-12 | Good manlane |
| 6665236 | John | 1984-03-09 | Docotor town |
| 7665236 | Aidan | 1983-07-09 | Banker worlds |
| 8665236 | Joe | 1983-07-09 | 24 hitherwood |
+------------+-------+------------+-----------------+
+------------+--------+------+-------+
| Student_id | Mod_id | GPA | Grade |
+------------+--------+------+-------+
| 4655236 | 2222 | 3.84 | A- |
| 5655236 | 11111 | 3.44 | B+ |
| 6655236 | 33333 | 3.24 | B |
| 7655236 | 44444 | 2.45 | C- |
| 8655236 | 44444 | 2.45 | C- |
+------------+--------+------+-------+
The PRIMARY KEY in the students table is Student_id INT 11
The PRIMARY KEY for records is (Student_id,Mod_id)
Individual SELECT FROM , statements work fine on both tables.
Issue occurs when I use
SELECT students.Name, records.Grade
FROM students
INNER JOIN records
ON students.Student_id=Student_id
ORDER BY students.Name
I get the following error
ERROR 1052 (23000): Column 'Student_id' in on clause is ambiguous
Thanks for amazingly fast response I tried
SELECT students.Name, records.Grade
FROM students
INNER JOIN records
ON students.Student_id=records.Student_id
ORDER BY students.Name;
And Got ---- Empty set (0.00 sec) ?
You have to qualify that column Student_Id with an alias, something like records.studentId so that it will be un ambiguous in the ON clause, or:
SELECT s.Name, r.Grade
FROM students AS s
INNER JOIN records AS r ON s.Student_id= r.Student_id
ORDER BY s.Name
You need to supply the table name for column Student_id to avoid ambiguity because it both exist on the two tables.
SELECT students.Name, records.Grade
FROM students
INNER JOIN records
ON students.Student_id = records.Student_id -- << THIS
ORDER BY students.Name
The reason you're column name is being flagged as ambiguous is because you've got two different tables that each have the Student_id field. You can join a table to itself, so even though you've got an identifier on the first instance of the field you need one on both.
Try the following code:
SELECT students.Name, records.Grade
FROM students
INNER JOIN records
ON students.Student_id=records.Student_id
ORDER BY students.Name
You can also alias the tables if that helps your code look cleaner by using the following:
SELECT s.Name, r.Grade
FROM students s
INNER JOIN records r
ON s.Student_id=r.Student_id
ORDER BY s.Name
However, this only works if the Student IDs match in both tables. in the example data you've presented there are no matching records. 4665236 != 4655236