SQL Query - Join - sql

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'

Related

convert inner join query to subquery

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

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.

sql join tables IS NOT

I need to get the data of all teachers who gives a course but does not have an assignement. I tried using inner or left join so if the name of the teacher is not in the table of the assignments it will show the data. If the name of the teacher is in the table of assignments it must not show the data. But I could not get it working.
What am I doing wrong?
select teachers.nameteacher, courses.namecourse, courses.codecourse
from teachers
inner join courses
on teachers.codecourse = courses.codecourse
left join assignments
on assignments.nameteacher = teachers.nameteacher
where teachers.nameteacher IS NULL
filter should be from assignments table
SELECT t.nameteacher,
c.namecourse,
c.codecourse
FROM teachers t
INNER JOIN courses c
ON t.codecourse = c.codecourse
LEFT JOIN assignments a
ON a.nameteacher = t.nameteacher
WHERE a.nameteacher IS NULL --should be assignments
Note : start using alias names to make the query more readable
The easy (and efficient) way, if you are using PostgreSQL or Microsoft SQL Server is to say:
select
a.nameteacher,
c.namecourse,
c.codecourse
from
(
select nameteacher from teachers
except
select nameteacher from assignments
) a
join teachers b
on a.nameteacher = b.nameteacher
join courses c
on b.codecourse = c.codecourse;
If you are using Oracle change "except" to "minus".
If you are using SQLite or MySQL you need to use one of the other suggestions because these don't support except or minus.

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;

Connecting Three Tables SQL

Hello what I'm trying to do is retrieve values from tables but I'm having a problem with forming a correct SQL statement. This is what i have.
$qry_display = "SELECT a.section_id, b.section_id,b.student_id,c.*
FROM tbl_section AS a
LEFT OUTER JOIN tbl_er AS b On a.section_id = b.section_id
LEFT OUTER JOIN tbl_enroll AS c On b.student_id = c.student_id
WHERE b.student_id=c.student_id
AND a.bname='$branch'";
This is my database structure.
tbl_section:
section_id section_name sy adviser_id level
tbl_er:
student_id section_id
tbl_enroll
student_id fname lname
I'm having problems with forming the proper sql statement would appreciate any help on how to do it right. What i want is to show all students Under a given section.
In what table bname belongs? change bname into sectionname. Try this one,
SELECT c.*
FROM tbl_section a
INNER JOIN tbl_er b
on a.section_ID = b.section_ID
INNER JOIN tbl_enroll c
ON b.student_ID = c.student_ID
WHERE a.sectionname = 'sectionNameHere'