subqueries. so far when data has been filtered the filter has been known and simply added to the...
TRANSCRIPT
![Page 1: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/1.jpg)
SubqueriesSubqueries
![Page 2: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/2.jpg)
So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter value is OR you don’t want to hardcode a value into the query. If the filter can be found elsewhere in the database then you can put a subquery in the WHERE clause.
•Allows for variable values (list student name who have got higher than average marks, name is in one table and average marks from another table.)•Hard coding is poor practice•Values change•Etc.
SQL Subqueries: queries within queries
![Page 3: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/3.jpg)
Seminar task solutionSeminar task solution Which tutors teach modules that all students
have passed. The question asked IF there were tutors who had all
students pass, select all tutors who have passed students and select tutors who have failed then compare the results (2 queries needed)
Sub-queries are a better way Put the query that lists the tutors with failures
and add it to the where clause
Which tutors teach modules that all students have passed. The question asked IF there were tutors who had all
students pass, select all tutors who have passed students and select tutors who have failed then compare the results (2 queries needed)
Sub-queries are a better way Put the query that lists the tutors with failures
and add it to the where clause
![Page 4: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/4.jpg)
Use sub-queryUse sub-querySelect name, sname
From staffmember join class on (teacher = staffid)
Join enrolled on (class.subjectid = subject.subjectid)
Join marks on (subject.subjectid = marks.subjectid)
Where mark >= 40
And staffid not in (Select staffid
From staffmember join class on (teacher = staffid)
Join marks using (subjectid)
where mark < 40);
Need to pull from Subject to get sname
Who has passed
Lists the names of staffWith failed students and
Excludes them
![Page 5: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/5.jpg)
The basic concept is to pass a single value or manyvalues from the subquery to the next query and so on.
4
3
2
1
When reading or writing SQL subqueries, you should start from the bottom upwards, working out which data is to be passed to the next query up.
![Page 6: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/6.jpg)
Worked example ....
Based on the database that is used for the ‘Tasks’ (University database)
1.Student names are held in the STUDENT table2.Student marks are recorded in the MARKS table but marks are allocated against the STUDENTID
If we want to list the names of the students who have failed the module we need to first identify the students who have failed the module, then use this list to select the names for students with those id’s
![Page 7: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/7.jpg)
Why use IN?
![Page 8: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/8.jpg)
Retrieve a list of student id’s who have mark < 40 for COMP1011
Retrieve the name of the
studentid’s in this list.
![Page 9: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/9.jpg)
Rules for Subqueries:
1.The data types must match, if the studentid expects a number then the subquery must return a number.2.Remember = means that it is expecting a single value, you must therefore be sure that the subquery returns only 1 result, if there is any doubt you should use the IN keyword.3.You can nest / use as many subqueries as you like.4.This is not a very efficient way of coding or pulling data from multiple tables, and you may be able to generate the required result using joins (covered later in the module)
![Page 10: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/10.jpg)
ComparatorsComparators
= equal to> greater than>= greater than or equal to< less than<= less than or equal to<> not equal to
and other keywords ……
INNOT INANYALL
![Page 11: Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter](https://reader035.vdocuments.site/reader035/viewer/2022072010/56649ddb5503460f94ad1b9f/html5/thumbnails/11.jpg)
Exercise - do in pairs
1. Write a query that will list the names of who is older than the average student.
TIP the sub-query needs to select the average age of studentsthis should be used then as a filter.
select stunamefrom studentwhere age >
(select avg(age) from student);
This will return 25 students of the 74 who are enrolled as being older than the average age.