<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rajanand ilangovan</title>
    <description>The latest articles on DEV Community by rajanand ilangovan (@rajanand).</description>
    <link>https://dev.to/rajanand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F133254%2F974346f4-9cac-4ef9-98e0-1fe01e39a1f4.jpeg</url>
      <title>DEV Community: rajanand ilangovan</title>
      <link>https://dev.to/rajanand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajanand"/>
    <language>en</language>
    <item>
      <title>What happens to the records inserted into a table variable, if the transaction rollback?</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Thu, 24 Mar 2022 20:25:41 +0000</pubDate>
      <link>https://dev.to/rajanand/what-happens-to-the-records-inserted-into-a-table-variable-if-the-transaction-rollback-i2o</link>
      <guid>https://dev.to/rajanand/what-happens-to-the-records-inserted-into-a-table-variable-if-the-transaction-rollback-i2o</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DECLARE @People TABLE (first_name VARCHAR(50), age int);
BEGIN TRAN
INSERT INTO @People VALUES ('John', 25);
INSERT INTO @People VALUES ('Daniel', 30);

ROLLBACK

SELECT * FROM @People;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Answer:
&lt;/h2&gt;

&lt;p&gt;The records inserted into a table variable will not be affected by the transaction rollback.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_BQnx9ou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649189941814/YBUdXEA2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_BQnx9ou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649189941814/YBUdXEA2w.png" alt="1118_20220406_004013.png" width="545" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://twitter.com/BrentO/status/1296117565823934464"&gt;Brent Ozar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like this SQL interview question, you may also like the below interview question and answers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/what-happens-to-the-variable-value-set-during-the-transaction-if-its-rollback"&gt;What happens to the variable value set during the transaction, if its rollback?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-generate-permutations-in-sql"&gt;How to generate permutations in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-calculate-total-travel-hours-between-city-x-and-y-and-vice-versa"&gt;How to do reciprocal in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-find-an-account-balance-in-sql"&gt;How to find account balance (running total) in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>What happens to the variable value set during the transaction, if its rollback?</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Fri, 18 Mar 2022 19:12:59 +0000</pubDate>
      <link>https://dev.to/rajanand/what-happens-to-the-variable-value-set-during-the-transaction-if-its-rollback-417f</link>
      <guid>https://dev.to/rajanand/what-happens-to-the-variable-value-set-during-the-transaction-if-its-rollback-417f</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DECLARE @my_age INT = 20;

BEGIN TRAN
SET @my_age += 30;
ROLLBACK;

SELECT @my_age;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Answer:
&lt;/h2&gt;

&lt;p&gt;Even though the transaction is rolled back, the value set to the variable remains same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_gpZ2zth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649185831120/2u9WEhnbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_gpZ2zth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1649185831120/2u9WEhnbc.png" alt="1117_20220406_003230.png" width="418" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Changes to variables aren't affected by the rollback of a transaction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Reference:&lt;br&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/variables-transact-sql"&gt;Microsoft docs&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://twitter.com/BrentO/status/1296117565823934464"&gt;Brent Ozar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like this SQL interview question, you may also like the below interview question and answers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-calculate-total-travel-hours-between-city-x-and-y-and-vice-versa"&gt;How to do reciprocal in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-generate-permutations-in-sql"&gt;How to generate permutations in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/how-to-find-an-account-balance-in-sql"&gt;How to find account balance (running total) in SQL?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.rajanand.org/what-happens-to-the-records-inserted-into-a-table-variable-if-the-transaction-rollback"&gt;What happens to the records inserted into a table variable, if the transaction rollback?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>10 tips to increase your SQL Server Management Studio productivity</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Sat, 19 Feb 2022 17:37:28 +0000</pubDate>
      <link>https://dev.to/rajanand/10-tips-to-increase-your-sql-server-management-studio-productivity-26no</link>
      <guid>https://dev.to/rajanand/10-tips-to-increase-your-sql-server-management-studio-productivity-26no</guid>
      <description>&lt;p&gt;In this post, I will show you few tips that you can use in your day-to-day SQL development workflow. If you are working with SQL Server, most likely that you will using SQL Server Management Studio on daily basic to manage and develop new SQL queries. These tips you can easily incorporate and your productivity will increase instantly.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Execute SQL query in loop
