Mastering SQL: Queries - Take It Easy

You don’t have to be an SQL guru to realize that adding another condition to the WHERE clause will not pick up your quer

1,150 176 340KB

English Pages [25] Year 2016

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Mastering SQL: Queries - Take It Easy

Citation preview

Mastering SQL: Queries – Take It Easy By Yuli Vasiliev

Mastering SQL: Queries – Take It Easy By Yuli Vasiliev Trademarked names appear in this book. Where the author was aware of those trademarks, they have been written in initial caps, in the same way they appear in the vendor documentation. The information included to this work is believed to be accurate and dependable. However, due to the fact that things in the IT world change quickly as well as the possibility of human errors, the author cannot guarantee the completeness or accuracy of the information provided in this work, and will not be responsible for possible errors, omissions, or damages caused directly or indirectly by the use of the information contained herein.

Table of Contents

Using Non-Equi Joins Reporting on Sparse Datetime Data Performing Multi-level Aggregation How count Handles NULLs Excluding a Column Presented In The GROUP BY Clause From The SELECT List Conclusion

This text provides some interesting examples of using SQL when it comes to querying database data. The level of complexity implies you’re not a total beginner to SQL and have at least a basic understanding of how SQL queries are built. To understand and follow the samples though, you are not supposed to be an advanced SQL user either – each example comes with a detailed explanation of how it works, illustrating that even complex data management tasks can be taken easy with the proper use of SQL capabilities. The sample queries provided in this text are implemented with Oracle SQL and MySQL SQL dialects, meaning you’ll need to have access either to Oracle Database or to MySQL to follow them. With some adjustments, the samples can be used in other systems, of course. In other words, the sample queries you’ll see here are not specific to a certain database platform, focusing on some interesting data management tasks, rather than system specific ones.

Using Non-Equi Joins In most cases, a join is a query on two tables connected through two columns – each from a different table – with the help of the equality operator. In the case of a non-equi join however, that would be most likely wrong to the first statement, wrong to the second, and wrong to the third. The point is, non-equi joins are those ones in which the join condition relies on an inequality comparison performed within the same column, and are often defined on the same table, thus representing self joins. In the following example, you build a non-equi-join over a table whose rows contain information about three football teams. The structure of the team table used in this example is very straightforward and consists just of two columns: team_no and team_name. You are supposed to prepare a schedule for a series of games so that each team meets the others once. As you might guess, using an equi-join is not an option here, because you would end up with each team scheduled to play against itself – just like in the following example, assuming you have the team table already created and filled with the data: SELECT t1.team_name team1, t2.team_name team2 FROM team t1, team t2 WHERE t1.team_no = t2.team_no TEAM1 TEAM2 –––––––––––IceMen IceMen Slavia Slavia Amazzo Amazzo Of course, that’s not what you want to see. In contrast, using an inequality comparison brings the desired results. All you need to do is to replace t1.team_no = t2.team_no with t1.team_no < t2.team_no in the above query to get the results you need, which might look like this: TEAM1 TEAM2 –––––––––––-

IceMen Slavia IceMen Amazzo Slavia Amazzo Although, this time you have the right combination, that’s not it, though. Suppose you also have to set a date for each game. Being an Oracle database user, you can do that with the SYSDATE function along with the ROWNUM function. So, if you need to establish the first match in a week and then each next day, then the query might look like this: SELECT t1.team_name team1, t2.team_name team2, SYSDATE + ROWNUM + 6 GAME_DATE FROM team t1, team t2 WHERE t1.team_no < t2.team_no Producing the following output: TEAM1 TEAM2 GAME_DATE –––––––––––––––––––– IceMen Slavia 17-AUG-15 IceMen Amazzo 18-AUG-15 Slavia Amazzo 19-AUG-15 If you are using MySQL, the query might look like this to produce the same output: SELECT t1.team_name team1, t2.team_name team2, INTERVAL (@rownum:=@rownum+1) + 6 DAY + CURDATE() GAME_DATE FROM team t1, team t2, (SELECT @rownum:=0) r WHERE t1.team_no < t2.team_no;

Reporting on Sparse Datetime Data Alternatively, this section might be titled: Fabricating Data Sets, because that’s exactly what you often have to do when it comes to reporting on sparse data: densify a sparse data set by fabricating the missing values. In the case of date data, such missing values can be generated on the fly, within a query. For example, you may need to generate a row per hour within a certain day or generate a row per day within a certain date range. Often you need a report showing every date/time position within the entire range, regardless of whether there was an activity on a particular day or hour, or not. Consider the following example. Suppose you want to generate an order total report for the following date diapason: 01-jan-15 to 15-jan-15, which would include a row for each day within the specified diapason, whether there were orders on a given day or not. In other words, you need to build a query that will output a row for each day of those fifteen in the diapason. The only guaranteed way to achieve this is with the help of a join connecting the selection from the orders table to the desired date range set, which might be either generated on the fly or selected from a table. When using Oracle Database, you can choose between these two options. In MySQL, you have only the latter option. Let’s start with an example for Oracle Database. To keep things simple, first let’s look at the query generating the required date set. Such a query might look like this: SELECT rownum num, TO_DATE(‘01-jan-15’, ‘dd-mon-yy’) + INTERVAL ‘1’ DAY*rownum-1 date_ FROM dual CONNECT BY LEVEL