How to not using inner select - sql

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.

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

SQL on three tables

I have 3 tables
cl_address: FK - cl_ser_id, FK - add_ser_id
address: PK - add_ser_id
youths_info: FK - cl_ser_id
I would like to
select *
from youths_info
select *
from address
Can some guru please help and with code and suggest the most optimized SQL?
Below is my trial code which is causing an error on the last INNER JOIN
SELECT a.*, y.*
FROM [informix.address] a
INNER JOIN [informix.cl_address] cla ON a.addr_ser_id = cla.addr_ser_id
INNER JOIN [informix.cl_address] cla ON [youth_info] y
cla.cl_ser_id = y.cl_ser_id
As there is no sample data and desired result, I am assuming that you need query as below.
SELECT a.*,
yi.*
FROM address a
JOIN cl_address cla ON a.add_ser_id = cla.add_ser_id
JOIN youths_info yi ON yi.cl_ser_id = cla.cl_ser_id;
The above query will return inner results of all columns from address and youth_info table. If you need left or right outer result, you have to adjust the joins in above query according to your dbms.

use Inner Join in SQL

I want to join two tables and then I want to join this result with another table
but it doesn't work
select * from
(
(select SeId,FLName,Company from Sellers) s
inner join
(select SeId,BIId from BuyInvoices) b
on s.SeId=b.SeId
) Y
inner join
(select * from BuyPayments) X
on Y.BIId=X.BIId
thanks
In most databases, your syntax isn't going to work. Although parentheses are allowed in the FROM clause, they don't get their own table aliases.
You can simplify the JOIN. This is a simpler way to write the logic:
select s.SeId, s.FLName, s.Company, bp.*
from Sellers s inner join
BuyInvoices b
on s.SeId = b.SeId inner join
BuyPayments bp
on bp.BIId = b.BIId;

Adding a condition to an inner join query in Oracle

I have this inner join query:
select *
from ioa_invoice_line
INNER JOIN ioa_invoice
ON ioa_invoice_line.invo_id = ioa_invoice.id ;
Now, I want to add this condition also in the above inner join that is
where ioa_invoice_line.invo_id =234
Please advise how to add this condition in above query.
As Felix says in his comment you can add it without problems:
select *
from ioa_invoice_line
INNER JOIN ioa_invoice
ON ioa_invoice_line.invo_id = ioa_invoice.id
AND ioa_invoice_line.invo_id = 234
As this is criteria on the first table, you would usually simply add this WHERE clause at the end of your query (before the semicolon of course).
However, you are dealing with an invoice table and its detail table here and the criteria is on the key linking the tables. So for readability, I would swap the tables and name the parent table first and join the child table. That feels more natural:
select *
from ioa_invoice i
join ioa_invoice_line il on il.invo_id = i.id
where i.id = 234;
select * from
ioa_invoice_line il
INNER JOIN ioa_invoice i
ON il.invo_id = i.id
where il.invo_id = 234
This format use as a professional practice

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'