&lt;/h2&gt;

&lt;p&gt;If you want any query to be executed more than once, either you can select the query and execute (F5) it multiple times and write a while loop to execute desired number of times. You might not have come across that the batch separator "GO" can be used to execute the query &lt;code&gt;n&lt;/code&gt; number of times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 'execute 3 times' 
GO 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trick will be extremely helpful if you want to insert some dummy data to a table for testing purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--Execute SQL command in loop with "GO"
use demo;
create table marks (
    student_id int identity(1,1), 
    marks tinyint, 
    create_datetime datetime2
)
GO

insert into marks 
select cast(rand()*100 as int), getdate()
GO 1000 -- 1000 records will be inserted

select * from marks
drop table marks

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176687893%2FAt77qv_6F.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176687893%2FAt77qv_6F.gif" alt="ssms-execute-query-in-loop-go-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Copy and paste multiple values at once.
&lt;/h2&gt;

&lt;p&gt;Ctrl+C - Copy multiple values. Hold down Ctrl+Shift and then press V few times to cycle through copied values from clipboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643350171319%2FWk5JNVOV4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643350171319%2FWk5JNVOV4.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176761684%2FY_omcMUNz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176761684%2FY_omcMUNz.gif" alt="ssms-copy-and-paste-multiple-values-at-once-clipboard-ring-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Write down column names instead of 'select *'
&lt;/h2&gt;

&lt;p&gt;If you have working with SQL for some time, you may know that selecting all the columns in select clause when all the columns are not necessary. But it is easy to just type * (asterisk) to select all columns than type out all the column names. This will be even time consuming if the number of columns in a table is quite large.&lt;/p&gt;

&lt;p&gt;An easy way to do that is just drag and drop from object explorer. You have to expand the table name and drag the columns folder folder to the Query editor window. By default, the columns will be surrounded by square brackets when dragged from object explorer. If you prefer without square brackets, you can change it in &lt;code&gt;Tools&lt;/code&gt; =&amp;gt; &lt;code&gt;Options&lt;/code&gt; =&amp;gt; &lt;code&gt;SQL Server Object Explorer&lt;/code&gt; =&amp;gt; &lt;code&gt;Surround object names with brackets when dragged&lt;/code&gt; to false.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176866393%2FzazASM7C8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176866393%2FzazASM7C8.gif" alt="ssms-drag-and-drop-column-names-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. CTRL+3 is your friend
&lt;/h2&gt;

&lt;p&gt;You can configure frequent used queries in shortcuts and use that to execute. &lt;code&gt;Tools&lt;/code&gt; =&amp;gt; &lt;code&gt;Options&lt;/code&gt; =&amp;gt; &lt;code&gt;Environment&lt;/code&gt; =&amp;gt; &lt;code&gt;Keyboard&lt;/code&gt; =&amp;gt; &lt;code&gt;Query Shortcuts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I like to configure these 3 query shortcuts whenever I install a new SQL Server Management Studio in new computer.&lt;br&gt;&lt;br&gt;
Ctrl+3 =&amp;gt; &lt;code&gt;SELECT * FROM&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Ctrl+4 =&amp;gt; &lt;code&gt;SELECT Top(5) * FROM&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Ctrl+5 =&amp;gt; &lt;code&gt;SELECT COUNT(1) FROM&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Note that there is a space after &lt;code&gt;FROM&lt;/code&gt; clause above.&lt;/p&gt;

&lt;p&gt;You can configure other shortcuts also. Once you configure the above three shortcuts, you can just select any valid tables and then press the shortcuts to see the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176802605%2Fhi-ZwKRfd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176802605%2Fhi-ZwKRfd.gif" alt="ssms-ctrl+3-shortcut-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Change column names in single-line to multi-line and vice versa
&lt;/h2&gt;

&lt;p&gt;If you have all the column name listed as a comma separated in a single line and you want each column names in a separate line, you can do that with a simple 'Find and Replace'Ctrl+H - To open find and replace window. Type comma , in find field and ,\r in replace field. Then Alt+E to enable regular expression and then Alt+A to replace all.&lt;/p&gt;

