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.
Related
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;
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.
I have a question about selecting data from three tables
the first table is like that:Timetable
ClassID teacherID assistantID
1 1 2
0
2 3 4
` 0 is a break time, so No assistants and teachers
Class table:
classID classname
0 Break
1 MATH
2 ART
teacher table
teacherID teacherName
1 Tom
2 Lily
3 Rose
4 Nile
I want to produce a table like this
Timetables:
ClassName TeacherName AssistantName
Math Tom Liky
Break
Art Rose Nile
Join the teacher table twice with different alias names
select c.className, teach.teacherName, assist.teacherName as assistantName
from Timetable t
join class c on c.classID = t.classID
left join teacher teach on teach.teacherID = t.teacherID
left join teacher assist on assist.teacherID = t.assistantID
I have two tables: "series" and "product"
"series" holds the names of series of books, "product" has the details of individual book titles.
So something like this:
Series Table:
id series_name series_description
1 Lord of the Rings Trilogy of fantasy books recently made into film
2 A Song of Ice and Fire Epic series of novels currently showing on HBO
3 Harry Potter Famous Children's book series also loved by adults
Product Table:
id product_name series_id author etc...
1 Fellowship of the Ring 1 JRR Tolkien
2 The Two Towers 1 JRR Tolkien
3 Return of the King 1 JRR Tolkien
4 A Game of Thrones 2 George R R Martin
5 A Clash of Kings 2 George R R Martin
6 A Storm of Swords 2 George R R Martin
7 A Feast for Crows 2 George R R Martin
8 A Dance with Dragons 2 George R R Martin
9 Harry Potter and the... 3 JK Rowling
etc.
I want to SELECT * FROM series, and COUNT(*) FROM product so that the query returns a table containing the series info, with the number of products in the database corresponding to each series appended as the last column of the table. I'd also like to do this by genre, so there is an additional WHERE clause in there somewhere. It would look something like this, if selecting "Fantasy and Magic" as the genre:
id series_name series_description number_of_products
1 Lord of the Rings Trilogy of Fantasy... 3
2 A Song of Ice and Fire Epic Series of Novels... 5
3 Harry Potter Famous Children's book... 7
I think I may need a LEFT JOIN but my best attempts so far have been in vein.
This is what I have so far, but I think it's probably totally wrong.
SELECT series.id,
series.series_name,
series.publisher_id,
series.description,
series.image,
COUNT (product.*) as nRows
FROM series
LEFT OUTER JOIN product
ON series.id = product.series_id
WHERE series.genre = 'Fantasy and Magic'
GROUP BY ... (do I need a GROUP BY?)
Any help at all would be really appreciated. Thanks in advance!
Almost there. Try this.
SELECT series.id,
series.series_name,
series.publisher_id,
series.description,
series.image,
COUNT (product.id) as nRows
FROM series
LEFT OUTER JOIN product
ON series.id = product.series_id
WHERE series.genre = 'Fantasy and Magic'
GROUP BY series.id,
series.series_name,
series.publisher_id,
series.description,
series.image
Wouldn't this help;
select s.id,
s.series_name,
s.series_description,
(select count(*) from Products p where p.series_id = s.id) number_of_products
from Series s
I have three tables which i have joined ..but the manner in which i have to display them is a little difficult for me to understand.I am trying to group them and not getting the desired result.
Sports_Name
Id | Name
---------
1 Football
2 Cricket
3 Hockey
4 Tenis
Teams
Id | Sport_Id | Team_Name
----------------------------
101 1 Manchester United
103 2 Australia
104 2 India
109 1 Real Madrid
110 3 New Zeland
Player_Name
Id | Team_Id | Player_Name
------------------------------
1 101 Rooney
2 104 Tendulkar
3 103 Ponting
4 109 Ronaldo
5 101 Van Persie
6 110 Simond
I need to display this information in the following manner..
**Football**
Manchester United -Rooney
Manchester United -Van Persie
Real Madrid -Ronaldo
**Cricket**
India-Tendulkar
Australia-Ponting
**Hockey**
New Zeland -Simond
and something similar for tenis
See this picture for details
Is this what you want?
SELECT sports_name.name, teams.team_name, player_name.player_name FROM player_name
JOIN
teams ON
player_name.team_ID = teams.ID
JOIN
sports_name on
sports_name.id = teams.sport_id
order by sports_name.name, team_name
This gets all the data in order then you just need to use your presentation layer to use sports.name as group header.
This produces
name team_name player_name
Cricket Australia Ponting
Cricket india Tendulkar
Football Man u Rooney
Football Man u RVP
Football Real Madrid Ronaldo
Hockey N Z Simond
try this:
SELECT T.TEAM_NAME+' - '+ P.PLAYER_NAME as 'FOOTBALL'
FROM TEAMS T JOIN PLAYER_NAME P
ON T.ID=P.TEAM_ID
JOIN SPORTS_NAME S
ON T.SPORT_ID=S.ID
WHERE S.NAME='FOOTBALL'
Change the where clause to 'Cricket','Hockey' to get other results
The basic query would be something like:
SELECT s.Name, t.Name, p.Player_Name
FROM Player_Name p
INNER JOIN Teams t
ON t.Id = p.Team_id
INNER JOIN Sports_Name s
ON s.Id = t.Sport_Id
Then if you want to display the results like you described it, my guess would be to use a cursor. Use parameterized procedure to add a WHERE clause to the query in order to filter the results by sport's name.