First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 4
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
4 Julian
So far I've made this:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
where TotalRevenue > (select TotalRevenue
from Table2 as t2
inner join Table1 as t1 on t2.Lesson = t3.LessonName
inner join Table3 as t3 on t3.IDTeacher = t1.TeacherID
where t3.TeacherName = "Mike");
And I don't know how to keep going because when I run it, an error appears.
My expected result would be something like:
IDTeacher TotalRevenue
3 200
Thanks!
In your main query you must use a HAVING clause instead of a WHERE clause and also in the subquery fix your joins:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
having TotalRevenue > (
select sum(t2.PricePerClass)
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
where t3.TeacherName = "Mike"
);
See the demo.
Results:
| IDTeacher | TotalRevenue |
| --------- | ------------ |
| 3 | 200 |
Related
First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know how is called the teacher who makes more money with the classes:
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 1
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
My main problem is that I don't know how to deal with the repeated values from the first table when I'm doing the triple join.
So far "I've made" this:
select IDTeacher, PricePerClass
from Table1 as T1
inner join Table2 as T2 on t1.LessonName = t2.Lesson
inner join Table3 as T3 on t1.TeacherId = t3.idTeacher
...
And I don't know how to keep going. I will have to group the t1.LessonName but every time I try to do it I get syntax errors. As you can see I'm pretty lost.
EDIT: My expected result would be something like:
IDTeacher TotalRevenue
1 250
Thanks a lot.
join the tables, group by teacher to aggregate and get the top row after you sort by the total descending:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
order by TotalRevenue desc limit 1
Note that this query does not return ties, if any.
SELECT t3.IDTeacher, t3.TeacherName, sum(t2.PricePerClass) from table1 t1
inner join table3 t3 on t1.TeacherID = t3.IDTeacher
inner join table2 t2 on t1.Lessonname = t2.Lesson
group by t3.TeacherName
order by sum(t2.PricePerClass) desc limit 1;
I want to get Ronald from Left Table through matching Right Table values with 1 AND 2. I know I need to use DISTINCT to get only one row but other than that, I'm stumped.
Left Table
pid | name
1 Ronald
2 Chris
3 John
Right Table
pid | value
1 1
1 2
2 1
3 2
Joined Table
pid | name | value
1 Ronald 1
1 Ronald 2
2 Chris 1
3 John 2
Expected Output
pid | name
1 Ronald
You want to match both 1 and 2. Aggregation and having come to mind:
select t1.pid, t1.name
from table1 t1 join
table2 t2
on t1.pid = t2.pid
where t2.value in (1, 2)
group by t1.pid, t1.name
having count(*) = 2; -- should be `count(distinct)` if duplicates are possible in `table2`
Self join table2 and select value = 1 in the first and value = 2 in the second. Join the result with table 1.
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_1.value = 1 AND t2_2.value = 2
Or if you like that better:
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid AND t2_1.value = 1
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_2.value = 2
Here is my tables:
T1 record:
id referencePerson1ID referencePerson2ID referencePerson3ID
1 1 2 3
T2 referencePerson:
id name
1 Peter
2 John
3 Mary
I want to get the following result when I choose T1 id =1:
id referencePerson1 referencePerson2 referencePerson3
1 Peter John Mary
How can I do that?
Thanks
This is one basic way to do things:
SELECT T1.id, P1.name, P2.name, p3.name
FROM record T1 LEFT JOIN referencePerson P1
ON T1.referencePerson1ID=P1.id
LEFT JOIN referencePerson P2
ON T1.referencePerson2ID=P2.id
LEFT JOIN referencePerson P3
ON T1.referencePerson3ID=P3.id
Another way when number of persons is unknown can be done using a PIVOT
You can do that with three times JOIN :
SELECT t1.id, t21.name, t22.name, t23.name
FROM T1
INNER JOIN T2 t21 ON t21.id = T1.referencePerson1ID
INNER JOIN T2 t22 ON t22.id = T1.referencePerson2ID
INNER JOIN T2 t23 ON t23.id = T1.referencePerson3ID
WHERE T1.id = 1
Table1
id Name
1 John
2 Sheldon
3 Sarah
========
Table2
rid id id2 relation
1 1 2 1
2 1 3 1
How can i get all members from table 1 in one query and each member's total relations.
The result I want should be:
Jhon 2
Sheldon 0
Sarah 0
select Table1.name, count(Table2.id) from Table1 LEFT Join Table2 on Table1.id=Table2.id group by Table2.id
OR
select Table1.name,IFNULL(count(Table2.id), 0) from Table1 LEFT Join Table2 on Table1.id=Table2.id group by Table2.id
SELECT t1.name, IFNULL(COUNT(t2.id2), 0)
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id=t2.id
GROUP BY t1.id
Question:
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
Answer:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
New Question?
Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:
sum | user ID | user Name
Maybe this will be useful:
SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number
Try this query:
SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id