&lt;p&gt;Similarly, you can change from multi-line to single-line by just swapping the values in Find and replace. Find - ,\rReplace - ,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176913518%2FPDIeWTM-C.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176913518%2FPDIeWTM-C.gif" alt="ssms-column-names-in-separate-line-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Modify multiple lines at once.
&lt;/h2&gt;

&lt;p&gt;Hold down Shift+Alt keys and then click in query editor for start region and then the click the end region to select. Now if you check based on how many lines (region) that you have selected that many cursor will be blinking and you can just start typing it. This will be especially useful if you are just copy and pasting column name from result pane and you want to add comma , at the start (or end) of the column name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176921498%2F6mjWIJqsS.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643176921498%2F6mjWIJqsS.gif" alt="ssms-modify-multiple-lines-at-once-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Query editor in full screen.
&lt;/h2&gt;

&lt;p&gt;If you using a computer with small screen resolution size, you SQL Server management studio's various windows might cluster query editor space and you may not have enough space. If you switch to full screen mode, you can have a distraction free query query editor. to achieve this, either you can close all the other windows or you can just make the query editor window as full screen using the shortcut Shift+Alt+Enter. This way all other windows will remain there when you exit the full screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643178429370%2FxxGap3tIG.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643178429370%2FxxGap3tIG.gif" alt="ssms-query-editor-in-full-screen-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Never execute a query in production database inadvertently
&lt;/h2&gt;

&lt;p&gt;If you have worked with databases for quite sometime, either you would have run some query in production that you didn't wanted to run or someone from your team would have done that. Dropping a database or a table or you would have updated/deleted all the records of the table. Because you forget the where clause.&lt;/p&gt;

&lt;p&gt;You should pay extra attention when you execute any query in production server. In this trick, you will configure the status bar colour for to something different (eye catching) for production servers. So that when you connect to the server, you get a visual cue that you are connected to production server. It's not guaranteed that you will never execute that something in production server inadvertently. There is nothing will stop you from doing that. It's just a visual cue for you to know that you are working in production server.&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;Change connection&lt;/code&gt; =&amp;gt; &lt;code&gt;Options&lt;/code&gt; =&amp;gt; &lt;code&gt;Connection Properties&lt;/code&gt; =&amp;gt; &lt;code&gt;Use custom color&lt;/code&gt; to set different colour.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643178992239%2FBuWCDdFoB.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643178992239%2FBuWCDdFoB.gif" alt="ssms-change-status-bar-colour-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Don't google for even a simple SQL syntax
&lt;/h2&gt;

&lt;p&gt;Sometimes, you don't remember even a simple SQL syntax and you want to google for it to get the syntax and then copy paste and modify according to your need. Instead of that you can just use Snippets ( Ctrl+K and Ctrl+X ) You can add your own snippets also. That's for some other day.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643180255033%2FrWG_oDTBo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643180255033%2FrWG_oDTBo.gif" alt="ssms-code-snippets-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Surround your code with IF condition or While loop.
&lt;/h2&gt;

&lt;p&gt;This tip also similar to the previous one. If you have a block of code that you want to surround with a &lt;code&gt;If&lt;/code&gt; , &lt;code&gt;While&lt;/code&gt; or &lt;code&gt;Begin..End&lt;/code&gt;, you can use Surround with using the short cut ( Ctrl+K and Ctrl+S ) or you can simple select the statements and &lt;code&gt;right&lt;/code&gt; click and select &lt;code&gt;Surround with&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643182097261%2FjVnTIuo4A.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1643182097261%2FjVnTIuo4A.gif" alt="ssms-surround-with-rajanand.org.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thanks for reading this post till the end. I hope these tips and tricks help you to improve your SQL development productivity as it does for me. If you have any tricks that you use are not listed in this post or any other questions, let me know in comments section below.&lt;/p&gt;

