Issue with JOIN command - sql

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

Related

Why the output of a SELECT can be another SELECT?

I am rather confused about the following SQL query:
SELECT (SELECT S.name FROM student AS S
WHERE S.sid = E.sid) AS sname
FROM enrolled as E
WHERE cid='15-455';
SELECT should be followed by an output, but why here there is another SELECT? How to understand the step-by-step meaning of this query?
The following is the query that has the same result of the above query, but its meaning is rather explicit: the output of the second SELECT is passed into the IN() function.
SELECT name FROM student
WHERE sid IN (
SELECT sid FROM enrolled
WHERE cid = '15-445'
);
Here are the original tables of this question:
mysql> select * from student;
+-------+--------+------------+------+---------+
| sid | name | login | age | gpa |
+-------+--------+------------+------+---------+
| 53666 | Kanye | kayne#cs | 39 | 4.00000 |
| 53688 | Bieber | jbieber#cs | 22 | 3.90000 |
| 53655 | Tupac | shakur#cs | 26 | 3.50000 |
+-------+--------+------------+------+---------+
mysql> select * from enrolled;
+-------+--------+-------+
| sid | cid | grade |
+-------+--------+-------+
| 53666 | 15-445 | C |
| 53688 | 15-721 | A |
| 53688 | 15-826 | B |
| 53655 | 15-445 | B |
| 53666 | 15-721 | C |
+-------+--------+-------+
mysql> select * from course;
+--------+------------------------------+
| cid | name |
+--------+------------------------------+
| 15-445 | Database Systems |
| 15-721 | Advanced Database Systems |
| 15-826 | Data Mining |
| 15-823 | Advanced Topics in Databases |
+--------+------------------------------+
In real life I'd say both queries are just two creepy ways to avoid joins.
But in this particular case they were included in the slides you've found in order to show in how many place nested loops can be used.
They all do the same thing as the following
SELECT name
FROM student s
JOIN enrolled e
ON s.sid = e.sid
WHERE cid = '15-445';
As for your question about step-by-step meaning of the first query. It is the following
This will loop through every record from "enrolled" table that has cid = '15-455'.
FROM enrolled as E
WHERE cid='15-455';
For every record from step 1 it will perform the following query
SELECT S.name
FROM student AS S
WHERE S.sid = E.sid;
This construct:
SELECT (SELECT S.name FROM student S WHERE S.sid = E.sid) AS sname
-------^
is called a scalar subquery. This is a special type of subquery that has two important properties:
It returns one column.
It returns at most one row.
In this case, the scalar subquery is also a correlated subquery meaning that it references columns in the outer query, via the where clause.
A scalar subquery can be using almost anywhere that a scalar (i.e. constant value) can be used in a query. They can be handy. They are not exactly equivalent to a join, because:
An inner join can filter values. A scalar subquery returns NULL if there are no rows returned.
A join can multiply the number of rows. A scalar subquery returns an error if it returns more than one row.
If you want to get informations like :
Name of student | CID | Grade |
You can do something like :
select t.name, e.cid, e.grade
from enrolled e
inner join student t on (e.sid = t.sid)
Or without join (for optimization) :
select (name from student t where t.sid = e.sid) as name, e.cid, e.grade
from enrolled e
so results are the same but in the second one you're avoiding joins.

SQL Self Join not "joining" in PopSQL

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;

SQL Join to the latest record in MS ACCESS

I want to join tables in MS Access in such a way that it fetches only the latest record from one of the tables. I've looked at the other solutions available on the site, but discovered that they only work for other versions of SQL. Here is a simplified version of my data:
PatientInfo Table:
+-----+------+
| ID | Name |
+-----+------+
| 1 | John |
| 2 | Tom |
| 3 | Anna |
+-----+------+
Appointments Table
+----+-----------+
| ID | Date |
+----+-----------+
| 1 | 5/5/2001 |
| 1 | 10/5/2012 |
| 1 | 4/20/2018 |
| 2 | 4/5/1999 |
| 2 | 8/8/2010 |
| 2 | 4/9/1982 |
| 3 | 7/3/1997 |
| 3 | 6/4/2015 |
| 3 | 3/4/2017 |
+----+-----------+
And here is a simplified version of the results that I need after the join:
+----+------+------------+
| ID | Name | Date |
+----+------+------------+
| 1 | John | 4/20/2018 |
| 2 | Tom | 8/8/2010 |
| 3 | Anna | 3/4/2017 |
+----+------+------------+
Thanks in advance for reading and for your help.
You can use aggregation and JOIN:
select pi.id, pi.name, max(a.date)
from appointments as a inner join
patientinfo as pi
on a.id = pi.id
group by pi.id, pi.name;
something like this:
select P.ID, P.name, max(A.Date) as Dt
from PatientInfo P inner join Appointments A
on P.ID=A.ID
group by P.ID, P.name
Both Bing and Gordon's answers work if your summary table only needs one field (the Max(Date)) but gets more tricky if you also want to report other fields from the joined table, since you would need to include them either as an aggregated field or group by them as well.
Eg if you want your summary to also include the assessment they were given at their last appointment, GROUP BY is not the way to go.
A more versatile structure may be something like
SELECT Patient.ID, Patient.Name, Appointment.Date, Appointment.Assessment
FROM Patient INNER JOIN Appointment ON Patient.ID=Appointment.ID
WHERE Appointment.Date = (SELECT Max(Appointment.Date) FROM Appointment WHERE Appointment.ID = Patient.ID)
;
As an aside, you may want to think whether you should use a field named 'ID' to refer to the ID of another table (in this case, the Apppintment.ID field refers to the Patient.ID). You may make your db more readable if you leave the 'ID' field as an identifier specific to that table and refer to that field in other tables as OtherTableID or similar, ie PatientID in this case. Or go all the way and include the name of the actual table in its own ID field.
Edited after comment:
Not quite sure why it would crash. I just ran an equivalent query on 2 tables I have which are about 10,000 records each and it was pretty instanteneous. Are your ID fields (i) unique numbers and (ii) indexed?
Another structure which should do the same thing (adapted for your field names and assuming that there is an ID field in Appointments which is unique) would be something like:
SELECT PatientInfo.UID, PatientInfo.Name, Appointments.StartDateTime, Appointments.Assessment
FROM PatientInfo INNER JOIN Appointments ON PatientInfo_UID = Appointments.PatientFID
WHERE Appointments.ID = (SELECT TOP 1 ID FROM Appointments WHERE Appointments.PatientFID = PatientInfo_UID ORDER BY StartDateTime DESC)
;
But that is starting to look a bit contrived. On my data they both produce the same result (as they should!) and are both almost instantaneous.
Always difficult to troubleshoot Access when it crashes - I guess you see no error codes or similar? Is this against a native .accdb database or another server?

How to create a table from different query results SQL

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

Using a table to lookup multiple IDs on one row

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