Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where.

Similar presentations


Presentation on theme: "Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where."— Presentation transcript:

1 Relational Databases

2 SQL Sub-queries: queries within queries  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 if 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 sub-query into 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.

3  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

4 Select name, sname From staffmember join class on (teacher = staffid) Join subject on (class.subjectid = subject.subjectid) Join marks on (subject.subjectid = marks.subjectid) Where mark > 40 And name not in (select name from staffmember join class on (teacher = staffid) Join marks on (class.subjectid = marks.subjectid) where mark < 40); Need to pull from Subject to get sname Who has passed Lists the names of staff With failed students and Excludes them

5 The basic concept is to pass a single value or many values from the sub-query 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.

6  Based on the database that is used for the ‘Tasks’ (University database) 1. Student names are held in the STUDENT table 2. 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

7 To identify the students who have failed module COMP1011 Select studentid From marks Where subjectid = ‘COMP1011’ And mark < 40; If we want to retrieve a name based on a student id Select stuname From student Where studentid = 9292145; Simply swap the known value for the query that returns the ID Select stuname From Student Where studentid in ( select studentid From marks Where subjectid = ‘COMP1011’ And mark < 40); Why use IN?

8 Select stuname From Student Where studentid in ( select studentid From marks Where subjectid = ‘COMP1011’ And mark < 40); Retrieve a list of student id’s who have mark < 40 for COMP1011 Retrieve the name of the studentid’s in this list.

9 1. The data types must match, if the studentid expects a number then the sub-query must return a number. 2. Remember = means that it is expecting a single value, you must therefore be sure that the sub-query returns only 1 result, if there is any doubt you should use the IN keyword. 3. You can nest / use as many sub-queries 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)

10 = equal to > greater than >= greater than or equal to < less than <= less than or equal to <> not equal to And other keywords …… IN NOT IN ANY ALL

11 Exercise - do in pairs 1.Write a query that will list the names of who is older than the average student. TIPthe sub-query needs to select the average age of students this should be used then as a filter. select stuname from student where age > (select avg(age) from student); This will return 25 students of the 74 who are enrolled as being older than the average age.


Download ppt "Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where."

Similar presentations


Ads by Google