+612 9045 4394
SQL Cookbook : Cookbooks (O'Reilly) - Anthony Molinaro

SQL Cookbook

Cookbooks (O'Reilly)


Published: 1st December 2005
Ships: 7 to 10 business days
7 to 10 business days
RRP $79.99

eBook View Product

Published: 9th February 2009
Format: ePUB

You know the rudiments of the SQL query language, yet you feel you aren't taking full advantage of SQL's expressive power. You'd like to learn how to do more work with SQL inside the database before pushing data across the network to your applications. You'd like to take your SQL skills to the next level.

Let's face it, SQL is a deceptively simple language to learn, and many database developers never go far beyond the simple statement: SELECT FROM WHERE . But there is "so" much more you can do with the language. In the "SQL Cookbook," experienced SQL developer Anthony Molinaro shares his favorite SQL techniques and features. You'll learn about: Window functions, arguably the most significant enhancement to SQL in the past decade. If you're not using these, you're missing out Powerful, database-specific features such as SQL Server's PIVOT and UNPIVOT operators, Oracle's MODEL clause, and PostgreSQL's very useful GENERATE_SERIES function Pivoting rows into columns, reverse-pivoting columns into rows, using pivoting to facilitate inter-row calculations, and double-pivoting a result set "Bucketization," and why you should never use that term in Brooklyn. How to create histograms, summarize data into buckets, perform aggregations over a moving range of values, generate running-totals and subtotals, and other advanced, data warehousing techniques The technique of "walking a string," which allows you to use SQL to parse through the characters, words, or delimited elements of a string

Written in O'Reilly's popular Problem/Solution/Discussion style, the "SQL Cookbook" is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The "SQL Cookbook" moves quickly from problem to solution, saving you time each step of the way.

Prefacep. xiii
Retrieving Recordsp. 1
Retrieving All Rows and Columns from a Tablep. 1
Retrieving a Subset of Rows from a Tablep. 2
Finding Rows That Satisfy Multiple Conditionsp. 2
Retrieving a Subset of Columns from a Tablep. 3
Providing Meaningful Names for Columnsp. 3
Referencing an Aliased Column in the WHERE Clausep. 4
Concatenating Column Valuesp. 5
Using Conditional Logic in a SELECT Statementp. 6
Limiting the Number of Rows Returnedp. 7
Returning n Random Records from a Tablep. 9
Finding Null Valuesp. 11
Transforming Nulls into Real Valuesp. 11
Searching for Patternsp. 12
Sorting Query Resultsp. 14
Returning Query Results in a Specified Orderp. 14
Sorting by Multiple Fieldsp. 15
Sorting by Substringsp. 16
Sorting Mixed Alphanumeric Datap. 17
Dealing with Nulls when Sortingp. 20
Sorting on a Data Dependent Keyp. 26
Working with Multiple Tablesp. 28
Stacking One Rowset atop Anotherp. 28
Combining Related Rowsp. 30
Finding Rows in Common Between Two Tablesp. 31
Retrieving Values from One Table That Do Not Exist in Anotherp. 33
Retrieving Rows from One Table That Do Not Correspond to Rows in Anotherp. 37
Adding Joins to a Query Without Interfering with Other Joinsp. 38
Determining Whether Two Tables Have the Same Datap. 41
Identifying and Avoiding Cartesian Productsp. 47
Performing Joins when Using Aggregatesp. 49
Performing Outer Joins when Using Aggregatesp. 53
Returning Missing Data from Multiple Tablesp. 56
Using NULLs in Operations and Comparisonsp. 60
Inserting, Updating, Deletingp. 62
Inserting a New Recordp. 63
Inserting Default Valuesp. 63
Overriding a Default Value with NULLp. 65
Copying Rows from One Table into Anotherp. 65
Copying a Table Definitionp. 66
Inserting into Multiple Tables at Oncep. 67
Blocking Inserts to Certain Columnsp. 69
Modifying Records in a Tablep. 70
Updating when Corresponding Rows Existp. 71
Updating with Values from Another Tablep. 72
Merging Recordsp. 75
Deleting All Records from a Tablep. 77
Deleting Specific Recordsp. 78
Deleting a Single Recordp. 78
Deleting Referential Integrity Violationsp. 79
Deleting Duplicate Recordsp. 79
Deleting Records Referenced from Another Tablep. 81
Metadata Queriesp. 83
Listing Tables in a Schemap. 83
Listing a Table's Columnsp. 84
Listing Indexed Columns for a Tablep. 85
Listing Constraints on a Tablep. 87
Listing Foreign Keys Without Corresponding Indexesp. 88
Using SQL to Generate SQLp. 91
Describing the Data Dictionary Views in an Oracle Databasep. 93
Working with Stringsp. 95
Walking a Stringp. 95
Embedding Quotes Within String Literalsp. 98
Counting the Occurrences of a Character in a Stringp. 99
Removing Unwanted Characters from a Stringp. 100
Separating Numeric and Character Datap. 101
Determining Whether a String Is Alphanumericp. 105
Extracting Initials from a Namep. 110
Ordering by Parts of a Stringp. 114
Ordering by a Number in a Stringp. 115
Creating a Delimited List from Table Rowsp. 121
Converting Delimited Data into a Multi-Valued IN-Listp. 127
Alphabetizing a Stringp. 133
Identifying Strings That Can Be Treated as Numbersp. 139
Extracting the nth Delimited Substringp. 145
Parsing an IP Addressp. 151
Working with Numbersp. 155
Computing an Averagep. 155
Finding the Min/Max Value in a Columnp. 157
Summing the Values in a Columnp. 159
Counting Rows in a Tablep. 160
Counting Values in a Columnp. 162
Generating a Running Totalp. 163
Generating a Running Productp. 166
Calculating a Running Differencep. 169
Calculating a Modep. 170
Calculating a Medianp. 173
Determining the Percentage of a Totalp. 177
Aggregating Nullable Columnsp. 180
Computing Averages Without High and Low Valuesp. 181
Converting Alphanumeric Strings into Numbersp. 182
Changing Values in a Running Totalp. 185
Date Arithmeticp. 188
Adding and Subtracting Days, Months, and Yearsp. 188
Determining the Number of Days Between Two Datesp. 190
Determining the Number of Business Days Between Two Datesp. 192
Determining the Number of Months or Years Between Two Datesp. 197
Determining the Number of Seconds, Minutes, or Hours Between Two Datesp. 200
Counting the Occurrences of Weekdays in a Yearp. 201
Determining the Date Difference Between the Current Record and the Next Recordp. 213
Date Manipulationp. 219
Determining if a Year Is a Leap Yearp. 219
Determining the Number of Days in a Yearp. 226
Extracting Units of Time from a Datep. 229
Determining the First and Last Day of a Monthp. 232
Determining All Dates for a Particular Weekday Throughout a Yearp. 234
Determining the Date of the First and Last Occurrence of a Specific Weekday in a Monthp. 241
Creating a Calendarp. 248
Listing Quarter Start and End Dates for the Yearp. 266
Determining Quarter Start and End Dates for a Given Quarterp. 271
Filling in Missing Datesp. 278
Searching on Specific Units of Timep. 288
Comparing Records Using Specific Parts of a Datep. 289
Identifying Overlapping Date Rangesp. 292
Working with Rangesp. 298
Locating a Range of Consecutive Valuesp. 298
Finding Differences Between Rows in the Same Group or Partitionp. 303
Locating the Beginning and End of a Range of Consecutive Valuesp. 312
Filling in Missing Values in a Range of Valuesp. 317
Generating Consecutive Numeric Valuesp. 321
Advanced Searchingp. 325
Paginating Through a Result Setp. 325
Skipping n Rows from a Tablep. 328
Incorporating OR Logic when Using Outer Joinsp. 331
Determining Which Rows Are Reciprocalsp. 334
Selecting the Top n Recordsp. 335
Finding Records with the Highest and Lowest Valuesp. 337
Investigating Future Rowsp. 339
Shifting Row Valuesp. 342
Ranking Resultsp. 345
Suppressing Duplicatesp. 346
Finding Knight Valuesp. 349
Generating Simple Forecastsp. 356
Reporting and Warehousingp. 365
Pivoting a Result Set into One Rowp. 365
Pivoting a Result Set into Multiple Rowsp. 367
Reverse Pivoting a Result Setp. 375
Reverse Pivoting a Result Set into One Columnp. 377
Suppressing Repeating Values from a Result Setp. 380
Pivoting a Result Set to Facilitate Inter-Row Calculationsp. 383
Creating Buckets of Data, of a Fixed Sizep. 385
Creating a Predefined Number of Bucketsp. 389
Creating Horizontal Histogramsp. 394
Creating Vertical Histogramsp. 396
Returning Non-GROUP BY Columnsp. 399
Calculating Simple Subtotalsp. 404
Calculating Subtotals for All Possible Expression Combinationsp. 408
Identifying Rows That Are Not Subtotalsp. 418
Using Case Expressions to Flag Rowsp. 420
Creating a Sparse Matrixp. 422
Grouping Rows by Units of Timep. 423
Performing Aggregations over Different Groups/Partitions Simultaneouslyp. 427
Performing Aggregations over a Moving Range of Valuesp. 429
Pivoting a Result Set with Subtotalsp. 436
Hierarchical Queriesp. 441
Expressing a Parent-Child Relationshipp. 442
Expressing a Child-Parent-Grandparent Relationshipp. 445
Creating a Hierarchical View of a Tablep. 450
Finding All Child Rows for a Given Parent Rowp. 459
Determining Which Rows Are Leaf, Branch, or Root Nodesp. 462
Odds 'n' Endsp. 471
Creating Cross-Tab Reports Using SQL Server's PIVOT Operatorp. 471
Unpivoting a Cross-Tab Report Using SQL Server's UNPIVOT Operatorp. 473
Transposing a Result Set Using Oracle's MODEL Clausep. 475
Extracting Elements of a String from Unfixed Locationsp. 479
Finding the Number of Days in a Year (an Alternate Solution for Oracle)p. 481
Searching for Mixed Alphanumeric Stringsp. 483
Converting Whole Numbers to Binary Using Oraclep. 485
Pivoting a Ranked Result Setp. 488
Adding a Column Header into a Double Pivoted Result Setp. 492
Converting a Scaiar Subquery to a Composite Subquery in Oraclep. 503
Parsing Serialized Data into Rowsp. 505
Calculating Percent Relative to Totalp. 510
Creating CSV Output from Oraclep. 511
Finding Text Not Matching a Pattern (Oracle)p. 516
Transforming Data with an Inline Viewp. 519
Testing for Existence of a Value Within a Groupp. 520
Window Function Refresherp. 525
Rozenshtein Revisitedp. 551
Indexp. 587
Table of Contents provided by Ingram. All Rights Reserved.

ISBN: 9780596009762
ISBN-10: 0596009763
Series: Cookbooks (O'Reilly)
Audience: General
Format: Paperback
Language: English
Number Of Pages: 636
Published: 1st December 2005
Publisher: O'Reilly Media, Inc, USA
Country of Publication: US
Dimensions (cm): 23.3 x 17.8  x 3.2
Weight (kg): 1.02