I am trying to pull students and the courses they are registered for from a mysql database. I want to replace the course id with the course name in the output.
I have these tables and columns:
table: users_to_courses
columns: users_LOGIN and courses_ID
table: users
columns: login, name and surname
table: courses
columns: id and name
The tables tie together on the following:
users_to_courses.users_LOGIN = users.login
users_to_courses.courses_ID = courses.id
I am trying to get output of surname, name, courses (by name and not id) so it would look like Smith, John Chemistry, Physics, Composition
Here is what I am trying unsuccessfully:
SELECT
users.surname,
users.name,
users.login,
users_to_courses.users_LOGIN,
(select group_concat (courses.name) from courses
where users_to_courses.users_LOGIN = users.login and users_to_courses.courses_ID =
courses.id
GROUP BY users.surname) as courses
from courses join users_to_courses
on courses.id = users_to_courses.courses_ID
SELECT users.login, users.surname, users.name, courses.name
FROM users_to_courses
INNER JOIN users ON users_to_courses.users_LOGIN = users.login
INNER JOIN courses ON users_to_courses.courses_ID = courses.id
GROUP BY users_to_courses.users_LOGIN
It is not entirely clear how the users_to_courses table is organized, if it is one class per row entry, then you will end up with one row per class, but all the rows corresponding to the same user ID will be consecutive in the table...
If the users_to_courses.courses_ID entry is a list of multiple course ids this might not work...
Try this
Select
u.surname,
u.name,
u.login,
uc.users_LOGIN,
c.Name,
--added this line
group_concat (c.name)
From users_to_courses uc
JOIN users u ON uc.users_LOGIN = u.login
JOIN courses c ON uc.courses_ID = c.id
-- added this line
GROUP BY u.surname, u.name, u.login, uc.users_LOGIN
SELECT users.surname, users.name, users.login, users_to_courses.users_LOGIN from courses c
INNER JOIN users_to_courses uc ON uc.courses_ID = c.id
INNER JOIN login l ON uc.users_login = l.login
Related
I'm new to SQL. I want to get attempts of exams for every course of every student.
I've made this query in my database:
SELECT students.id, students.surname, students.name, courses.name AS "course" FROM `students`
JOIN exam_student
ON exam_student.student_id = students.id
JOIN exams
ON exam_student.exam_id = exams.id
JOIN courses
ON exams.course_id = courses.id
ORDER BY students.surname ASC, students.name ASC;
The query produces this
Now, what I want is to get for every user, the attempts of exams of every course name.
I have the exams pivot with user_id and course_id.
How can I reach this result?
Seems you want a total.
So let's COUNT the attempts by aggregating it.
SELECT s.id, s.surname, s.name
, c.name AS course
, COUNT(e.id) AS exam_attempts
FROM students s
LEFT JOIN exam_student es ON es.student_id = s.id
LEFT JOIN exams e ON e.id = es.exam_id
LEFT JOIN courses c ON c.id = e.course_id
GROUP BY s.id, s.surname, s.name, c.id, c.name
ORDER BY s.surname, s.name, c.name;
I have five tables: 1. courses 2.departmetns 3.students 4. enroll_courses 5.results
Departments has id
Courses has id, department_id
Students has id,department_id
Enroll_courses has id, courses_id,students_id
Results has id, courses_id,students_id
I have to select courses.name which are in enroll_courses tables but not in results table.
I have written the query, I couldn't get the exact answer. If anyone can help me to find the solution.
Query:
SELECT courses.name
FROM courses
JOIN departments ON courses.department_id = departments.id
JOIN students ON departments.id = students.department_id
WHERE students.id = 9
AND courses.id IN (SELECT course_id FROM enroll_courses)
Its a little bit blind shoot since you didn't provide any DDL's , but I assume results table have a course_id column , if not, change it to the relation. You can do this with EXISTS() and NOT EXISTS():
SELECT courses.name
FROM courses
JOIN departments ON courses.department_id = departments.id
JOIN students ON departments.id = students.department_id
WHERE EXISTS(SELECT 1 FROM enroll_courses ec WHERE ec.course_id = courses.id)
AND NOT EXISTS(SELECT 1 FROM results r WHERE r.course_id = courses.id)
AND students.id =9
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 users table which contains the users information (fname, lname...etc) and a invoices table. In the invoices table I have a field called created_id which links to the user.id that created the invoice. I also have a field called staff_id which links to the staff user.id that approved the invoice.
How can I query the first and last name for both the created_id and the staff_id in a single query? Here are a few things I've tried....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname
FROM
invoices
INNER JOIN users
ON users.id = invoices.created_id;
This works, but it only gets me the person's name that created the invoice. How can I add the staff's name to that as well....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname,
users2.fname as staff_fname,
users2.lname as staff_lname
FROM invoices, users
LEFT JOIN
invoices,
users AS users2
ON
users.id = invoices.created_id,
users.id = users2.id
That doesn't work, but is closer. Any guidance or examples would be very helpful. Also, if you have any recommendations for good books on learning how to do more advanced MySQL queries that would be helpful too.
You need to join users table twice on table Invoice.
SELECT a.*,
b.fname created_firstName,
b.lname created_LastName,
c.fname staff_firstName,
c.lname staff_LastName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
and best thing is you can concatenate their names into one using CONCAT
SELECT a.*,
CONCAT(b.fname, ' ', b.lname) created_fullName,
CONCAT(c.fname, ' ', c.lname) staff_fullName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
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;