Exercise #8 (tips and solutions) page 2 |
|||||
On the example of this simple task, a great variety of solution approaches, owing to the flexibility of SQL, can be demonstrated. Solution 4.4.4. Outer self-join
The Product table is joined to itself using LEFT JOIN on the condition the maker is the same, and the product type for the second table is laptop. The p1.maker column will be NULL if a manufacturer doesn’t have any laptop models; this is used in the WHERE clause together with the condition the product type for the record checked is PC. Solution 4.4.5. Grouping
In the subquery, unique pairs (supplier, type) are selected with type being either PC or Laptop. Then, grouping by supplier is performed, whereby the rows grouped have to meet the following conditions:
With so many solution approaches, it’s natural to ask about their efficiency – that is, which query will be executed faster. Leading the field both in number of operations and estimated execution time is solution 4.4.5. The third solution has the weakest performance. Others are estimated to take about twice as much time to execute as the leader. In Management Studio (SQL Server), you can get the estimated execution time value and the query execution plan by first running the command
SET SHOWPLAN_ALL ON;
and then the queries to be analyzed. To return to normal mode, type
SET SHOWPLAN_ALL OFF;
If 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 isn’t installed on your computer, you can get the query execution plan directly from the site. Return to discussion of exercise #8
|