What is meant by set based programming?

  • Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

  • Set based languages are declarative. You tell sql server WHAT you want not how to do it.

    "I want the set of customers who ordered more than $100 last month unioned with the top 200 customers from last year by total number of orders"

    In a procedural language, you tell it HOW to calculate the results. So, you would need loop through all the orders for last month, summing them up , then you'd need to count the total orders for every customer last year, sort them, keep the top 200, and return the unique customers.

    The key to becoming proficient in sql is being able to express the "WHAT" while letting Sql Server worry about the "HOW".

    Also, pure set based logic doesn't use loops. So, if you find yourself wanting to loop over rows, you are not taking a set based approach.

  • To quote Jeff Moden's signature:

    First step towards the paradigm shift of writing Set Based code:

    Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

    Wiser words have not been spoken, except possibly (:-)) for my mantra:


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Not that I don't admire Jeff's abilities, but he doesn't speak the language sometimes... or better stated, he speaks the language of database.

    From the perpsective of every coder, here's the difference.

    Instead of a For Each loop on each component of a collection, you attempt to perform an operation against the collection directly. So instead of a For Each row In (select) do y, you do Collection.DoY (value). It's not a direct comparison but it gets the idea across.

    Here's why, though: In your average programming language, you're expected to do 90% of the work. In a database engine, 90% of the work is done for you if everything is setup correctly. From there, properly defined and organized, all you need to do is tell it to do what you want in a single attempt to everything you want it to work against. The complications come in during setup, which is why the majority of us are here to attempt to help. 🙂


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • What I've found most useful when designing set-based vs row-based coding, is a few tips:

    1. Assume rows don't have an order to them. So you can't do "row 1, then row 2, then row 3". Do rows 1-3 all at once.

    2. Treat sets of rows as a single object, and call methods against them, instead of doing something procedural to individual rows. Methods include calculations, aggregations, CRUD, and so on. Think of the From clause in a query as defining the object, and the other clauses (Select, Update, Where, Order By, etc.) as methods against that set-of-rows object.

    3. The concept of working against columns instead of rows also helps.

    There's more to it, but that's a good start.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Shadab Shah (8/9/2012)


    Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

    Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 90% of the work is done for you

    How do I love thee SQL? Let me count(theWays)..

    [font="Courier New"]Looking for a Deadlock Victim Support Group..[/font]
  • Think of it this way , if u walk into a room and ask each person if they are above 20 years of age, ur are performing something like RBAR or row by row operation. on the other hand if you shouted "everybody over 20 please raise your hand" you dealing with a set. The idea here is to ignore the uniqueness for each row and try to identify what they have in common and use the commonality to programmer better.

    Jayanth Kurup[/url]

  • Possinator (8/10/2012)


    90% of the work is done for you

    How do I love thee SQL? Let me count(theWays)..

    Ditto that!

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jayanth_Kurup (8/10/2012)


    Think of it this way , if u walk into a room and ask each person if they are above 20 years of age, ur are performing something like RBAR or row by row operation. on the other hand if you shouted "everybody over 20 please raise your hand" you dealing with a set. The idea here is to ignore the uniqueness for each row and try to identify what they have in common and use the commonality to programmer better.

    I've seen a lot of "word" examples but I really like this one especially for it's simplicity and it's going into my cerebral brief case.. Thanks for posting it.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (8/10/2012)


    Shadab Shah (8/9/2012)


    Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

    Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.

    Hi,

    Let me consider a example over here as

    Select subject1 + subject2 + subject3 As TotalMarks from Student where class= 'ComputerScience'

    As per my understanding this operation would be perform row by row. The first row with the class ComputerScience is selected and then the other and then it goes on. so does that means that the above SQL Statement does not represent what we called as set based operation.

    I think after you answer this question my understanding for set based programming would be more clear.Thanks

  • Shadab Shah (8/12/2012)


    Jeff Moden (8/10/2012)


    Shadab Shah (8/9/2012)


    Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

    Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.

    Hi,

    Let me consider a example over here as

    Select subject1 + subject2 + subject3 As TotalMarks from Student where class= 'ComputerScience'

    As per my understanding this operation would be perform row by row. The first row with the class ComputerScience is selected and then the other and then it goes on. so does that means that the above SQL Statement does not represent what we called as set based operation.

    I think after you answer this question my understanding for set based programming would be more clear.Thanks

    No. That particular example has nothing to do with whether something is set based or not. That particular example is just bad database design. There should not be multiple columns for "subject". There should only be 1.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Evil Kraig F (8/10/2012)


    Not that I don't admire Jeff's abilities, but he doesn't speak the language sometimes... or better stated, he speaks the language of database.

    I respectfully submit that even someone who knows nothing of databases can relate to the simplicty of rows vs columns. 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Shadab Shah (8/12/2012)


    Jeff Moden (8/10/2012)


    Shadab Shah (8/9/2012)


    Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

    Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.

    Hi,

    Let me consider a example over here as

    Select subject1 + subject2 + subject3 As TotalMarks from Student where class= 'ComputerScience'

    As per my understanding this operation would be perform row by row. The first row with the class ComputerScience is selected and then the other and then it goes on. so does that means that the above SQL Statement does not represent what we called as set based operation.

    I think after you answer this question my understanding for set based programming would be more clear.Thanks

    This is set-based. The set in this example is all rows in student which have 'ComputerScience' in their class column. The addition operation is performed for all rows in the set without any need to treat the rows one by one.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Shadab Shah (8/12/2012)


    Jeff Moden (8/10/2012)


    Shadab Shah (8/9/2012)


    Hi all,

    I have come across various section on this website where it is mention that SQL Server is a set based programming language.

    My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.

    I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.

    Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.

    Hi,

    Let me consider a example over here as

    Select subject1 + subject2 + subject3 As TotalMarks from Student where class= 'ComputerScience'

    As per my understanding this operation would be perform row by row. The first row with the class ComputerScience is selected and then the other and then it goes on. so does that means that the above SQL Statement does not represent what we called as set based operation.

    I think after you answer this question my understanding for set based programming would be more clear.Thanks

    That is set-based. As mentioned, it's probably a poor database normalization pattern, having subject1, subject2, et al, as columns in a table, but that's a different subject.

    A non-set-based method of doing that query would be to run a cursor over the table, and when it finds a row where class = 'ComputerScience', it adds the three subjects togethr, and then returns that value, possibly into some sort of array for the front-end application to work on.

    Behind the scenes, actually, it isn't even row-by-row at the mechanical level. It's done by reading pages of data into RAM and processing those byte-by-byte through a sequential engine in the CPU and the various buses on the motherboards, etc. But that's not what set-based vs procedural means. It means you've told the SQL engine what to do with a set of rows, not with one row at a time.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 15 posts - 1 through 15 (of 34 total)

You must be logged in to reply to this topic. Login to reply