need output with sub query from the tables in mysql - mysql

FROM THE GIVEN TABLES: Teacher And Student
teacher_ID name city fee
------------------- ------------------------
1 Jit New York 15000
2 Nilon Paris 13000
5 Pol London 11000
6 Maj Paris 14000
7 Paul Rome 13000
3 Liza Saudi 12000
----------------------------------------------------------
student_id name city rank teacher_ID
2 Nick New York 1 1
7 Brady New York 2 1
5 Gunman California 2 2
8 Juli London 3 2
4 Faral Paris 3 6
9 Goku Berlin 1 3
3 John Moscow 2 7
1 Badstar London 5
From the Given Tables I need to make a list in ascending order for the students who holds a rank less than 3 and taught either by a teacher or by own.
It's expected output is this
name city rank Teacher city
Nick New York 1 Jit New York
John Moscow 2 Paul Rome
Gunman California 2 Nilon Paris
Brady New York 2 Jit New York
Goku Berlin 1 Liza Saudi
I tried writing sql code
SELECT name,city,rank FROM Student WHERE rank < 3 ORDER BY student_ID ASC;
That code gives me first 3 columns of the expected output however I've no idea how to get the last two column of the expected output, any suggestions are welcome

The query...
SELECT Student.name,
Student.city,
rank,
Teacher.name as "Teacher",
Teacher.city
FROM Student
LEFT JOIN Teacher on Teacher.teacher_ID = Student.teacher_ID
WHERE rank < 3
ORDER BY student_ID ASC;
There are a few things that you should consider in the statement
When selecting the fields you want to display you need to make sure table column references are unique. As both tables have the field name and city, you should specifically reference them using the format of [table].[column name]
There are multiple ways that you can join tables. In this case, you want to use a left join because all data exists in the tables on the left side of the join, and might not exist in the table on the right side of the join.
If the student has a result of less than 3 and no teacher, the teacher name and city will appear as null. You might want to consider adding an IFNULL or similar to make these fields clear.

Related

How to count rows in mysql subquery

I have two tables course and registered_course
course table
crid
crname
crlevel
1
math
senior
2
english
senior
3
physics
senior
Registered course table
id
crid
student_id
1
2
25
2
2
26
3
3
23
4
3
24
5
3
27
so i want to achieved this result the first table join with second table and a count of students that registered a subject just like below thanks
crname
crlevel
number_of_student
math
senior
2
physic
senior
3
SELECT table1.crname, table1.crlevel, count(table2.studentid)
FROM table1
INNER JOIN table2 ON table1.crid = table2.crid
GROUP BY table1.crname, table1.crlevel

counting no of exams a student has appeared in exam combining two tables

I have two below tables student and examination:
Student table:
Id Name
1 Samantha
2 Jane
3 Bob
4 Scarlet
5 David
Examination:
student_id subject
1 Biology
1 physics
3 history
4 geography
4 geography
Now I need to find which student has appeared in which examination how many number of times, the exact ans I expect is:
student.id examination.subject no_of_times
1 biology 1
1 physics 1
3 history 1
4 geography 2
I have tried below query, but got wrong ans:
select Student.id, examination.subject, count(*) from Student join examination Student.id = examination.student_id;
Please help me to write the correct query to get correct expected output!!
Thank You!
For your exact query, we don't even need to join, we can just use the second Examination table exclusively:
SELECT student_id, subject, COUNT(*) AS no_of_times
FROM Examination
GROUP BY student_id, subject;

I want to join two tables, removing duplicate values

I have to say I'm an amateur on MySQL, plus english is not my mother's language.
Here is my first table "Teacher":
ID Name Procedence
1 John Italy
2 Mike Russia
3 Lauren Spain
4 Arnold Spain
And here my second table "Course":
ID1 CourseName
1 Sailing
1 Football
2 Basketball
2 Hockey
I would like to know which country has more teachers giving courses, but I don't want that it count the same teacher twice. So far I made this:
SELECT Procedence, Count(Procedence) as Procedence_Count
from Teacher INNER JOIN Course
ON Course.ID1 = Teacher.ID
GROUP by Procedence
having Procedence_Count > 1;
When I run this, I obtain:
Procedence Procedence_Count
Italy 2
Russia 2
But the code counts John and Mike as two persons, I would like to remove that duplicity, so I would like to obtain:
Procedence Procedence_Count
Italy 1
Russia 1
Thanks a lot.
Use count(distinct):
select Procedence, Count(distinct t.id) as teacher_count
from Teacher t join
Course c
on c.ID1 = t.ID
group by Procedence
having teacher_count > 1;
One additional note: "procedence" is not at all a common English word. One word is "provenance". I think more commonly "origin" or just "country" would be used.

Relational databases SQL: Parent/Child relationships

I am trying to learn SQL. I have three different tables. All the tables have one common column. I am showing a very small portion of sample data.
I am trying to understand how to read from a parent table after a child table has been updated.
Table_1 (booking platform info)
booking_date column2 column3 column4 cust_country_id hotel_id booking_value
22-mar-2016 .................. 1001 1 $150
01-apr-2016 .................. 1002 2 $500
09-apr-2016 .................. 1001 2 $222
17-apr-2016 .................. 1002 4 $75
19-apr-2016 .................. 1003 1 $690
03-May-2016 ................., 1001 3 $301
Table_2 (hotel information)
hotel_id hotel_name hotel_country
1 Marriott Germany
2 Novotel France
3 Oberoi India
4 Osaka Japan
Table_3 (customer information)
country_id country_name
1001 India
1002 France
1003 Japan
My question is if a new hotel Hyatt from France is added to Table_2 (hotel information), how can I find out the first date at which a French customer booked that hotel?
I am getting a bit confused on how to approach this; as it has to take values from all 3 tables.
I think in this case you should write mysql query to find the oldest booking_date for customer from France and hotel with name Hyatt
select booking_date from Table_1
join Table_2 on Table_2.hotel_id = Table_1.hotel_id
join Table_3 on Table_3.country_id = Table_1.cust_country_id
where Table_3.country_name = 'France' and Table_2.hotel_name = 'Hyatt'
order by Table_1.booking_date asc
limit 1

Trying to sum and group two sets of results from three SQL tables

I am having some major difficulties with grouping and summing results of a query in the way I want. I am having trouble explaining what I want to do in words, so I'll just show you. I have three tables: A, H, and W which look like (simplified):
Table A:
Aid name
1 adam
2 bob
Table H:
Hid Wid date atk Aid
1 1 - 10 2
2 2 - 1 1
3 2 - 5 1
4 1 - 2 2
5 1 - 22 1
6 2 - 7 2
Table W:
Wid name user pass
1 charlie - -
2 donald - -
I am trying to get the SUM of atk grouped by Aid and Wid. Basically, assume this is a fight club tally. I want to display the sum of how many times person W attacked person A (it will always be a one directional fight, ie: charlie can only attack adam, but adam can't attack charlie). (not really a fight club - being used for an online game :))
I am trying to get my result to look like:
name1 atk name2
charlie 22 adam
charlie 12 adam
donald 6 bob
donald 7 bob
My current query looks like...
SELECT w.name AS name1, h.atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
...which gives me every instance that name1 attacked name2. When I try to GROUP BY and/or SUM(h.atk) it is grouping or summing in a way I can't figure out. I'm just not understanding how to accomplish this. Any help would be greatly appreciated.
Thanks in advance!
SELECT w.name AS name1, sum(h.atk) as atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
GROUP BY w.name, a.name