I'm struggling with how to structure an SQL statement involving multiple tables and was wondering if anyone could help point me in the right direction.
I assume multiple JOINS will be needed, but I'm not entirely sure of how exactly they work.
Basically the system I need to create will allow teaching staff to view an HTML table of the assignment submissions with relevant details. Below is the ER diagram representing the database.
Each row in the HTML table will relate to a particular assignment submission, and will include the following information - Forename of the student, Surname of the student, Module the assignment relates to, Assignment Name, a link to the feedback PDF document, the time the feedback was collected and the number of times the feedback was commented on.
So basically I need an SQL statement that would return the information mentioned above.
I probably haven't explained this too well, but hopefully the diagram can help fill in the parts I have missed.
SELECT
Forename,
Surname,
ModuleName,
sa.StudentID as StudentID,
sa.AssignmentID as AssignmentID,
FeedbackURL,
FirstViewed,
LastViewed,
ViewCount,
a.Name AS AssignmentName,
FROM assignment AS a
INNER JOIN student_assignment AS sa ON a.AssignmentID=sa.AssignmentID
INNER JOIN student AS s ON sa.StudentID=s.StudentID
INNER JOIN course AS c ON s.CourseID=c.CourseID
INNER JOIN course_module AS cm ON c.CourseID=cm.CourseID
INNER JOIN module AS m ON cm.CRN=m.CRN
Related
We have to select authors that havent written a book but there are 3 different tables which makes me confused about how to write the join expression.
We have tables:
authors: author_id
authorships: author_id, book_id
books: book_id.
Obviously I selected the names from authors and tried inner join but it wont work for me. Help would be appreciated!
Since this sounds like a school assignment I won't give the full answer.
Try using an outer join between authors and authorship. Make sure you retrieve the book I'd from the authorship.
Try to work out what an author who has not published looks like the. You can use this to formulate the query for the answer you are looking for with an appropriate where clause.
This is a good spot to use the LEFT JOIN antipattern:
SELECT a.*
FROM authors a
LEFT JOIN authorships s ON s.author_id = a.author_id
WHERE s.author_id IS NULL
Rationale: when the LEFT JOIN comes up empty, it means that the author has no corresponding record in the authorships table. The WHERE clause filters out on unmatched authors records only (ie authors that have no books). This is called an antipattern because the purpose of a JOIN is usually to match records, whereas here we use it to detect unmatched records.
Its really easy, just check which column seems to be having common value between all this three tables if something is common atleast within two tables then put inner join on those two and an outer join on the uncommon data table.
Remember your Aliases will always matter when you join between different tables, also the ON and WHERE should be properly mentioned.
After looking at example after example of both inner joins and subqueries as fields, I'm apparently not getting some aspect, and I would appreciate help please. I am trying to write one query that must, alas, run in MS Access 2007 to talk to an Oracle database. I have to get values from several different places for various bits of data. One of those bits of data is GROUP_CODE (e.g., faculty, staff, student, alum, etc.). Getting that is non-trivial. I am trying to use two inner joins to get the specific value. The value of borrower category must be the value for my main row in the outer query. Here is what this looks like:
Patron table Patron_Barcode table Patron_Group table
Patron_id Barcode Patron_Group_iD
Barcode Patron_Group_id PATRON_Group_Code
I want to get the PATRON_GROUP.PATRON_GROUP_CODE. This is only one of 35 fields I need to get in my query. (Yes, that's terrible, but wearing my librarian hat, i can't write the Java program I'd like to write to do this in a snap.)
So as a test, I wrote this query:
select PATRON.PATRON_ID As thePatron,
(SELECT PATRON_GROUP.PATRON_GROUP_CODE As borrowwerCategory
FROM (PATRON_GROUP
INNER JOIN PATRON_BARCODE ON PATRON_GROUP.PATRON_GROUP_ID = PATRON_BARCODE.PATRON_GROUP_ID
) INNER JOIN PATRON ON PATRON_BARCODE.PATRON_ID = thePatron.PATRON_ID
));
I don't know what I'm doing wrong, but this doesn't work. I've written a fair amount of SQL in my time, but never anything quite like this. What am I doing wrong?
PATRON.BARCODE is the foreign key for the BARCODE table.
PATRON_BARCODE.PATRON_GROUP_ID is the foreign key for the PATRON_GROUP table. PATRON_GROUP_CODE in PATRON_GROUP is he column value that I need.
PATRON.BARCODE -> BARCODE.PATRON_GROUP_ID -> PATRON_GROUP.PATRON_GROUP_CODR>
The main table, PATRON, will have lots of other things, like inner and outer join to PATRON_ADDRESS, etc., and I can't just do an inner join directly to what I want in my main query. This has to happen in a subquery as a field. Thanks.
Ken
I have 3 tables
Students{sId, name}
Class{cId, cName} and
Enrollment{sId, cId, semester}
A student can enroll in multiple classes. I need to produce a query that displays all the pairs of students (their names and ids )who have completed one or more of the same classes in the same semester. I'm not really sure what I am to SELECT to display because I need to display in a format that shows the pairs
I started by doing the joins and came up with this
FROM Students s
INNER JOIN Enrollment e
ON s.sId = e.sId
INNER JOIN Class c
ON c.cId = e.sId
Then I am not sure what to do.. If someone could point me in the right direction it would be much appreciated. I am very new to SQL and am even struggling to find some keywords to google.
Thanks for any help
Say you have these tables:
PHARMACY(**___id_pharmacy___**, name, addr, tel)
PHARMACIST(**___Insurance_number___**, name, surname, qualification, **id_pharmacy**)
SELLS(**___id_pharmacy___**, **___name___**, price)
DRUG(**___Name___**, chem_formula, **id_druggistshop**)
DRUGGISTSHOP(**___id_druggistshop___**, name, address)
I think this will be more specific.
So, I'm trying to construct an SQL statement, in which I will fetch the data from id_pharmacy and name FROM PHARMACY, the insurance_number, name, and surname columns from PHARMACIST, for all the pharmacies that sell the drug called Kronol.
And that's basically it. I know I'm missing the relationships in the code I wrote previously.
Note: Those column names which have underscores left and right to them are underlined(Primary keys).
The query you've written won't work in any DBMS that I know of.
You'll most likely want to use some combination of JOINs.
Since the exact schema isn't provided, consider this pseudo code, but hopefully it will get you on the right track.
SELECT PH.Ph_Number, PH.Name, PHCL.Ins_Number, PHCL.Name, PHCL.Surname
FROM PH
INNER JOIN PHCL ON PHCL.PH_Number = PH.Ph_Number
INNER JOIN MLIST ON MLIST.PH_Number = PH.PH_Number
WHERE MLIST.Name = "Andy"
I've obviously assumed some relationships between tables that may or may not exist, but hopefully this will be pretty close. The UNION operator won't work because you're selecting different columns and a different number of columns from the various tables. This is the wrong approach all together for what you're trying to do. It's also worth mentioning that a LEFT JOIN may or may not be a better option for you, depending on the exact requirements you're trying to meet.
Ok, try this query:
SELECT A.id_pharmacy, A.name AS PharmacyName, B.Insurance_number,
B.name AS PharmacistName, B.surname AS PharmacistSurname
FROM PHARMACY A
LEFT JOIN PHARMACIST B
ON A.id_pharmacy = B.id_pharmacy
WHERE A.id_pharmacy IN (SELECT id_pharmacy FROM SELLS WHERE name = 'Kronol')
I am beginner in SQL, could you please help me write a query to:
find the name of lecturer who is also a participant in one course
find 4 courses with the most participants
The tables are:
- Lecturer [LecturerID (PK), name]
- Course [CourseId (PK), LecturerID, name]
- Participant [ParticipantID (PK), CourseID(PK)]
Thanks!
If you're trying to learn how joins work, it would be more beneficial for us to help you create the SQL yourself. The basic format for a join is this:
SELECT *
FROM table1
JOIN table2 ON table1.joinID = table2.joinID
I would approach this in 3 steps:
Write a basic SELECT statement that will return the joined table data
Modify the SQL to only show "the name of lecturer who is also a participant in one course"
Starting again with the basic SELECT statement from step 1, modify the SQL to only show "4 courses with the most participants"
These will end up being 2 different queries.
If you want to get a start on it, and get stuck, we can help you along, but it would not help you learn it if we just gave you the SQL. Try writing a little of it, and post what you have when you are stuck.
select L.name from lecture L join participant P on L.id=P.id
select C.cid from course C join participant P
ORDER BY P.cid DESC
LIMIT 4;
Hopefully it helps you