Posts

Showing posts from January, 2023

How to get the n-th row in a SQL table ..in 4 different ways! Simple, clear and with example!

Image
Hi guys, After the deep previous post about the SQL statistic today we will talk about a more more easy topic. Have you ever needed to extract from a table the second row, or the third or the n-th row? This post is for you!    Extract ..Get..Select... the n-th row in a table We can solve this task in more than one way. To follow my example you can simply execute the following command that create and fill the Ord table with some data Create Table Ord (Id int identity (1,1) primary key , Code varchar (10)) Insert into Ord (Code) values ( 'A' ),( 'B' ),( 'C' ),( 'D' ),( 'E' ),( 'F' ),( 'G' ),( 'H' ),( 'I' ) This is our table:   The first way I seen many times is using a derivate table .   Yes, you can solte the request by split the problem in two parts. In the first part you can get the first n-th rows. This will be the derivate table.   Then you can get first row of this derivate table orde...

A proposal for a new way to manage SQL Statistics for data not evenly distributed. A dip in the logic of SQL Statistics!

Image
Hi guys, I love database theory because it includes mathematics, statistics, logic, programming and data organization .  I wrote this post because i read a post on linkedin about the well known  " 201 bucket limitation " in the statistics used in SQL Server. Bob Ward (the Principal Architect at Microsoft, in the photo on the right while dreaming statistics and rugby...lol ) rensponded to the author of the linkedin post asking him to give a real example where this was a limitation. He also said he would verify. I then asked myself whether the logic with which statistics are managed could not be improved in any way without increasing their size.   In the post on Linkedin was also referred to a post of the well-known mr. brent Ozar : The 201 Buckets Problem, Part 1: Why You Still Don’t Get Accurate Estimates Brent used the Stack Overflow 2010 database database for his examples and I will do the same in this post. If you want to follow my examples then, first of all, down...