Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
The Language of SQL- P32:Research has shown that, being pressed for time, most readers tend to skip the introduction of any book they happen to read and then proceed immediately to the first real chapter. With that fact firmly in mind, we will only cover relatively unimportant material in the introduction, such as an explanation of what you will and will not learn by reading this book. | CHAPTER 14 SUBQUERIES Keyword Introduced EXISTS In Chapter 4 we talked about composite functions. These were functions that contained other functions. In a similar manner it is possible for SQL queries to contain other queries. The queries that are contained within other queries are called subqueries. The topic of subqueries is somewhat complex primarily because there are many different ways in which they can be used. Subqueries can be found in many different parts of the SELECT statement each with different nuances and requirements. As a query contained within another query subqueries can be related to and dependent on the main query or they can be completely independent of the main query. Again this distinction results in different usage requirements. No matter how subqueries are used they add a great deal of flexibility to the ways in which you can write SQL queries. It many cases subqueries provide functionality that could be accomplished by other means. In such instances personal preferences will come into play as you decide whether or not you want to utilize the subquery solution. However as you will see there are certain situations where subqueries are absolutely essential for the task at hand. With that let s begin our discussion with an outline of the basic types of subqueries. Types of Subqueries Subqueries can be utilized not only with SELECT statements but also with the INSERT UPDATE and DELETE statements which will be covered in 141 142 Chapter 14 Subqueries Chapter 17. In this chapter however we re going to restrict our discussion of subqueries to the SELECT. Here s the general SELECT statement that we have seen SELECT columnlist FROM tablelist WHERE condition GROUP BY columnlist HAVING condition ORDER BY columnlist Subqueries can be inserted into virtually any of the clauses in the SELECT statement. However the way in which the subquery is stated and used varies slightly depending on whether it is used in a columnlist a tablelist or a condition. But