I'm trying to get the the students number & name with the course code & name for students who have a grade below 40. This is what i have
SELECT S.name, S.no, C.code, C.name, T.grade
FROM student S INNER JOIN course C INNER JOIN take T
WHERE grade <40;
It is outputting the grades under 40 but it is returning 128 rows showing everyone's name and number grade repeating them.
Sorry if this is wrong but im a beginner.
You need the conditions that relate the tables to each other:
SELECT S.name, S.no, C.code, C.name, T.grade
FROM student AS s
JOIN take AS t ON t.student_no = s.no
JOIN course AS c ON t.course_code = c.code
Replace student_no and course_code with the actual foreign key columns in the take table.
Simple syntax refer it
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
Related
I have 4 tables, three are many to many relationship:
Student(StudID,GroupId,Name,....)
Lesson(LessID,LessonName,Mark)
StudentLesson(StudID,LessID)
and the relationship between student and Group is One to Many
Student(StudID,Name,....)
Group(GroupId,GroupNumber)
What I want is how select Name, LessonName, Mark, GroupNumber
select S.Name, L.LessonName, L.Mark, G.GroupNumber from Student s
join StudentLesson SL on SL.StudId = S.StudId
join Lesson L on SL.LessID = L.LessID
Join Group G on G.GroupId = S.GroupId
I think the error in this line Join Group G on G.GroupId=S.GroupId, because when I omit it, it works between many to many but between one to many it didn't work.
group is a reserved word, so it needs to be quoted. In MySQL, you can use backticks:
select S.Name, L.LessonName, L.Mark, G.GroupNumber
from Student S
join StudentLesson SL on SL.StudId = S.StudId
join Lesson L on SL.LessID = L.LessID
Join `Group` G on G.GroupId = S.GroupId
Based on Comments: the query is fine; you lack data that matches the results you're after.
There are no students with a groupID
There are no students with a groupID matching GROUPID in the group table.
To prove this out you could simply make the last join a LEFT Join provided you have no where clause with limits on Group.
FROM:
select S.Name,L.LessonName,L.Mark,G.GroupNumber from Student s
join StudentLesson SL on SL.StudId=S.StudId
join Lesson L on SL.LessID =L.LessID
Join Group G on G.GroupId=S.GroupId
TO:
SELECT S.Name, L.LessonName, L.Mark, G.GroupNumber
FROM Student s
INNER JOIN StudentLesson SL on SL.StudId=S.StudId
INNER JOIN Lesson L on SL.LessID =L.LessID
LEFT JOIN Group G on G.GroupId=S.GroupId
This will show you all students w/ lessons and groupNumber if the groupID's match; but i'm betting they will all be NULL.
So are you after all students regardless if they have lessons or groups if so your inner joins should be left. If you're only after students that have lessons and belong to groups then they all need to be inner joins. Just depends on what you're after!
Left join will say include all records from the prior data joins, and only those that match from this join (to group in the example)
I'm getting the error " Not a Unique table/alias 'Customer' " when I run my join statement. I want to display all the information that I have. I've researched the JOIN statements and I can't find what's wrong.
SELECT Customer.CustomerID, Customer.FirstName, Customer.LastName, Customer.StreetAddress,Customer.City,Customer.State,Customer.Zipcode, Customer.HomePhone,Customer.MobilePhone,Customer.OtherPhone, Pizza.PizzaID,
Pizza.PizzaName, Pizza.Description, Pizza.UnitPrice, OrderInformation.OrderID, OrderInformation.OrderDate, OrderItem.Quantity
FROM Customer
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;
SELECT
...
FROM Customer
...
JOIN Customer
You're selecting FROM Customer and then doing JOIN Customer, meaning you now have two instances of Customer. When you reference something like Customer.CustomerID, your query doesn't know which iteration of the Customer table you're referring to.
If you actually needed two references to the same table, you could give one an alias. However, being that you reference an OrderItem table, but never JOIN or select FROM it, I have a hunch that one of those Customer tables should be OrderItem. Perhaps like this...
SELECT ...
FROM OrderItem
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;
I would strongly suggest that you use table aliases. It would appear that the first reference to Customer should really be OrderItem:
SELECT c.CustomerID, c.FirstName, c.LastName,
c.StreetAddress, c.City, c.State, c.Zipcode,
c.HomePhone, c.MobilePhone, c.OtherPhone,
p.PizzaID, p.PizzaName, p.Description, p.UnitPrice,
oinf.OrderID, oinf.OrderDate, oi.Quantity
FROM OrderItem oi JOIN
OrderInformation oinf
ON oinf.OrderID = oi.OrderID JOIN
Pizza p
ON p.PizzaID = oi.PizzaID JOIN
Customer c
ON c.CustomerID = oi.CustomerID;
What happens actually when we use cascaded join statements
select student.name, count(teacher.id)
from student
left join course on student.course_id = course.id
left join teacher on student.teacher_id = teacher.id
group by student.name;
It seems when I used only the first left join alone it returned 30 rows while using the second left join alone returned 20 rows. But using together returns 600 rows. What is actually happening ? Does the result from the first left join is used in the second ? I don't understand the semantics. Help me understand it.
Since you don't have any join conditions between teacher and course, you're getting a full cross-product between each of the other two joins. Since one join returns 20 rows and the other returns 30 rows, the 3-way join returns 20x30 = 600 rows. Its equivalent to:
SELECT t1.name, count(t2.id)
FROM (SELECT student.name
FROM student
LEFT JOIN course ON student.id = course.id) AS t1
CROSS JOIN
(SELECT teacher.id
FROM student
LEFT JOIN teacher ON student.id = teacher.id) AS t2
GROUP BY t1.name
Notice that the CROSS JOIN of the two subqueries has no ON condition.
The correct way to structure this database is as follows:
student table: id (PK), name
course table: id (PK), name, fee, credits
student_course table: id (PK), student_id (FK), course_id (FK), unique key on (student_id, course_id)
Then to get the name of each student and the average course fee, you would do:
SELECT s.name, AVG(c.fee) AS avg_fee
FROM student AS s
LEFT JOIN student_course AS sc ON s.id = sc.student_id
LEFT JOIN course AS c ON sc.course_id = c.id
All Mysql joins are graphically explained here. Take a look and choose correct joins for both joined tables.
I'm trying understand how I can pull information from multiple tables at once in one query if that is possible.
I have 3 tables and I'm wondering if there is a way I can query all the product names for customers that live in california?
Table:
products
Fields:
productOid
productName
companyOid
Table:
customerData
Fields:
customerOid
firstName
lastName
state
Table:
orders
Fields:
orderNumber
customerOid
productOid
Would this fall under something like an INNER JOIN?
Also, I'm learning mySQL.
You will need to use inner joins for this.
SELECT DISTINCT p.productName
FROM orders o
INNER JOIN customerData c ON o.customerOid = c.customerOid
INNER JOIN products p ON o.productOid = p.productOid
WHERE c.state = 'CA';
I am using DISTINCT here because it's possible a customer would order the same product more than once (or multiple customers would order the same products) and I'm assuming you don't want duplicates.
I'm also making the assumption that your state is represented as a two character column.
Read more about joins
You could use one more join, but I would write it this way:
SELECT DISTINCT p.productName
FROM
orders o INNER JOIN products p
ON o.productOid = p.productOid
WHERE
o.customerOid IN (SELECT customerOid
FROM customerData
WHERE state = 'California')
It might be a little slover than a join, but it's more readable.
This shows products that CA customers have ordered:
SELECT p.productName
FROM orders o
INNER JOIN products p ON o.productOid = p.productOid
INNER JOIN customerData c ON o.customerOid = c.customerOid
WHERE c.state = 'CA'
I have a pretty simple MySQL question. I have two tables, Customer and Orders. Customer table has fields (id, name) and Order has fields (id, customerID, and item).
I can find which customer bought product A and customers that bought product B with the following query in MySQL.
SELECT DISTINCT c.`id`, c.name, o.`item`, o.qty FROM `customer` as c
INNER JOIN order AS o ON (c.`Id` = o.`customerID`)
where o.`item` ="Product A"
Union
SELECT DISTINCT c.`id`, c.name, o.`item`, o.qty FROM `customer` as c
INNER JOIN order AS o ON (c.`Id` = o.`customerID`)
where o.`item` ="Product B"
How can find the difference and similarity in these two result sets?
1) I.e. Customers that bought only product A but did not by product B
2) I.e. Customers that bought both product A and B
Thank you for your assistance.
D
You can try using the LEFT OUTER JOIN to get the result.