Can someone help me trouble shoot my syntax i spent 1 hour on it yet i cant figure it out.
I have 3 tables:
**Student**
-ID (UUID)
email (String)
**Teacher**
-ID (UUID)
email (String)
**StudentTeacher**
-ID (UUID)
studentId (UUID)
teacherId (UUID)
I have an initiate query which returns some studentId
SELECT studentId FROM TeacherStudents WHERE teacherId IN ('123', '456') GROUP BY studentId HAVING COUNT(DISTINCT teacherId) = 2
I would like to join the results with Students table so i can return the email address
However, this is not working
SELECT email FROM TeacherStudents WHERE teacherId IN ('123', '456') GROUP BY studentId HAVING COUNT(DISTINCT teacherId) = 2 INNER JOIN Students on TeacherStudents.studentId = Students.id
It returns an error.
The correct syntax is:
SELECT s.studentId, s.email
FROM TeacherStudents ts JOIN
Students s
ON ts.studentId = s.id
WHERE ts.teacherId IN (123, 456)
GROUP BY s.studentId, s.email
HAVING COUNT(DISTINCT ts.teacherId) = 2 ;
Notes:
WHERE is a SQL clause that follows the FROM clause.
JOINs are an operator in the FROM clause.
TeacherId is probably a number, so do not use single quotes.
Table aliases make the query easier to write and to read.
You are learning SQL, so be sure the unaggregated columns in SELECT match the GROUP BY. You can drop s.studentId from the SELECT if you really want to.
Qualify all column names so you know what table they come from.
The generalised case, to get all columns from the Student table...
SELECT
Students.*
FROM
(
SELECT studentId
FROM TeacherStudents
WHERE teacherId IN ('123', '456')
GROUP BY studentId
HAVING COUNT(DISTINCT teacherId) = 2
)
StudentsOfInterest
INNER JOIN
Students
ON StudentsOfInterest.studentId = Students.id
Related
Problem: MySQL - Get the sum of IBB for each PersonID in Batting and update that IBB for each PersonID in Batting_Career
Could someone help me figure out the JOIN?
What I have that is not working:
UPDATE Batting_Career
SET Batting_Career.IBB = Batting.IBB WHERE PersonID IN
( SELECT DISTINCT
PersonID,
SUM(IBB) AS IBB
FROM
Batting
GROUP BY PersonID )
AND Batting_Career.PersonID = Batting.PersonID;
The Batting select statement works (...) - it gets me the sum of IBB in a column IBB for all playerIDs but when I add to the update using it does not work.
The error that I am getting is
Operand should contain 1 column(s)
I believe the rules are You can only select one column from such a query. Which mean I may have to Join the tables. Could anyone help me figure this out?
Aggregate in Batting and join to Batting_Career:
UPDATE Batting_Career bc
JOIN (
SELECT PersonID, SUM(IBB) as IBB
FROM Batting
GROUP BY PersonID
) b ON b.PersonID = bc.PersonID
SET bc.IBB = b.IBB;
I have written a query that will do your work :)
UPDATE Batting_Career t2
SET t2.IBB = t1.IBB where PersonID IN (
SELECT
PersonID,
SUM(IBB) AS IBB
FROM
Batting
WHERE
PersonID IN(
SELECT DISTINCT
PersonID
FROM
Batting
)
GROUP BY
PersonID
) t1
WHERE
t2.PersonID = t1.PersonID;
I have the two tables grades and feedback and the following MySQL query:
SELECT id, mingrade, maxgrade, quizid
FROM feedback
WHERE quizid=6
ORDER BY id DESC LIMIT 6
How do I need to modify the query to add userid at the first table?
What are the fields of the grades and feedback tables? Use a JOIN that links the two based on a common attribute.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
WHERE condition;
So if your two tables are grades and feedback, and both have the field userid you could say:
SELECT id, userid, mingrade, maxgrade, quizid
FROM feedback
INNER JOIN grades ON feedback.userid = grades.userid
WHERE quizid = 6
ORDER BY id DESC LIMIT 6;
I think that should work.
I am stuck in MySQL query. I have two tables one name is STUDENT and field names is (ID, FullName) where ID is PRIMARY KEY and another table is Student_FEE field is (ID, Student_ID, Date) here Student_ID is foreign key.
When i execute this query i get all the record of but i just want to select only that last row of each Student_ID not all here is image and query.
Remember I Just Need ID 5 and ID 9
SELECT Student_FEE.*
FROM Student_FEE
LEFT JOIN Student
ON Student_FEE.ID=Student.ID
Thanks you so much i have found this , thanks stackoverflow. Here is answer
SELECT Student_FEE.* FROM Student_FEE LEFT JOIN Student
ON Student_FEE.student_ID = Student.ID
WHERE Student_FEE.ID IN (SELECT MAX(ID)
FROM Student_FEE GROUP BY Student_ID)
You should use a group by adn an in clause
SELECT Student_FEE.*
FROM Student_FEE
LEFT JOIN Student
ON Student_FEE.student_ID=Student.ID
where Student_FEE in ( select max(ID)
from Student_FEE
group by student_ID)
I have two tables Customers and Customer_orders. I'm trying to SELECT customer's order by using Customer_ID from Customer table.
SELECT * FROM Customer_orders WHERE Customer_ID = SELECT ID FROM Customers WHERE Customer_name = 'John Doe'
This code does not work. How do I do this?
You need to JOIN those 2 tables and then query for what you need. Like this:
SELECT co.*
FROM Customer_orders co
INNER JOIN Customers c ON co.Customer_ID = c.ID
WHERE c.Customer_name = 'John Doe';
The other answer is a little complex - it might attempt to show you a solution that can solve a more difficult problem.
First, it queries the ID of the John Doe from Customer table.
Second, it queries the all columns from Customer_orders table where customer_id equals ID.
My solution is easy:
SELECT * FROM Customer_orders WHERE Customer_ID in (SELECT ID FROM Customers WHERE Customer_name = 'John Doe')
or
SELECT * FROM Customer_orders WHERE Customer_ID = (SELECT ID FROM Customers WHERE Customer_name = 'John Doe')
Generally, ID is a primary key of Customer table and the customer_id should be a key index of the Customer_orders.
Two times query of single table using key index is faster than the query using join operation especially when the tables have too many rows.
And thanks Tim to correct my grammar. I so appreciate it. My English is not good .
you can use join method (inner / left) or even subquery (but not recommend to use this on this problem)
here example
inner join
SELECT cust_order.* FROM Customer_orders cust_order, Customers cust WHERE cust_order.Customer_ID = cust.ID AND cust.Customer_name = 'John Doe'
left join
SELECT cust_order.* FROM Customer_orders cust_order LEFT JOIN Customers cust ON cust_order.Customer_ID = cust.ID WHERE cust.Customer_name = 'John Doe'
sub query
SELECT * FROM Customer_orders WHERE Customer_ID = (SELECT ID FROM Customers WHERE Customer_name = 'John Doe')
make sure you only have 1 ID on table Customers if you want to use sub query, or if it have more than 1 ID, you can use "IN" instead of "="
So I have a table called members and another table called group.The leader of the group is also a member
To retrieve members,who are not leaders I did the following
code:
SELECT first_name, last_name, rank
FROM members
EXCEPT ALL
SELECT first_name, last_name, rank
FROM members INNER JOIN groups ON mid=leader; --edited gid as mid
Doing this in MySQL gives me a syntax error.What should I use for EXCEPT ALL in MySQL?
SELECT first_name, last_name, rank
FROM members
LEFT OUTER JOIN groups ON gid=leader
WHERE leader is null
Not sure if leader or gid is in the groups table. The column that is in the groups table must have a null check in the where clause.
subquery may do, something like:
SELECT first_name, last_name, rank
FROM members
WHERE id NOT IN (
SELECT leader
FROM groups
WHERE leader = members.id
)
We need to know your table structure to help you further
You can try with this scenario.
SELECT
r.col1,r.col2
FROM tabel1 r
WHERE ROW ( r.col1,r.col2 ) NOT IN(
SELECT
col1,col2
FROM tabel2
);