convert inner join query to subquery - sql

I have 3 tables where I am trying to find answer combining them and try getting these columns
select student_name,course_title,last_name
from students
inner join student_enrollment
on students.student_no = student_enrollment.student_no
inner join courses
on courses.course_no = student_enrollment.course_no
inner join teach
on courses.course_no = teach.course_no
order by student_name
I want to convert my inner join query into a subquery , how can I achieve the same result? I have tried some but its not running
my attempted solution
select (select student_name from students where students.student_no = student_enrollment.student_no) as student_name,
(select last_name from teach where teach.course_no = student_enrollment.course_no) as professor_name,
(select course_title from courses where courses.course_no = teach.course_no ) as course_title
from student_enrollment

Not sure what error you are getting but I believe your issue could be comming from the fact that in your third subquery you are trying to link to a table in your second subquery.
Each subquery does not know about the other, only about the tables in the main query.....
so where you have
courses.course_no = teach.course_no
you can replace it with
courses.course_no = student_enrollment.course_no
Then I think you should work....
Have a look at the dbfiddle I have created below for an example that is similar
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=31aa8d0ab9e139d1eae2d50e703caff5

Related

How to display the desired rows of two tables using a subquery?

My subquery:
select studentName, Course.dataStart
from Student,
Course
where Student.id in (select Course.id from Course);
I need a solution to this (above) subquery (not a join)
Why does the SQL subquery display one date for each name? (task: display the names of students from the Student table and the course start date from the Course table using a subquery)
With the help of Join, I get it as it should: (but I need to do it with a subquery)
You seem to be using implicit join syntax, but really you should be using an explicit inner join:
SELECT s.studentName, c.dataStart
FROM Student s
INNER JOIN Course c
ON c.id = s.course_id;
If you really wanted to use the implicit join syntax, it should be something like this:
SELECT s.studentName, c.dataStart
FROM Student s, Course c
WHERE c.id = s.course_id;
But again, please use the first version as its syntax is considered the best way to do it.
You can apply join :
SELECT S.studentName, C.dataStart
FROM Student S
INNER JOIN Course C
ON C.id = S.course_id;
With Sub query:
Select studentName, (Select Course.dataStart from Course
Where Course.id = course_id)
From Student
Asuming that Course.Id field is Student.Id (although it seems strange to me), I think the only way to get the results you want with a subquery would be using it in the SELECT clause:
select studentName, (SELECT Course.dataStart FROM Course WHERE Course.Id = Student.Id)
from Student
This would fail if you have more than 1 row in Course per Student, in that case you could use (SELECT DISTINCT Course.dataStart...)

How to not using inner select

select student_surname,
student_name,
student_recordbook,
student_kurs,
student_state,
student_dep_id,
student_kafedra_id
from
student
where student_studgroups = (select studgroups_number
from studgroups
join study on study_studgroups_id = studgroups_id
where studgroups_year != study_kurs
and study_state_id IN (1,2,3,4,5,6,7,21,22,23,24));
I need to optimize it without a subquery.
Thanks.
You can try like below-
select * from student a
inner join student_groups b on a.student_studgroup=b.studgroups_number
inner join study on study_studgroup_id=stud_Group_id
where (your condition --)
Please try this..
select *
from student s
join studgroups sg on (s.studygroups = sg.studgroups_number)
join study st on (sg.studgroupys_id = st.study_studgroups_id)
where sg.studgroups_year !=s.study_kurs
and s.study_state_id IN (1,2,3,4,5,6,7,21,22,23,24);
What I did here, I joined all three tables with the help of primary and foriegn key.
So you can get the same result using JOIN what you get from using subquery.

SQLITE3 Inner Join Returning Too Many Tuples

