I'm doing some SQL practice and have been stumped by the following question.
I'm given the database schema:
Course (Course#, title, dept)
Student (Student#, name, program)
Enrolled (Student#, Course#, grade)
I'm trying the translate the following statement to SQL:
List the names of all students who takes Computer courses or Science courses.
Initially I thought the answer might be something like this:
SELECT Sname
FROM Course,Student,Enrolled
WHERE Course.dept = "Computer" OR Course.dept = "Science"
However, I feel like the rows in the table are not joined quite how I imagined, and that there is something off with this. How far off am I?
This is not that simple: first, you need to join the tables, and then you need to group by name to eliminate duplicates:
SELECT s.name
FROM Student s
JOIN Enrolled e ON s.Student#=e.Student#
JOIN Course c ON e.Course#=c.Course#
WHERE c.dept = 'Computer' OR c.dept = 'Science'
GROUP BY s.name
GROUP BY is necessary because the same student may be taking both "Computer" and "Science" courses, in which case JOIN would produce multiple records for the same student. In this case you have an option of replacing it with DISTINCT.
If you have 2 courses Computing (1) and Science (2) with the IDs 1 and 2, you need to do a query like this:
SELECT s.first_name, s.last_name FROM students s JOIN enrolled e ON e.student_id = s.id WHERE e.course_id IN(1, 2)
Sorry may have misread, if you need to do it by course type and the courses are tagged as dept = Computer, Science, Literacy etc... Do the following query:
SELECT s.first_name, s.last_name FROM students s JOIN enrolled e ON e.student_id = s.id JOIN courses c ON c.id = e.course_id WHERE c.dept IN('Computing', 'Science')
Or if you want to do an OR:
SELECT s.first_name, s.last_name FROM students s JOIN enrolled e ON e.student_id = s.id JOIN courses c ON c.id = e.course_id WHERE c.dept = 'Computing' OR c.dept = 'Science'
Related
Write a query to display list of students name who all are from IT department. Sort the result based on students name in descending.
This query will work for any rdbms and any schema (I assume that's what you are after here)
select studentid, firstname, surname
from tblStudent s
inner join tblStudentDepartment sd on s.studentid = sd.studentid
inner join tblDepartment d on sd.departmentid = d.departmentid
where d.department = 'IT'
select s.name, s.id
from student s join takes t on t.id = s.id
where s.name like 'D%'
group by s.name, s.id
having (
select count(distinct c.course_id)
from course c
where c.dept_name = 'History' and c.course_id = t.course_id)>4
order by s.name
I am confused about how GROUP BY works. I am trying to find the students who has taken at least 5 courses from history department and name start with D.
Not sure with the nested subqueries...
course(course id, title, dept name, credits)
student(ID, name, dept name, tot_cred)
takes(ID, course_id, sec_id, semester, year, grade)
You have to additionally JOIN with course table:
select s.name, s.id
from student s
inner join takes t on t.id = s.id
inner join course c on c.course_id = t.course_id
where s.name like 'D%' and c.dept_name = 'History'
group by s.name, s.id
having count(distinct c.course_id) >= 5
The WHERE clause returns all students whose names start with a 'D' and have taken at least one course in history department. The HAVING clause filters out any students with 4 or less distinct courses in history department.
I am student and
I have such kind of a table structures
student (sid, sname, gender, age, year, gpa)
dept(dname, numphds)
course(cno, cname, dname)
enroll(sid, grade, dname, cno, sectno)
and i want the results for.....
Q-Print the names of students who are taking both a "Computer Sciences" course and a "Maths" course................
I have tried following query but it does not give me throughput..............
select e.sid, s.sname from
enroll e ,enroll e1 ,student s ,course c
where s.sid=e.sid,
e.cno =c.cno
and e.sid=e1.sid
and c.cname='computer science' and c.name='maths';
It's been more than 20 years since proper joins were introduced to SQL. FFS use them!
Try this:
select s.sid, s.sname
from student s
join enroll e on e.sid = s.sid
join course c on c.cno = e.cno and c.cname='computer science'
join enroll e1 on e1.sid = s.sid
join course c1 on c1.cno = e1.cno and c1.name='maths'
Note how you may put non-key conditions in a join condition. Although the query would still work if the course names were in a where clause, putting the conditions in the join clause makes it more obvious where they apply, which improves readability.
By formatting well and ordering the tables sanely, you can actually read the query and (hopefully) understand it.
use the following code
select e.said, s.sname from enroll e inner join
enrool e1 on e1.sid = e.sid inner join
student s on s.sid = e.sid inner join
course c on c.cno = e.cno
where c.cname = 'computer science' and c.name = 'maths'
if you need to use the left join then use
select e.said, s.sname from enroll e left join
enrool e1 on e1.sid = e.sid left join
student s on s.sid = e.sid left join
course c on c.cno = e.cno
where c.cname = 'computer science' and c.name = 'maths'
This is a question I could not answer in oracle lab exam.
Given the schema:
(Courses: cid(int), deptid(int)...);
(Students: sid(int), sname (string), deptid(int)...);
(Participation: cid(int), sid(int), ...);
A student can attend courses outside his department.
Need to get the names of the students who take all the courses offered by his department.
How to do this in sqlplus?
SELECT s.sid, s.sname, s.deptid
FROM Students s
INNER JOIN Participation p
ON s.sid = p.sid
INNER JOIN Courses c
ON p.cid = c.cid
AND s.deptid = c.deptid
GROUP BY s.sid, s.sname, s.deptid
HAVING COUNT(DISTINCT c.cid) = (SELECT COUNT(*)
FROM Courses c2
WHERE c2.deptid = s.deptid)
I cannot tesst the query right now, so I don't know if I have a syntactic error, anyway, you can try this idea to achieve your requirements.
SELECT studentName
FROM
(SELECT stu.sname AS studentName,
cour.deptid AS dept,
COUNT(*) AS assistedCoursesByDpt,
Max(cour.total) AS total
FROM students stu,
participation part,
(SELECT cour.deptid,COUNT(*) AS total FROM courses cour GROUP BY cour.deptid
) AS cour
WHERE stu.sid=part.sid
AND part.cid =cour.cid
GROUP BY stu.sid,
cour.deptid
)
WHERE total=assistedCoursesByDpt
Th idea is to create a subquery (cour) that has a new calculated column, the total courses by debt. Then you can compare this total with the agrouped student courses by dept.
Guess I am in a complex situation. Heres the scene.
I have 3 tables in SQL Server.
Enrollment, Student, Course. (FYI, there are no foreign keys)
The table columns are
Student - StudentId, StudentName
Course - CourseId, CourseName
Enrollment - EnrollmentId, StudentId, CourseId, CourseResult
Sample Data
Student - s1, Sid
Course - c1, Science
Enrollment - 1, s1, c1, 80
I would want a SQL query that selects data like below
1, s1, Sid, c1, Science, 80
I did it hard way in the DAL layer with multiple calls to database. But would like to do it in one call, and in DB.
Any one for the rescue!
Use a join.
select enrollment.*, student.*, course.* from enrollment
inner join Student on enrollment.studentId = student.studentId
inner join Course on enrollment.courseId = course.courseId
There actually are foreign keys in your data model. They may not be marked as such in the db or you don't realize it. I also think it would become clearer if in your list you put the Enrollment table second, between Student and Course. Enrollment is the table that links a student to a course.
I think you just want an inner join on the three tables like this:
SELECT e.EnrollmentId, s.StudentId, c.CourseId, c.CourseName, e.CourseResult
FROM Student AS s
JOIN Enrollment AS e ON s.StudentId = e.StudentId
JOIN Course AS c on e.CourseId = c.CourseId
Seems like a few simple JOINs would do it...
select
e.EnrollmentId
,s.StudentId
,s.StudentName
,c.CourseId
,c.CourseName
,e.CourseResult
from Enrollement e
inner join Course c on e.CourseId = c.CourseId
inner join Student s on e.StudentId = s.StudentId
Try:
SELECT e.EnrollmentId, s.StudentId, s.StudentName, c.CourseId, c.CourseName, e.CourseResult
FROM Student s JOIN
Enrollment e ON s.StudentId = e.StudentId JOIN
Course c ON e.CourseId = c.CourseId