<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sathish Vadivel on Medium]]></title>
        <description><![CDATA[Stories by Sathish Vadivel on Medium]]></description>
        <link>https://medium.com/@SathishVadivel?source=rss-0ad525dd1e52------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3qOQjvJHX6c-9gAlvUxN-w.png</url>
            <title>Stories by Sathish Vadivel on Medium</title>
            <link>https://medium.com/@SathishVadivel?source=rss-0ad525dd1e52------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:16:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@SathishVadivel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[KDB+/q - Optimization]]></title>
            <link>https://medium.com/@SathishVadivel/kdb-q-optimization-8e38cb16abcb?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e38cb16abcb</guid>
            <category><![CDATA[sathish]]></category>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[kdb]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Sat, 24 Oct 2026 13:37:29 GMT</pubDate>
            <atom:updated>2026-10-24T13:37:29.743Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vsNCpHxB725HJy1m8K21tg.png" /></figure><p>In KDB+/q, there is often more than one way to express the same operation. However, equivalent results do not always come with equivalent performance.</p><p>Let’s explore a small example that compares three ways to calculate the maximum of a scalar value and each element of a column.</p><h3>1. Setting Up the Example</h3><p>First, let’s create a simple table:</p><pre>q)table:([]name:`sam`ram`eno`sai`choi;KM:15 17 19 21 23)<br><br>/The table looks like this:<br>q)table<br>name  KM<br>---------<br>sam   15<br>ram   17<br>eno   19<br>sai   21<br>choi  23</pre><p>Now, define a scalar value:</p><pre>q)findValue:18</pre><p>Our goal is to create a new column, newValue, containing the maximum of findValue and each value in the KM column.</p><pre>In other words:<br><br>max(18;15) = 18<br>max(18;17) = 18<br>max(18;19) = 19<br>max(18;21) = 21<br>max(18;23) = 23</pre><p>The expected output is:</p><pre>name  KM  newValue<br>------------------<br>sam   15  18<br>ram   17  18<br>eno   19  19<br>sai   21  21<br>choi  23  23</pre><h3>2. Different Ways to Write the Same Logic</h3><h4>Approach 1: Using each + max</h4><pre>q)update newValue:{max(findValue;x)} each KM from table<br>name KM newValue<br>----------------<br>sam  15 18<br>ram  17 18<br>eno  19 19<br>sai  21 21<br>choi 23 23</pre><h4>Approach 2: Using Vector Conditional</h4><pre>q)update newValue:?[KM&gt;findValue;KM;findValue] from table<br>name KM newValue<br>----------------<br>sam  15 18<br>ram  17 18<br>eno  19 19<br>sai  21 21<br>choi 23 23</pre><h4>Approach 3: Using max Directly</h4><pre>q)update newValue:max(findValue;KM) from table<br>name KM newValue<br>----------------<br>sam  15 18<br>ram  17 18<br>eno  19 19<br>sai  21 21<br>choi 23 23</pre><h4>Approach 4: Using the Primitive |</h4><pre>q)update newValue:|[findValue;KM] from table<br>name KM newValue<br>----------------<br>sam  15 18<br>ram  17 18<br>eno  19 19<br>sai  21 21<br>choi 23 23</pre><h3>3. Benchmarking the Four Approaches</h3><p>Let’s compare their execution times using q’s \t timing command:</p><pre>/Using each<br>q)\t:1000000 update newValue:{max(findValue;x)} each KM from table<br>2348<br><br>/Using Vector conditional<br>q)\t:1000000 update newValue:?[KM&gt;findValue;KM;findValue] from table<br>1465<br><br>/Using max Directly<br>q)\t:1000000 update newValue:max(findValue;KM) from table<br>1283<br><br>/Using the Primitive |<br>q)\t:1000000 update newValue:|[findValue;KM] from table<br>954</pre><p>The timings are in milliseconds for the requested repetitions.</p><h4>Benchmark Results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*dCyXEe3dSGENPFchuo4leA.png" /></figure><p>Based on these benchmark results, the primitive | delivers the best performance among the three approaches tested.</p><p>These results are specific to this benchmark and environment. They should not be treated as universal performance guarantees.</p><h3>4. What Can We Learn from This?</h3><p>A few practical takeaways:</p><ul><li><strong>Avoid unnecessary iteration when a vector operation expresses the same logic.</strong> In q, a direct vector expression is often worth testing against an explicit each.</li><li><strong>Know your primitives.</strong> Operators such as | can express common operations directly and concisely.</li><li><strong>Benchmark equivalent expressions.</strong> A shorter expression is not automatically faster, so measure it in your actual workload.</li><li><strong>Test realistic data sizes.</strong> A five-row table is useful for demonstrating correctness, but production-scale data is necessary to understand real performance.</li></ul><h3>Conclusion</h3><p>KDB+/q gives us multiple ways to express the same calculation, but choosing the right expression can make a significant difference in execution performance. While the difference may seem small in simple examples, it can become much more noticeable when handling large volumes of data.</p><p>Optimizing expressions by leveraging built-in primitives and avoiding unnecessary iteration can help improve efficiency in data-intensive workloads.</p><h4><strong>“When working with large datasets, even small optimizations can lead to significant performance gains.”</strong></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e38cb16abcb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Advantage of q Functional Queries in kdb+]]></title>
            <link>https://medium.com/@SathishVadivel/advantage-of-q-functional-queries-in-kdb-7b6d5d2243cf?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b6d5d2243cf</guid>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[kdb]]></category>
            <category><![CDATA[sathish]]></category>
            <category><![CDATA[functional-query]]></category>
            <category><![CDATA[q]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Sat, 26 Sep 2026 17:36:48 GMT</pubDate>
            <atom:updated>2026-09-26T17:36:48.221Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PCKfDekSj9u2uNN-BBWh-Q.jpeg" /></figure><p>One of the powerful features of <strong>kdb+/q</strong> is its ability to express the same operation in different ways. A good example is selecting rows from a table using the functional form of ? versus using #.</p><p>Let’s start with a simple table:</p><pre>q)table:([]name:`sam`ram`eno`sai`choi;age:11 12 13 14 15)<br><br>q)?[table;();0b;()]<br>name age<br>--------<br>sam  11<br>ram  12<br>eno  13<br>sai  14<br>choi 15</pre><h3><strong>Functional Query Form</strong></h3><p>The functional form of select is:</p><pre>?[table;where;by;select]</pre><p>For example:</p><pre>q)?[table;();0b;()]<br>name age<br>--------<br>sam  11<br>ram  12<br>eno  13<br>sai  14<br>choi 15</pre><p>This gives us the complete table.</p><p>Now notice what happens when we provide the optional n argument:</p><pre>q)?[table;();0b;();0]<br>name age<br>--------</pre><p>With n=0, no rows are returned.</p><p>With n=3:</p><pre>q)?[table;();0b;();3]<br>name age<br>--------<br>sam  11<br>ram  12<br>eno  13</pre><p>And with n=7:</p><pre>q)?[table;();0b;();7]<br>name age<br>--------<br>sam  11<br>ram  12<br>eno  13<br>sai  14<br>choi 15</pre><p>Even though we requested 7 rows, the result contains only the 5 rows available in the table.</p><h3>Now Compare This with #</h3><p>Consider:</p><pre>q)0#select from table<br>name age<br>--------</pre><p>So far, the behavior looks similar.</p><p>But now:</p><pre>q)7#select from table<br>name age<br>--------<br>sam  11<br>ram  12<br>eno  13<br>sai  14<br>choi 15<br>sam  11<br>ram  12</pre><p>Here we get <strong>7 rows</strong>, even though the original table contains only 5 rows.</p><h3><strong>Why?</strong></h3><p>Because # is the <strong>take operator</strong>. When the requested count is greater than the number of available items, q takes items cyclically.</p><p>So:</p><p>5 rows available</p><p>7 requested</p><pre>sam → ram → eno → sai → choi → sam → ram</pre><p>This produces 7 rows.</p><h3>Why Is the Functional Query Useful?</h3><p>This difference becomes important when writing database queries.</p><p>The functional query form:</p><pre>?[table;();0b;();7]</pre><p>is operating as part of q’s query language. It gives us a way to construct and manipulate queries programmatically.</p><p><strong>A Simple Comparison:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDHyY7hK_yFFhb4H5o-MFQ.png" /></figure><p>Understanding this distinction helps avoid subtle bugs and, more importantly, opens the door to one of kdb+/q’s biggest strengths: <strong>building powerful queries dynamically using q’s functional form.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b6d5d2243cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Error Handling in KDB+/q: Understanding Trap and Trap At]]></title>
            <link>https://medium.com/@SathishVadivel/error-handling-in-kdb-q-understanding-trap-and-trap-at-9750ab5bd2e5?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/9750ab5bd2e5</guid>
            <category><![CDATA[trap]]></category>
            <category><![CDATA[sathish]]></category>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[trap-at]]></category>
            <category><![CDATA[kdb]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Sat, 29 Aug 2026 16:37:43 GMT</pubDate>
            <atom:updated>2026-08-29T16:37:43.913Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BpXBOCWNYBgh_HSZLOM5MQ.png" /></figure><p>Error handling is an important part of writing reliable KDB+/q applications.</p><p>If you’re coming from languages such as Python, Java, or JavaScript, you might expect something like:</p><pre>try:<br>    ...<br>except:<br>    ...</pre><p>but, q takes a different approach.</p><p>In q, Trap and Trap At provide protected evaluation. They allow us to execute a function and decide what should happen if that function signals an error.</p><p>The two forms are:</p><pre>.[f;fx;e]      / Trap<br>@[f;fx;e]      / Trap At</pre><p>Although they look similar, understanding the difference between them — and especially understanding what is actually protected from errors — is important.</p><p>Let’s explore them using a simple example.</p><p>First, Let’s create a sample table:</p><pre>q)table:([]name:`sam`ram`eno`naruto`sakura`sasuke;age:11 12 13 14 15 16;place:6?`8)<br>q)table<br>name   age place<br>-------------------<br>sam    11  aeajbddp<br>ram    12  blmjhgah<br>eno    13  ooeiogdj<br>naruto 14  jgjmgihj<br>sakura 15  cflmacjn<br>sasuke 16  bpmcjjdg</pre><p>Calling the function normally works:</p><pre>q)fun:{y#x};<br><br>/This function takes two arguments:<br>x → the input object<br>y → the number of items to take<br><br>q)fun[table;3]<br>name age place<br>-----------------<br>sam  11  aeajbddp<br>ram  12  blmjhgah<br>eno  13  ooeiogdj<br><br></pre><p>But what happens if we provide an invalid value?</p><pre>q)fun[table;`a]<br>&#39;type<br>  [1]  fun:{y#x}</pre><p>The function signals a type error.</p><p>Instead of allowing that error to propagate, we can use Trap.</p><h3>Trap</h3><p>The general form of Trap is: <strong>.[f;fx;e]</strong></p><p>Here:</p><ul><li>f is the function to execute.</li><li>fx is the argument list.</li><li>e is the expression to evaluate if the function fails.</li></ul><p>You can think of it conceptually as:</p><pre>execute f with fx<br>       |<br>       +---- success ----&gt; return result<br>       |<br>       +---- error ------&gt; evaluate e</pre><p>This is similar in purpose to try/catch, although the evaluation rules in q are different.</p><p><strong>A Simple Trap Example :</strong></p><pre>q).[fun;(table;`a);`ouch]<br>`ouch</pre><p>the error is caught and the expression `ouch is returned.</p><p>So instead of &#39;type, we get `ouch - this gives the calling code a controlled result.</p><p><strong>The Error Handler Can Be a Function :</strong></p><p>The third argument doesn’t have to be a simple value. If the error expression is a function, q evaluates that function using the text of the signalled error.</p><pre>q).[fun;(table;`a);{x,&quot; error occurred&quot;}]<br>&quot;type error occurred&quot;</pre><p>Instead of simply returning a fixed value, the handler can inspect the error and construct an appropriate response.</p><h3>What Happens When There Is No Error?</h3><p>Trap doesn’t change the result of a successful operation.</p><p>For example:</p><pre>q).[fun;(table;3);{x,&quot; error occurred&quot;}]<br>name age place<br>-----------------<br>sam  11  aeajbddp<br>ram  12  blmjhgah<br>eno  13  ooeiogdj</pre><p>The function succeeds, so the error handler is never evaluated.</p><p>The result is exactly the same as:</p><pre>q)fun[table;3]<br>name age place<br>-----------------<br>sam  11  aeajbddp<br>ram  12  blmjhgah<br>eno  13  ooeiogdj</pre><p>This gives us a useful rule:</p><blockquote>If the protected function succeeds, Trap returns the normal result. If it fails, the trap expression determines the result.</blockquote><h3>Trap At</h3><p>Now let’s look at Trap At.</p><p>The syntax is: <strong>@[f;fx;e]</strong></p><p>Trap At is essentially a simpler form of Trap for unary argument values.</p><h3>Why Do We Need Trap At?</h3><p>Suppose we have a function that takes a single argument.</p><pre>q)fun:{x#table};<br><br>/This function takes one argument:<br>x → the number of items to take</pre><p><strong>A Simple Trap At Example :</strong></p><pre>q)@[fun;`a;`ouch]<br>`ouch</pre><p>the error is caught and the expression `ouch is returned.</p><p><strong>Trap At works in much the same way as Trap.</strong></p><pre>q)@[fun;5;{x,&quot; error occurred&quot;}]<br>name   age place<br>-------------------<br>sam    11  aeajbddp<br>ram    12  blmjhgah<br>eno    13  ooeiogdj<br>naruto 14  jgjmgihj<br>sakura 15  cflmacjn</pre><p>The function succeeds, so the error handler is never evaluated.</p><h3>Trap vs Trap At</h3><p>The relationship can be summarized as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYVvzYBV7e7LCDYLM1ZEpw.png" /></figure><p>Trap and Trap At provide a simple yet powerful way to handle runtime errors in KDB+/q without interrupting the entire execution flow. Understanding when errors are caught — and how the error handler is evaluated — helps us write more reliable and predictable q code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9750ab5bd2e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Boolean Filters in kdb+]]></title>
            <link>https://medium.com/@SathishVadivel/understanding-boolean-filters-in-kdb-228f4795e986?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/228f4795e986</guid>
            <category><![CDATA[kdb]]></category>
            <category><![CDATA[filters]]></category>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[sathish]]></category>
            <category><![CDATA[kdb-table]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Sun, 21 Jun 2026 09:45:47 GMT</pubDate>
            <atom:updated>2026-06-21T09:45:47.800Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l4L8cCo94LaF2U3XW_Vwkg.png" /></figure><p>One of the most elegant features of kdb+ is how naturally it handles boolean filtering. If a column contains boolean values (1b for true and 0b for false), you can use that column directly in a where clause without writing an explicit comparison.</p><p><strong>Creating a Sample Table:</strong></p><pre>q)table:([] <br>    sno:1 2 3 4 5 6 7;<br>    name:`sam`ram`eno`choi`lee`kali`sai;<br>    available:1100110b)<br><br>q)table<br>sno name available<br>------------------<br>1   sam  1<br>2   ram  1<br>3   eno  0<br>4   choi 0<br>5   lee  1<br>6   kali 1<br>7   sai  0</pre><p>In most cases, we apply an explicit condition to filter rows from a table. For example, to retrieve records where available is true, we might write:</p><pre>q)select from table where available = 1<br>sno name available<br>------------------<br>1   sam  1<br>2   ram  1<br>5   lee  1<br>6   kali 1</pre><p>However, when the column is of boolean type, there is no need to compare it explicitly with 1 or 1b. The boolean column itself can be used directly as the filter condition:</p><pre>q)select from table where available<br>sno name available<br>------------------<br>1   sam  1<br>2   ram  1<br>5   lee  1<br>6   kali 1</pre><p>This works because available already contains boolean values (1b and 0b). During filtering, q treats 1b as <strong>true</strong> (keep the row) and 0b as <strong>false</strong> (discard the row).</p><p>Similarly, to retrieve rows where the boolean value is false, we can use the not operator:</p><pre>q)select from table where not available<br>sno name available<br>------------------<br>3   eno  0<br>4   choi 0<br>7   sai  0</pre><p>For boolean columns, these forms are sufficient and are considered the idiomatic way to filter data in q:</p><p>→ select from table where available <br> → select from table where not available</p><p>This syntax is concise, readable, and avoids unnecessary comparisons.</p><p>Understanding this behavior helps you write more readable and efficient q queries, especially when working with large tables and real-world datasets where boolean flags are commonly used to represent states such as active, available, valid, or enabled.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=228f4795e986" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Filtering Data with Multiple Conditions in KDB+/q]]></title>
            <link>https://medium.com/@SathishVadivel/filtering-data-with-multiple-conditions-in-kdb-q-a37302c45e7e?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/a37302c45e7e</guid>
            <category><![CDATA[q]]></category>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[kdb]]></category>
            <category><![CDATA[where]]></category>
            <category><![CDATA[filters]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Sat, 09 May 2026 12:30:32 GMT</pubDate>
            <atom:updated>2026-05-23T13:46:01.945Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TPUnbcugbJ7qxDwDCyEjVg.jpeg" /></figure><p>Filtering data efficiently is one of the core skills in kdb+/q. While the where clause looks simple, it behaves differently depending on the data type—especially for symbol and char.</p><ul><li>Symbol data type</li><li>Char data type</li><li>Numeric data type (int, float, short, long)</li><li>Date data type</li></ul><p>Before jumping into filters, let’s define a simple table:</p><pre>/ Sample table<br>table: ([]<br>    id:1+til 7;<br>    name:`sam`ram`eno`sam`ram`eno`lee;<br>    Place:7?(&quot;Chennai&quot;;&quot;Bengalore&quot;;&quot;Delhi&quot;;&quot;Pune&quot;;&quot;Hyderabad&quot;);<br>    date:2025.04.01+til 7<br>    );<br><br>q)table<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br>6  eno  &quot;Bengalore&quot; 2025.04.06<br>7  lee  &quot;Delhi&quot;     2025.04.07</pre><ul><li>id → numeric type (long)</li><li>name → symbol type</li><li>place → char type (each value is a single character)</li><li>date → date type</li></ul><ol><li><strong>Filtering Symbol Data Type</strong></li></ol><p>✅Single Value Condition:</p><p>To filter a single symbol value:</p><pre>/ method-1 <br>q)select from table where name = `sam<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br><br>/ method-2<br>q)select from table where name in `sam<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br><br>/ method-3<br>q)select from table where name like &quot;sam&quot;<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>4  sam  &quot;Bengalore&quot; 2025.04.04</pre><p>Note: The like operator can also be used with symbol columns to filter data.</p><p>✅Multiple Value Condition:</p><p>To filter for multiple symbols:</p><pre>/ method-1<br>q)select from table where name in `sam`ram<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br><br>/ method-2<br>q)select from table where (|/)(name = `sam; name = `ram)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br><br>/ method-3<br>q)select from table where (name like &quot;sam&quot;) or (name like &quot;ram&quot;)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br><br>/ method-4<br>q)select from table where (|/)(name like &quot;sam&quot;; name like &quot;ram&quot;)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br><br>/ method-5<br>q)select from table where any name like/:(&quot;sam&quot;;&quot;ram&quot;)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05</pre><p><strong>2. Filtering Char Data Type</strong></p><p>✅Single Value Condition:</p><p>To filter a single char value:</p><pre>/ method-1<br>q)select from table where Place like &quot;Delhi&quot;<br>id name Place   date<br>--------------------------<br>3  eno  &quot;Delhi&quot; 2025.04.03<br>5  ram  &quot;Delhi&quot; 2025.04.05<br>7  lee  &quot;Delhi&quot; 2025.04.07<br><br>/ method-2<br>q)select from table where Place in enlist &quot;Delhi&quot;<br>id name Place   date<br>--------------------------<br>3  eno  &quot;Delhi&quot; 2025.04.03<br>5  ram  &quot;Delhi&quot; 2025.04.05<br>7  lee  &quot;Delhi&quot; 2025.04Filter.07</pre><p>✅Multiple Value Condition:</p><p>To filter for multiple char :</p><pre>/ method-1<br>q)select from table where Place in (&quot;Delhi&quot;;&quot;Bengalore&quot;)<br>id name Place       date<br>------------------------------<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br>6  eno  &quot;Bengalore&quot; 2025.04.06<br>7  lee  &quot;Delhi&quot;     2025.04.07<br><br>/ method-2<br>q)select from table where (Place like &quot;Delhi&quot;) or (Place like &quot;Bengalore&quot;)<br>id name Place       date<br>------------------------------<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br>6  eno  &quot;Bengalore&quot; 2025.04.06<br>7  lee  &quot;Delhi&quot;     2025.04.07<br><br>/ method-3<br>q)select from table where (|/)(Place like &quot;Delhi&quot;; Place like &quot;Bengalore&quot;)<br>id name Place       date<br>------------------------------<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br>6  eno  &quot;Bengalore&quot; 2025.04.06<br>7  lee  &quot;Delhi&quot;     2025.04.07<br><br>/ method-4<br>q)select from table where any Place like/:(&quot;Delhi&quot;;&quot;Bengalore&quot;)<br>id name Place       date<br>------------------------------<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br>5  ram  &quot;Delhi&quot;     2025.04.05<br>6  eno  &quot;Bengalore&quot; 2025.04.06<br>7  lee  &quot;Delhi&quot;     2025.04.07</pre><p><strong>3. Filtering Numeric Data Type</strong></p><p>✅Single Value Condition:</p><p>To filter a single Numeric value:</p><pre>/ method-1<br>q)select from table where id = 2<br>id name Place     date<br>----------------------------<br>2  ram  &quot;Chennai&quot; 2025.04.02<br><br>/ method-2<br>q)select from table where id in 2<br>id name Place     date<br>----------------------------<br>2  ram  &quot;Chennai&quot; 2025.04.02</pre><p>✅Multiple Value Condition:</p><p>To filter for multiple numeric:</p><pre>/ method-1<br>q)select from table where id in 2 3 4<br>id name Place       date<br>------------------------------<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br><br>/ method-2<br>q)select from table where id within 2 4<br>id name Place       date<br>------------------------------<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br><br>/ method-3<br>q)select from table where (id=2) or (id=3) or (id=4)<br>id name Place       date<br>------------------------------<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04<br><br>/ method-4<br>q)select from table where (|/)(id=2; id=3; id=4)<br>id name Place       date<br>------------------------------<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br>4  sam  &quot;Bengalore&quot; 2025.04.04</pre><p><strong>4. Filtering Date Data Type</strong></p><p>✅Single Value Condition:</p><p>To filter a single Date value:</p><pre>/ method-1<br>q)select from table where date = 2025.04.01<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br><br>/ method-2<br>q)select from table where date in 2025.04.01<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01</pre><p>✅Multiple Value Condition:</p><p>To filter for multiple date:</p><pre>/ method-1<br>q)select from table where date in 2025.04.01 2025.04.02 2025.04.03<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br><br>/ method-2<br>q)select from table where date within 2025.04.01 2025.04.03<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br><br>/ method-3<br>q)select from table where (date = 2025.04.01) or (date = 2025.04.02) or (date = 2025.04.03)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br><br>/ method-4<br>q)select from table where (|/)(date = 2025.04.01; date = 2025.04.02; date = 2025.04.03)<br>id name Place       date<br>------------------------------<br>1  sam  &quot;Hyderabad&quot; 2025.04.01<br>2  ram  &quot;Chennai&quot;   2025.04.02<br>3  eno  &quot;Delhi&quot;     2025.04.03<br></pre><p>Filtering data in KDB+/q becomes much more flexible once you understand how different data types behave with the where clause. As we explored, symbol, char, numeric, and date columns each support multiple filtering approaches such as =, in, within, like, logical or, and even functional forms.</p><p>While there are many ways to achieve the same result, choosing the right approach improves readability, maintainability, and query performance. In most real-world scenarios, operators like in and within provide the cleanest and most scalable solutions for handling multiple conditions in KDB+/q.</p><p>By mastering these filtering techniques, you can write more efficient queries, simplify data analysis tasks, and work more confidently with large-scale time-series datasets in KDB+/q.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a37302c45e7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiple String Search & Replace(SSR) in KDB+/q]]></title>
            <link>https://medium.com/@SathishVadivel/multiple-string-search-replace-ssr-in-kdb-q-b6be08249dfe?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6be08249dfe</guid>
            <category><![CDATA[kdb]]></category>
            <category><![CDATA[multiple-ssr]]></category>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[ssrs]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 07:04:26 GMT</pubDate>
            <atom:updated>2026-03-19T15:57:26.102Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1SI9n4lt-l-NIlsRWKIDuw.jpeg" /></figure><p>When working with text data in <strong>q</strong>, it’s common to clean or normalize strings before processing them. The built-in ssr function is useful for <strong>search and replace</strong>, but many developers overlook how easily it can perform <strong>multiple replacements in one step</strong>.</p><p>Let’s walk through a simple example.</p><h3><strong>Sample Input</strong></h3><pre>input:&quot;Hi, Can u make to&amp; cha-nge it&quot;</pre><p>Our string contains a few things we want to clean:</p><ul><li>Remove &amp;</li><li>Replace u with you</li><li>Remove -</li></ul><h3>Single Replacement with ssr</h3><p>The ssr function performs a <strong>search-and-replace</strong> on a string.</p><pre>ssr[input;&quot;&amp;&quot;;&quot;&quot;]<br>&quot;Hi, Can u make to cha-nge it&quot;</pre><p>Here:</p><ul><li>&quot;&amp;&quot; → the substring to find</li><li>&quot;&quot; → replace with nothing (effectively removing it)</li></ul><h3>Multiple Search &amp; Replace Using Over(/)</h3><p>Instead of running ssr multiple times manually, we can <strong>apply it iteratively</strong> using <strong>over (</strong><strong>/)</strong>.</p><pre>ssr/[input;(&quot;u&quot;;&quot;&amp;&quot;;&quot;-&quot;);(&quot;you&quot;;&quot;&quot;;&quot;&quot;)]<br>&quot;Hi, Can you make to change it&quot;</pre><p><strong>How It Works</strong></p><p>The syntax:</p><pre>ssr/[string; searchList; replaceList]</pre><p>Where:</p><ul><li>searchList → list of substrings to find</li><li>replaceList → corresponding replacements</li></ul><p>The / operator <strong>applies </strong><strong>ssr sequentially</strong> across the lists, passing the result of each replacement into the next step.</p><p><strong>Step-by-Step Concept</strong></p><p>Internally this behaves like:</p><pre>step1:ssr[input;&quot;u&quot;;&quot;you&quot;]<br>step2:ssr[step1;&quot;&amp;&quot;;&quot;&quot;]<br>step3:ssr[step2;&quot;-&quot;;&quot;&quot;]</pre><p>Result:</p><pre>&quot;Hi, Can you make to change it&quot;</pre><p>Using ssr/ keeps the code shorter and more expressive by replacing multiple lines of replacement logic with a single concise expression.</p><p><strong>✔️Tip:</strong> If you often perform the same replacements, you can store the search and replace lists as variables and reuse them across scripts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6be08249dfe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Appending Data to Splayed Tables in KDB+/q]]></title>
            <link>https://medium.com/@SathishVadivel/appending-data-to-splayed-tables-in-kdb-q-405f3ecab07a?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/405f3ecab07a</guid>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[kdb-table]]></category>
            <category><![CDATA[splayed-table]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[kdb]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Wed, 08 Oct 2025 07:23:46 GMT</pubDate>
            <atom:updated>2025-10-08T07:23:46.028Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqPUYIaq6z9KmYRR0Uyz_g.jpeg" /></figure><h4>Introduction :-</h4><p>In KDB+/q, appending data to a table is a common operation, but it works differently with splayed tables compared to regular tables. By default, methods like insert or upsert won’t work for splayed tables as they are structured differently. A typical insert operation like:</p><pre>`table_name insert (col1_value; col2_value; col3_value)</pre><p>is not applicable to splayed tables because of the way data is physically stored. Splayed tables are optimized for time-series data and store each column in separate files, which requires a specialized approach for appending new rows.</p><h4><strong>How to Insert a Row into a Splayed Table in KDB+/q :-</strong></h4><p>Since a splayed table stores each column as a separate file on disk, we cannot use the usual insert or upsert directly.</p><p>Instead, we use a file-based insert method that writes the new data directly into the underlying directory structure of the splayed table.</p><p><strong>Correct Syntax to Insert a Row into a Splayed Table</strong></p><p>To insert into a splayed table, use the following pattern:</p><pre>`:table_name/ insert .Q.en[`:.;]([] col1:value1; col2:value2; col3:value3)</pre><p>Let’s break this down:</p><ul><li>:table_name/ – The path to your splayed table (with a trailing slash to indicate a directory).</li><li>.Q.en[:.; table]` – Encodes the table in a format suitable for writing to disk.</li><li>([] col1:val1; col2:val2; ...) – This is a single-row keyed table used as the row to insert.</li></ul><p><strong>Important Note on </strong><strong>char Columns</strong></p><p>If you’re inserting into a column of type char (i.e., a string column), you must <strong>enlist</strong> the value. This is because a string like &quot;Chennai&quot; is treated as a list of characters in q unless explicitly enclosed.</p><p>Without enlist, &quot;Chennai&quot; would be interpreted as:</p><pre>&quot;Chennai&quot; ~ (&quot;C&quot;; &quot;h&quot;; &quot;e&quot;; &quot;n&quot;; &quot;n&quot;; &quot;a&quot;; &quot;i&quot;)</pre><p>Which will <strong>not match</strong> the expected single string per row in a char column.</p><h4>Example :-</h4><p>Here’s a full example of inserting a row into a splayed table named table1 with multiple data types:</p><pre>`:table_name/ insert .Q.en[`:.;]([] sno:7; name:`Naruto; fixed_date:2003.04.03; place:enlist &quot;Chennai&quot;)</pre><ul><li>sno is an integer.</li><li>name is a symbol.</li><li>fixed_date is a date.</li><li>place is a char (string), so we use enlist.</li></ul><p>This method safely writes the new row to disk in the correct format for a splayed table.</p><h3>Preview Before Insert: Safe Way to Insert into a Splayed Table :-</h3><p>When inserting directly into a splayed table using:</p><pre>`:table_name/ insert .Q.en[`:.; ([] ...)]</pre><p>you’re modifying the on-disk table <strong>immediately</strong>. While this is efficient, it’s also <strong>risky</strong> — if you make a mistake (wrong data type, unexpected nulls, typos), the change is already made and harder to undo.</p><p>To avoid that, you can use a <strong>preview-then-commit</strong> approach. This involves:</p><ol><li>First simulating the insert in memory using a join.</li><li>Verifying the result.</li><li>Writing it back to the splayed table only if you’re satisfied.</li></ol><h4>Step 1: Append In-Memory Using , Join</h4><p>You can use the comma join operator , to combine a new row with the existing splayed table.</p><p>✅ Table Format (with enlist for char):</p><pre>table_name, ([] sno:7; name:`Naruto; fixed_date:2003.04.03; place:enlist &quot;Chennai&quot;)</pre><p>✅ Dictionary Format (no enlist needed for char):</p><pre>table_name, (`sno`name`fixed_date`place!(7; `Naruto; 2003.04.03; &quot;Chennai&quot;))</pre><p>This does <strong>not</strong> change anything on disk. It only creates a new in-memory table with the new row appended. You can check the result, validate the structure, preview the data, and only move forward if everything looks correct.</p><h4>Step 2: Assign Back to the Variable</h4><p>If everything looks good, assign the updated table back:</p><pre>table_name: table_name, ([] sno:7; name:`Naruto; fixed_date:2003.04.03; place:enlist &quot;Chennai&quot;)</pre><h4>Step 3: Persist to Disk (Splayed Table)</h4><p>Finally, write the updated table back to the splayed folder:</p><pre>`:table_name/ set .Q.en[`:.; table_name]</pre><p>This safely writes your validated table to disk.</p><h3>Conclusion :-</h3><p>Inserting data into splayed tables in KDB+/q isn’t as straightforward as with in-memory tables. The default insert method doesn&#39;t apply, so we need to use .Q.en with proper formatting, especially for types like char. For safer updates, previewing the insert in-memory using a join allows you to verify data before committing changes. This reduces the risk of mistakes and ensures your splayed tables remain consistent. Always validate your data and structure before writing to disk for better reliability.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=405f3ecab07a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the ‘?’ and ‘_’ Operators in KDB+/Q]]></title>
            <link>https://medium.com/@SathishVadivel/understanding-the-and-operators-in-kdb-q-f9def2968607?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9def2968607</guid>
            <category><![CDATA[sathish-vadivel]]></category>
            <category><![CDATA[sathish]]></category>
            <category><![CDATA[operators]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[kdb]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Mon, 07 Jul 2025 17:33:46 GMT</pubDate>
            <atom:updated>2025-07-07T17:33:46.620Z</atom:updated>
            <content:encoded><![CDATA[<p>Working with lists is fundamental in Q/KDB, and mastering the core operators makes your data manipulation much easier. In this post, we’ll explore two powerful operators: ? (query) and _ (cut/drop). Whether you want to randomly select elements or remove parts of a list, understanding these operators will help you write cleaner and more efficient Q code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WF8hkQ0iyTkeNlvOh83kw.jpeg" /></figure><p>Let’s dive in and see how they work with practical examples.</p><h3><strong>1. ? (Query) Operator :-</strong></h3><p>The ? operator in Q has <strong>two different uses</strong>, depending on how you write it.</p><pre>q)2 ? 1 2 3 4 5 /licks the random value from list<br>5 1<br><br>q)1 2 3 4 5 ? 2 /returns the index of the value from the list<br>1</pre><p>When you write it like 2?1 2 3 4 5, it acts as a <strong>random query</strong>, picking a certain number of random elements from the list—in this case, 2 random values from [1, 2, 3, 4, 5].</p><p>On the other hand, when you write it like 1 2 3 4 5 ? 2, it behaves as a <strong>search operation</strong>. It looks for the value 2 in the list and returns its <strong>index position</strong> (starting from 0).</p><p>So, while the same symbol ? is used, its meaning changes based on its position in the expression: <strong>before the list</strong> means random selection, and <strong>after the list</strong> means search.</p><h3><strong>2. </strong><strong>_ (Cut/Drop) Operator :-</strong></h3><p>The _ operator in Q is also used in <strong>two different ways</strong>, depending on where the number appears.</p><pre>q)2 _ 1 2 3 4 5 /drops first two values<br>3 4 5<br><br>q)-2 _ 1 2 3 4 5 /drops last two values<br>1 2 3<br><br>q)1 2 3 4 5 _ 2 /drops the index value<br>1 2 4 5</pre><p>When you write 2 _ 1 2 3 4 5, it means <strong>“drop the first 2 elements”</strong> from the list. So, it removes 1 and 2, and returns [3, 4, 5].</p><p>But when the number comes <strong>after</strong> the list, like in 1 2 3 4 5 _ 2, it means <strong>“remove the element at index 2”</strong> (counting from 0). In this case, the number 3 (which is at index 2) is removed, giving the result [1, 2, 4, 5].</p><p>So just like the ? operator, the meaning of _ changes based on its position: <strong>before the list</strong> means “drop from the start,” and <strong>after the list</strong> means “drop a specific index.”</p><h3><strong>3. ‘?’ Symbol list :-</strong></h3><p>You can also use the ? operator to <strong>randomly pick items from a symbol list</strong>.</p><pre>q)2?`1<br>`k`f<br>q)2?`2<br>`en`pl<br>q)2?`3<br>`nni`glc<br>q)2?`4<br>`gkpm`bghg<br>q)2?`5<br>`ifnam`fohfn<br>q)2?`6<br>`kdjblm`eegndc<br>q)2?`7<br>`ncejfii`jognjhc<br>q)2?`8<br>`cihanjhp`hkpblald<br>q)2?`9        /upto 8 only i will support<br>&#39;9<br>  [0]  2?`9</pre><p>For example, if you write <strong>2?`3</strong>, Q will give you <strong>2 random symbols</strong> from the list like`kao`baf. It changes every time because it’s random.</p><p>But there’s one important thing to remember: <strong>you can only query up to 8 symbols</strong> at a time. If the list has more than 8 items and you try to query more than 8, it won’t work.</p><p>In Q, the ? and _ operators can behave differently based on how you use them. Whether you&#39;re querying random values or dropping parts of a list, understanding these subtle differences is key to writing clean and efficient code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9def2968607" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Table Joining in KDB+/q]]></title>
            <link>https://medium.com/@SathishVadivel/table-joining-in-kdb-q-f15482e88c59?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/f15482e88c59</guid>
            <category><![CDATA[join]]></category>
            <category><![CDATA[kdb-table]]></category>
            <category><![CDATA[table]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[kdb]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Mon, 09 Jun 2025 15:23:46 GMT</pubDate>
            <atom:updated>2025-06-09T15:23:46.566Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with databases, joining tables is a common operation. In KDB+/q, joining allows you to combine related data spread across different tables. This post will break down the most common types of joins in KDB+/q using simple language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uuItJP5KdwAPopJgqTlhyA.jpeg" /></figure><p>In this post, I’ve covered the following types of joins in KDB+/q:</p><ul><li>Left Join</li><li>Inner Join</li><li>Union Join</li><li>Equi Join</li><li>Comma Join (,)</li><li>Coalesce Join (^)</li></ul><h3>Example tables to perform joins:-</h3><pre>q)table1:([]sno:1 2 3 4 5; values:10 20 30 40 50)<br><br>q)table1<br>sno values<br>----------<br>1   10<br>2   20<br>3   30<br>4   40<br>5   50<br><br>q)table2:([]sno:5 6 7 8 9; values:500 600 700 800 900)<br><br>q)table2<br>sno values<br>----------<br>5   500<br>6   600<br>7   700<br>8   800<br>9   900</pre><h3>1. Left Join :-</h3><p>A <strong>left join (lj)</strong> in KDB+/q is used to <strong>update or enrich values in </strong><strong>table1 using matching rows from </strong><strong>table2</strong> based on a common key column.</p><p>To perform a left join:</p><ul><li><strong>Either the right-hand table (</strong><strong>table2) must be keyed</strong>,</li><li><strong>or both tables must be keyed.</strong></li></ul><p><strong>Option 1: Right-hand table is keyed</strong></p><pre>q)table1 lj (1!table2)<br>sno values<br>----------<br>1   10<br>2   20<br>3   30<br>4   40<br>5   500  / value updated from table2</pre><p><strong>Option 2: Both tables are keyed</strong></p><pre>q)(1!table1) lj (1!table2)<br>sno | values<br>----|-------<br>1   | 10<br>2   | 20<br>3   | 30<br>4   | 40<br>5   | 500</pre><h3>2. Inner Join :-</h3><p>An <strong>inner join (</strong><strong>ij)</strong> returns only the <strong>rows that have matching key values in both tables</strong>.</p><p>To perform an inner join:</p><ul><li><strong>Either the right-hand table (</strong><strong>table2) must be keyed</strong>,</li><li><strong>or both tables must be keyed.</strong></li></ul><p><strong>Option 1: Right-hand table is keyed</strong></p><pre>q)table1 ij 1!table2<br>sno values<br>----------<br>5   500</pre><p><strong>Option 2: Both tables are keyed</strong></p><pre>q)(1!table1) ij (1!table2)<br>sno | values<br>----|--------<br>5   | 500</pre><h3><strong>3. Union Join :-</strong></h3><p>A <strong>union join (</strong><strong>uj)</strong> combines two tables into one by stacking them together.</p><p>It works under <strong>two conditions</strong>:</p><ul><li><strong>Both tables must be either unkeyed or keyed.</strong></li><li>If this rule is not followed, the join will fail.</li></ul><p><strong>⚠️ Unkeyed Tables — May Result in Duplicates</strong></p><p>When both tables are <strong>unkeyed</strong>, all rows from both tables are combined — including duplicates.</p><pre>q)table1 uj table2<br>sno values<br>----------<br>1   10<br>2   20<br>3   30<br>4   40<br>5   50<br>5   500   / duplicate sno<br>6   600<br>7   700<br>8   800<br>9   900</pre><p>Here, the sno = 5 appears <strong>twice</strong> because the union is simply appending the tables.</p><p><strong>✅ Keyed Tables — Merges Based on Key</strong></p><p>When both tables are <strong>keyed</strong>, uj merges them by updating matching keys with values from the second table.</p><pre>q)(1!table1) uj (1!table2)<br>sno | values<br>----|--------<br>1   | 10<br>2   | 20<br>3   | 30<br>4   | 40<br>5   | 500   / value updated from table2<br>6   | 600<br>7   | 700<br>8   | 800<br>9   | 900</pre><p>So, if a key exists in both tables, the values from table2 <strong>overwrite</strong> those in table1.</p><h3>4. Equi Join :-</h3><p>An <strong>equi join</strong> is similar to an <strong>inner join</strong>, but with a key difference:</p><ul><li><strong>It does not require the tables to be keyed.</strong></li><li><strong>Instead, you specify the column to join on manually.</strong></li><li><strong>The syntax is different from other joins.</strong></li></ul><p><strong>Syntax:-</strong></p><pre>ej[`column; table1; table2]</pre><p><strong>Join </strong><strong>table1 with </strong><strong>table2 on </strong><strong>sno</strong></p><pre>q)ej[`sno; table1; table2]<br>sno values<br>----------<br>5   500   / value from table2</pre><p><strong>Join </strong><strong>table2 with </strong><strong>table1 on </strong><strong>sno</strong></p><pre>q)ej[`sno; table2; table1]<br>sno values<br>----------<br>5   50    / value from table1</pre><h3>5. Comma Join ( , ) :-</h3><p>The <strong>comma join </strong><strong>(,)</strong> is similar to a <strong>union join (</strong><strong>uj)</strong>, used to combine two tables.</p><p>✅ It works <strong>only when both tables are either keyed or unkeyed</strong>.</p><p><strong>Joining the table without keyed</strong></p><pre>q)table1, table2<br>sno values<br>----------<br>1   10<br>2   20<br>3   30<br>4   40<br>5   50<br>5   500   / duplicate row<br>6   600<br>7   700<br>8   800<br>9   900</pre><p>In this case, the rows are simply <strong>appended</strong>, including duplicates.</p><p><strong>Joining the table with keyed</strong></p><pre>q)(1!table1), (1!table2)<br>sno | values<br>----|--------<br>1   | 10<br>2   | 20<br>3   | 30<br>4   | 40<br>5   | 500   / value from table2 replaces table1<br>6   | 600<br>7   | 700<br>8   | 800<br>9   | 900</pre><p>When both tables are <strong>keyed</strong>, rows with matching keys are <strong>merged</strong>, and the second table’s values <strong>overwrite</strong> the first.</p><h3>6. Coalesce Join (^) :-</h3><p>The <strong>coalesce operator (</strong><strong>^)</strong> in KDB+/q is similar to performing a <strong>union join (</strong><strong>uj)</strong> on <strong>keyed tables</strong>.</p><pre>q)(1!table1) ^ (1!table2)<br>sno | values<br>----|--------<br>1   | 10<br>2   | 20<br>3   | 30<br>4   | 40<br>5   | 500   / updated from table2<br>6   | 600<br>7   | 700<br>8   | 800<br>9   | 900</pre><p>Understanding different types of joins in KDB+/q is essential for efficiently combining and analyzing data across tables. Each join — whether it’s left join, inner join, union join, equi join, comma join (,), or coalesce (^)—serves a specific purpose and behaves differently based on how the tables are structured (keyed or unkeyed).</p><p>By mastering these join techniques, you’ll be better equipped to write powerful queries and handle real-world data manipulation tasks with confidence.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f15482e88c59" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KDB+/q: Creating a Null Table with Data Types]]></title>
            <link>https://medium.com/@SathishVadivel/kdb-q-creating-a-null-table-with-data-types-1254dd0ee881?source=rss-0ad525dd1e52------2</link>
            <guid isPermaLink="false">https://medium.com/p/1254dd0ee881</guid>
            <category><![CDATA[kdb-table]]></category>
            <category><![CDATA[table-with-datatype]]></category>
            <category><![CDATA[q]]></category>
            <category><![CDATA[kdb]]></category>
            <category><![CDATA[null-table]]></category>
            <dc:creator><![CDATA[Sathish Vadivel]]></dc:creator>
            <pubDate>Wed, 09 Apr 2025 18:35:19 GMT</pubDate>
            <atom:updated>2025-11-20T12:24:24.606Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, we’ll explore how to create a <strong>null table in KDB+</strong> using the <strong>data types from an existing table</strong>. This is useful when you want to initialize a table structure with the correct schema but no data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aSiCjXyK-ANmMkkEvlSj_w.jpeg" /></figure><p>Let’s walk through the process using a sample student table.</p><p><strong>Step 1: Start with the Existing Table</strong></p><p>Assume we have a table named student:</p><pre>q)student<br><br>name  roll_no class age joined_date<br>-----------------------------------<br>Sam   1       A     14  2025.06.01 <br>Ram   2       B     14  2025.06.02 <br>Tom   3       B     14  2025.06.02 <br>Jerry 4       A     14  2025.06.01 <br>Sai   5       B     14  2025.06.03 </pre><p><strong>Step 2: Get the Metadata</strong></p><p>We use the meta keyword to retrieve the metadata of the student table and store it in a variable table1:</p><pre>q)table1:meta student<br><br>q)table1<br><br>c          | t f a<br>-----------| -----<br>name       | s    <br>roll_no    | i    <br>class      | c    <br>age        | i    <br>joined_date| d  </pre><p><strong>Step 3: Create a Dictionary for Data Types</strong></p><p>Now, let’s define a mapping from type letters to their corresponding data types as strings. We’ll store this in a dictionary named datatypeDict.</p><pre>q)datatypeDict:((&quot;s&quot;;&quot;i&quot;;&quot;c&quot;;&quot;d&quot;)!(&quot;`symbol$&quot;;&quot;`int$&quot;;&quot;`char$&quot;;&quot;`date$&quot;))<br><br>q)datatypeDict<br><br>s| &quot;`symbol$&quot;<br>i| &quot;`int$&quot;<br>c| &quot;`char$&quot;<br>d| &quot;`date$&quot;</pre><p><strong>Step 4: Add a symDatatype Column</strong></p><p>Using the dictionary, we update table1 with a new column called symDatatype, which holds the type conversion in string format. We save the result as table2.</p><pre>q)table2:update symDatatype:datatypeDict[t] from meta student<br><br>q)table2<br>c          | t f a symDatatype<br>-----------| -----------------<br>name       | s     &quot;`symbol$&quot; <br>roll_no    | i     &quot;`int$&quot;    <br>class      | c     &quot;`char$&quot;   <br>age        | i     &quot;`int$&quot;    <br>joined_date| d     &quot;`date$&quot;   </pre><p><strong>Step 5: Construct the Final Column Type Format</strong></p><p>We now create another column colType, which combines the data type and column name in the format (datatype$columnname). This is saved as table3.</p><pre>q)table3:update colType:{x,string y}&#39;[symDatatype;c] from table2<br><br>q)table3<br><br>c          | t f a symDatatype colType            <br>-----------| -------------------------------------<br>name       | s     &quot;`symbol$&quot;  &quot;`symbol$name&quot;     <br>roll_no    | i     &quot;`int$&quot;     &quot;`int$roll_no&quot;     <br>class      | c     &quot;`char$&quot;    &quot;`char$class&quot;      <br>age        | i     &quot;`int$&quot;     &quot;`int$age&quot;         <br>joined_date| d     &quot;`date$&quot;    &quot;`date$joined_date&quot;</pre><p><strong>Step 6: Create the Null Table Function</strong></p><p>Let’s define a function nullTableFunction that takes a table name (as a symbol) and constructs a <strong>null table with the same schema</strong> as the original.</p><pre>q)nullTableFunction:{value ((string x),&quot;:([] &quot;,(ssr[(&quot; &quot; sv (exec colType from table3));&quot; &quot;;&quot;:(); &quot;]),&quot;:())&quot;)}</pre><p><strong>Step 7: Execute the Function</strong></p><p>Run the function with the table name. This will recreate the structure of the student table with no data.</p><pre>q)nullTableFunction [`student]</pre><p>Now, check the contents and metadata of the table:</p><pre>q)student<br><br>name roll_no class age joined_date<br>----------------------------------<br><br>q)meta student<br><br>c          | t f a<br>-----------| -----<br>name       | s    <br>roll_no    | i    <br>class      | c    <br>age        | i    <br>joined_date| d  </pre><p>The student table is successfully recreated with <strong>the same columns and data types</strong>, but with <strong>no records</strong> — just like we intended!</p><p>Mission accomplished 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1254dd0ee881" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>