I have 4 tables in sqlite3:
students(student_id, student_name)
instructors(instructor_id, instructor_name)
courses(course_id, course_name)
and
enrollments(enroll_id, student_id, instructor_id, course_id)
With the last 3 columns having a foreign key reference to the relevant columns in the first 3 tables
When I try to do a query that shows only the student’s name, instructor’s name and course name where an enroll_id is equal to 001 for example
SELECT students.student_name, instructors.instructor_name, courses.course_name
FROM users, instructors, courses INNER JOIN enrollments
WHERE enrollments.enroll_id = 001;
I'm getting a lot of data from all tables, but not a single tuple with just the relevant enroll data assigned to enroll_id 001. Any help is appreciated
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. The right way to express this query is:
SELECT s.student_full_name, i.instructor_name, c.course_name
FROM enrollments e JOIN
instructors i
ON e.instructor_id = i.instructor_id JOIN
courses c
ON e.course_id = c.course_id JOIN
students s
ON e.student_id = s.student_id
WHERE e.enroll_id = 001;
The use of table aliases is highly recommended. It makes the query easier to write and to read.
Try this:
SELECT students.student_full_name, instructors.instructor_name, courses.course_name
FROM users, instructors, courses, enrollments
WHERE enrollments.enroll_id = 001
and enrollments.student_id = users.student_id
and enrollments.instructor_id = instructors.instructor_id
and enrollments.course_id = courses.course_id;

What Join to use against 2 Tables for All Data

Hi I am looking to find out what join I would use if I wanted to join 2 tables together. I currently have a list of all students so 25 students to 1 class and the other table only shows 7 of those names with their test results.
What I would like is to have 1:1 join for the ones with the test results and the other ones without I would like to show them underneath so all in all I have 20 records.
If somebody could please advise on how I could achieve this please.
Thanks in advance.
It sounds like you want an OUTER JOIN.
For this example, we'll assume that there is a table named student and that it contains a column named id which is UNIQUE (or PRIMARY) KEY.
We'll also assume that there is another table named test_result which contains a column named student_id, and that column is a foreign key referencing the id column in student.
For demonstration purposes, we'll just make up some names for the other columns that might appear in these tables, name and score.
SELECT s.id
, s.name
, r.score
FROM student s
LEFT
JOIN test_result r
ON r.student_id = s.id
ORDER
BY r.student_id IS NULL
, s.score DESC
, s.id
Note that if student_id is not unique in test_result, there is potential to return multiple rows that match a row in student.
To get (at most) one row returned from test_result per student, we could use an inline view.
SELECT s.id
, s.name
, r.score
FROM student s
LEFT
JOIN ( SELECT t.student_id
, MAX(t.score) AS score
FROM test_result t
GROUP BY t.student_id
) r
ON r.student_id = s.id
ORDER
BY r.student_id IS NULL
, s.score DESC
, s.id
The expressions in the ORDER BY clause are designed to return the students that have matching row(s) in test_result first, followed by students that don't.
This is just a demonstration, and very likely excludes some important criteria, such as which test a score should be returned for. But without a sample schema and some example data, we're just guessing.
You are looking for a left outer join or a full outer join.
The left outer join will show all students and their tests if they have them.
select *
from Students as s
left outer join Tests as t
on s.StudentId = t.StudentId
The full outer join will show all students with their tests if they have them, and tests even if they do not have students.
select *
from Students as s
full outer join Tests as t
on s.StudentId = t.StudentId

SQL Query - Join

I have a Table which contains student information(name, id, course,....), and I have another table which contains information like(id, student leave)
I need to generate a report, which is by course all the students who were absent....
Am confused on how to do this properly - Do i use an outer join or what cause SQL 5.0 keeps giving me errors like syntax error....
Have to use this query in servlets...!!!
Currently am using the query in 2 parts.... but which is not generating the table properly....
SELECT * FROM Student s LEFT JOIN Attendance a ON a.student_id = s.id WHERE s.course_id = "ENG"
SELECT *
FROM Student s, Attendance a
WHERE s.id = a.student_id
AND s.course_id = "ENG"
AND s.id = <the_retrieved_id>
Should also be possible.
I suggest turning Student.id to Student.student_id to make it clear,
and also use integer for keys in course table.
then use join like Pradeep Singh only with "using":
select * from Student s left join Attendance a using (student_id) where s.course_id="ENG"
Select * From Student s left join Attendance a on a.student_id = s.id where course_id='ENG'