Correlated subqueries sometimes let to create the query very briefly, which can look more cumbersome when you use other means. We shall remind that correlated subquery contains reference to the query which it contains (we shall name it the main query) with the result that subquery executes for each string of the main query.
It is required to determine the date and the flights of every passenger, committed by them at its last flight day
To say the other way, you have to find the maximum dateof the flight for each passenger and to find all of his flights. With determination of the maximum date there is no problems :
However, there is no flight. If we'll try to include flight in the list of output:
Here for each flight it is checked, whether it is accomplished last flight day of the given passenger. Thus if such flights there was a several, we receive all of them.
Seeming lack of the resulted solution is just also that the subquery should be calculated for every string of main query. To avoid it, it is possible to offer the alternative solution, which is using joining of the Pass_in_trip table with a subquery resulted at the beginning which calculates the maximal dates for each passenger:
However, as it will seem strangely, optimizer of the A database management system (DBMS) by Microsoft Corporation. SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Server 2000 will choose for both queries the identical execution plan at presence of a natural index (cluster) on a primary key. Accordingly, and time of execution of queries should not differ.
It should be reminded that examples given here can be run directly on the website by selecting the check box “Without checking” on the page with exercises.
Suggested exercises: 58