The question is stated as there is a database on professors, departments, courses and schedules. Need to write a query that returns the names of all the professors and their respective courses. Each row must contain the name of the professor followed by the name of the course that the professor teaches. There must be no duplicates in terms of the rows.
The schema I have, in terms of the table name and the fields:
PROFESSOR: ID, NAME, DEPARTMENT_ID, SALARY
DEPARTMENT: ID, NAME
COURSE: ID, NAME, DEPARTMENT_ID, CREDITS
SCHEDULE: PROFESSOR_ID, COURSE_ID, SEMESTER, YEAR
The code I have right now:
SELECT DISTINCT p.Name AND c.NAME
FROM Prodessor p, Course c, Schedule S
WHERE
p.DEPARTMENT_ID = c.DEPARTMENT_ID
AND
p.ID = s.PROFESSOR_ID
AND
c.ID = c.COURSE_ID
The result I get is a list of all professors, but there aren't multiple courses, just a single one. What's going wrong here? It also mentioned that PROFESSOR.ID is foreign to COURSE.PROFESSOR_ID, so p.ID = s.PROFESSOR_ID is valid
I think the below code will solve your requirement
SELECT distinct p.name,c.name
from Professor p
inner join Schedule s on p.id=s.professor_id
inner join course c on c.id=s.course_id;
Alternatively if we want professors with the names of the courses they teach (or have taught) outside of their department.
select distinct p.name, c.name
FROM professor p
JOIN schedule s on s.professor_id = p.id
JOIN course c on s.course_id = c.id
WHERE c.department_id <> p.department_id;
seems you need left join instead of inner join. i think you don't need distinct() with this kind of information
select distinct p.Name, c.Name, s.*
from schedules s
left join Professor p on p.id = s.professor_id
inner join Department d on d.department_id = p.department_id
left join Course c on s.course_id= c.id
but if you only want professor and course info, assuming there's some professors that doesn't have schedules yet.
so no course info yet.
select distinct p.Name, c.Name
from Professor s
left join Course c on c.professor_id = p.id
I would use a left join here, and only involve the professor and course tables.
SELECT
p.NAME,
COALESCE(c.COURSE_ID, 'no courses available') AS COURSE_ID
FROM PROFESSOR p
LEFT JOIN COURSE c
ON p.ID = c.PROFESSOR_ID
ORDER BY
p.NAME;
The left join option lets us display a message should a given professor have no courses associated with him.
Related
I have a one to many relationship between departments and users.
Database Design:
users has each own department and department has many users. I wanted to select all the department_name but I have a duplicate values of department_name I want to merge it into one so I planned to use UNION how can I implement this using Inner Join? This is my code so far.
SQL
SELECT D.department_name FROM users U
INNER JOIN departments D ON D.id = U.department_id;
Results:
If you want only the distinct department names, you need to group the users into a comma separated values.
select d.department_name, group_concat(u.id) user_id_list
from departments d inner join users u on d.department_id = u.department_id
group by d.department_name
I have a table with persons (personid,name), another table with camps (campid,title) and a third table with camp participations (id,personid,campid).
Now, for a given camp, I need a list of all other camp participations by all persons participating in the current camp.
But I have no idea how to join these tables. I have looked at a lot of other examples, but I can't really seem to get any inspiration from those...
This should work:
SELECT *
FROM PERSONS AS D INNER JOIN CAMP_PARTICIPATIONS AS E ON D.PERSONID = E.PERSONID
INNER JOIN CAMPS AS F ON F.CAMPID = E.CAMPID
WHERE F.CAMPID <> [your_camp] AND A.PERSONID IN (
SELECT A.PERSONID
FROM PERSONS AS A INNER JOIN CAMP_PARTICIPATIONS AS B ON A.PERSONID = B.PERSONID
INNER JOIN CAMPS AS C ON C.CAMPID = B.CAMPID
WHERE C.CAMPID = [your_camp] )
select persons.*,participations.*,camps.* from persons left join participations
on participations.personid=persons.personid
left join camps on camps.campid=participations.campid
where camp.campid=1;
now u can change the campid in where clause and place column name which u want in select clause
There are three tables
Students, Courses and Registration
Students has id, name columns
Courses has also course.id, course.name
and there is third table joining the Students and Courses table
Registration : stu_id, course_id
One Student can take one or many courses.
I would like to find the name of Students registered in only one course.
Try with INNER JOIN
SELECT S.id, S.name
FROM students S
INNER JOIN registration R ON S.id = R.stu_id
GROUP BY S.id, S.name
HAVING COUNT(*) = 1
Like below:
SELECT s.id, s.name
FROM students s
LEFT JOIN registration r ON s.id = r.stu_id
GROUP BY s.id, s.name
HAVING COUNT(r.course_id) = 1
select s.*
from (
select r.stu_id stu_id
from Registration r
group by r.stu_id
having count(*) == 1) ra
join Students s on s.id = ra.stu_id;
This one is more efficient.
It's unlikely that your schema has null fields. Therefore, it doesn't matter which kind of join, inner or left, you use.
I have a MySql query problem. The tables I'm using are very large, so I listed a simple example that I can then use in my more complex table structure.
Lets say the tables are the following.
House(id, name)
Person(id, name, house_id)
Car(id, name, person_id, type)
CarEngine(id, name, hp)
Each Person belongs to a House. Each Car belongs to a Person. Each Car has a CarEngine with the same primary key (id).
How would I select only the name from each table while selection all the information from CarEngine from these tables efficiently where Car type is truck?
select p.name as PersonNamee, h.name as HouseName, c.name as CarName,
e.id as CarID, e.name as EngineName, e.hp
from Person p
inner join House h on p.house_id = h.id
inner join Car c on p.id = c.person_id
inner join CarEngine e on c.id = e.id
where c.type = 'truck'
I have the following database schema:
table courses:
id
tutor_id
title
table course_categories:
id
category_id
course_id
table categories:
id
name
table tutors:
id
name
table subscribers:
id
course_id
user_id
I need to make 1 sql to get a course with all it's categories, and the tutor for that course and the number of subscribers for that course. Can this be done in 1 query? Should this be done using stored procedures?
With this query you get what you want:
select co.title as course,
ca.name as category,
t.name as tutor,
count(s.*) as total_subscribers
from courses co
inner join course_categories cc on c.id = cc.course_id
inner join categories ca on cc.category_id = ca.id
inner join tutors t on co.tutor_id = t.tutor_id
left join subscribers s on co.id = s.course_id
where co.title = 'Cat1'
group by co.title, ca.name, t.name
I used left join on subscribers because there might be no one for a given course. I'm assuming that all the other tables have data on it for every course, categorie and tutor. If not, you can user left join as well but then you'll have data with null.
It can be done. You need to look up select and the use of join. See select and join to help complete the assignment
select cou.title, cat.name, tu.name, count(sub.user_id) from courses cou, course_categories cca, categories cat, tutors tu, subscribers sub where cou.id = cca.id and cat.id = tu.id and tu.id = sub.id group by cou.title, tu.name;