&lt;p&gt;This article was originally published in &lt;a href="https://blog.rajanand.org/10-tips-to-increase-your-sql-server-management-studio-productivity" rel="noopener noreferrer"&gt;blog.rajanand.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you would like to receive these tips and tricks directly to your inbox, consider subscribe my &lt;a href="http://newsletter.rajanand.org" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to generate Permutations in SQL?</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Sun, 13 Feb 2022 06:35:43 +0000</pubDate>
      <link>https://dev.to/rajanand/how-to-generate-permutations-in-sql-332l</link>
      <guid>https://dev.to/rajanand/how-to-generate-permutations-in-sql-332l</guid>
      <description>&lt;h2&gt;
  
  
  Question:
&lt;/h2&gt;

&lt;p&gt;You are planning to go for a summer vacation and finalized the cities you want to visit. But you have not finalized in which order you want to visit them yet.&lt;/p&gt;

&lt;p&gt;Write a SQL query to list out all different possible order you can visit these cities.&lt;/p&gt;

&lt;p&gt;Note that you don't want to visit the same city again and you don't want to skip any city in your travel plan either.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644727361179%2FR1v_xwyGG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644727361179%2FR1v_xwyGG.png" alt="03-permutations-table.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Expected output: &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644727369565%2FrokHTbXtb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644727369565%2FrokHTbXtb.png" alt="03-permutations-output.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;Let us create the test data first for the demo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- test data
use demo;
if object_id (N'dbo.city', N'u') is not null 
drop table dbo.city
go

create table dbo.city (
    id int identity(1,1),
    city_name varchar(100)
)

insert into dbo.city (city_name) values 
  ('Oslo'),
  ('Helsinki'),
  ('Stockholm'),
  ('Copenhagen')

select id, city_name from dbo.city

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution is implemented using the Recursive CTE in SQL Server. If you are using other database engines, you can implement it in similar way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- solution
declare @total_cities int = (select count(1) from dbo.city);
with travel (travel_path, level) as (
    select cast(city_name as varchar(200)), 
    level = 1 
    from dbo.city
    union all
    select cast(travel.travel_path + ' -&amp;gt; ' + city.city_name as varchar(200)), 
    level = level + 1
    from dbo.city
    inner join travel on level &amp;lt; @total_cities
    where charindex(city.city_name, travel.travel_path) = 0
    )

select 
id = row_number() over(order by travel_path),
travel_path
from travel
where level = @total_cities
order by id

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you like this interview question, you may also like this interview question and answer &lt;a href="https://blog.rajanand.org/series/sql-interview-questions" rel="noopener noreferrer"&gt;series&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>sqlserver</category>
      <category>interview</category>
    </item>
    <item>
      <title>How to find an account balance in SQL?</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Tue, 01 Feb 2022 19:39:07 +0000</pubDate>
      <link>https://dev.to/rajanand/how-to-find-an-account-balance-in-sql-lm6</link>
      <guid>https://dev.to/rajanand/how-to-find-an-account-balance-in-sql-lm6</guid>
      <description>&lt;h2&gt;
  
  
  Question:
&lt;/h2&gt;

&lt;p&gt;You have a &lt;code&gt;transaction_detail&lt;/code&gt; table with a record for each transaction. The transaction type could be debit, credit or refund. You need to add an additional column to track account balance.&lt;/p&gt;

&lt;p&gt;Write a SQL query to achieve the expected output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--009dOfcm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643741735004/70s8dDQ_K.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--009dOfcm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643741735004/70s8dDQ_K.png" alt="image.png" width="880" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer:
&lt;/h2&gt;

&lt;p&gt;Let us create the test data first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- create test data
USE demo;
GO
CREATE TABLE dbo.transaction_detail (
    date date,
    transaction_type varchar(50),
    amount money
    )
GO

INSERT INTO dbo.transaction_detail VALUES
('2022-01-01','credit',1000),
('2022-01-16','debit',300),
('2022-01-31','credit',150),
('2022-02-04','debit',50),
('2022-02-13','refund',50),
('2022-02-28','credit',400),
('2022-03-21','debit',500)
GO

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the desired result, you can use aggregate function &lt;code&gt;sum&lt;/code&gt; with window function to calculate the &lt;strong&gt;running total&lt;/strong&gt; for each row in a window. But, we have not defined the window (i.e, &lt;code&gt;partition by&lt;/code&gt; is not defined to define the window) here. Had we have to calculate the account balance for each account, we would have defined the window as account. So this query just calculates the running total for all the records in the table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- solution
SELECT 
date,
transaction_type,
amount,
balance = SUM(CASE WHEN transaction_type = 'debit' then amount * -1 
              else amount end) 
          OVER(ORDER BY date)
FROM dbo.transaction_detail

-- clean up
-- USE demo;
-- DROP TABLE dbo.transaction_detail

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the solution I have come up with. If you have achieved the output in any other way, let me know in comments. Thank you.&lt;/p&gt;

&lt;p&gt;If you would like to receive these interview questions directly to your inbox, sign up for my &lt;a href="//newsletter.rajanand.org"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>sqlserver</category>
      <category>tsql</category>
    </item>
    <item>
      <title>How to calculate total travel hours between city x and y and vice versa</title>
      <dc:creator>rajanand ilangovan</dc:creator>
      <pubDate>Tue, 01 Feb 2022 07:41:25 +0000</pubDate>
      <link>https://dev.to/rajanand/how-to-calculate-total-travel-hours-between-city-x-and-y-and-vice-versa-1bc6</link>
      <guid>https://dev.to/rajanand/how-to-calculate-total-travel-hours-between-city-x-and-y-and-vice-versa-1bc6</guid>
      <description>&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;You are working for an airline company and you have received a dataset of Pilot's travel time between cities. You need to determine the reciprocal and find the total travel hours between cities. That means, the pilot's travel from city '1' to '2' and city '2' to '1' should be considered as one and then the total travel hours has to be calculated.&lt;/p&gt;

&lt;p&gt;Write an SQL query to achieve the expected output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FLSyxpia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643696437905/pppVnfP9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FLSyxpia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643696437905/pppVnfP9w.png" alt="04-reciprocal-input-data-output.png" width="880" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;

&lt;p&gt;Let us create the test data first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--create test data
USE demo;

CREATE TABLE dbo.travel_detail (
    id int identity(1,1),
    from_city varchar(100),
    to_city varchar(100),
    travel_time_hours smallint
)
GO

INSERT INTO dbo.travel_detail values
    ('Oslo','Helsinki',125),
    ('Helsinki','Oslo',110),
    ('Stockholm','Oslo',132),
    ('Oslo','Stockholm',180),
    ('Copenhagen','Helsinki',148),
    ('Helsinki','Copenhagen',84),
    ('Stockholm','Copenhagen',116),
    ('Helsinki','Stockholm',124)
GO

SELECT * FROM dbo.travel_detail

--clean up
--USE demo;
--DROP TABLE dbo.travel_detail

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To achieve the desired result, you should swap the city names within the row based on some order. That means, you can sort the from_city and to_city names in a row in ascending or descending order and then keep the first city as &lt;code&gt;city_1&lt;/code&gt; and the other as city_2. To do this, you can use less than  &amp;lt;  or greater than &amp;gt; operator on &lt;code&gt;from_city&lt;/code&gt; and &lt;code&gt;to_city&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J07e33_Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643698330299/7HQnHHdEK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J07e33_Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1643698330299/7HQnHHdEK.png" alt="04-reciprocal-step-1.png" width="712" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way you will be able to aggregate the &lt;code&gt;travel_time_hours&lt;/code&gt; by grouping &lt;code&gt;city_1&lt;/code&gt; and &lt;code&gt;city_2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--solution
USE demo;

select 
city_1,
city_2,
total_travel_hours = sum(travel_time_hours)
from (
    select 
    city_1 = case when from_city &amp;lt; to_city then from_city else to_city end,
    city_2 = case when from_city &amp;gt; to_city then from_city else to_city end,
    travel_time_hours
    from dbo.travel_detail
)x
group by city_1, city_2
order by city_1, city_2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the solution I have come up with. If you have achieved the output in other way, let me know in comments.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://newsletter.rajanand.org/"&gt;Sign up&lt;/a&gt; to receive these interview questions directly in your inbox.&lt;/p&gt;

</description>
      <category>sqlserver</category>
      <category>interview</category>
      <category>questions</category>
    </item>
  </channel>
</rss>
