<?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>Forem: Alex Gascón</title>
    <description>The latest articles on Forem by Alex Gascón (@alexgascon).</description>
    <link>https://forem.com/alexgascon</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%2F13451%2F16232276-0c79-4ff1-9c9e-67f76d7c7b19.jpg</url>
      <title>Forem: Alex Gascón</title>
      <link>https://forem.com/alexgascon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alexgascon"/>
    <language>en</language>
    <item>
      <title>You are not worse when you are rejected</title>
      <dc:creator>Alex Gascón</dc:creator>
      <pubDate>Sun, 14 Jul 2019 22:53:59 +0000</pubDate>
      <link>https://forem.com/alexgascon/you-are-not-worse-when-you-are-rejected-1bod</link>
      <guid>https://forem.com/alexgascon/you-are-not-worse-when-you-are-rejected-1bod</guid>
      <description>&lt;p&gt;Tomorrow I'm starting a new job as a Software Development Engineer at Amazon. It's a step in my career that I would have considered completely unbelivable a few years ago, and maybe even a few months ago.&lt;/p&gt;

&lt;p&gt;Since I got the offer, I've been thinking a lot about how I got to that point. Of course, a very important part about it is preparing for the interview adequately, but there are hundreds of articles talking about the Amazon interview process, or about how to pass interviews at Big 4 or other great software companies; probably I will write one myself in the future, because I think that it may be useful to someone in the same way that the articles of people that interviewed with them before were useful to me. However, there's another part of the process that it's not often mentioned, and that really made a difference in my case: simply, being lucky.&lt;/p&gt;

&lt;p&gt;It's not a mystery to anyone that has been looking for a job that in most of the positions you apply to you won't get an answer, or if you do it will be to let you know that you have been rejected. That happened to me too: I also tried applying to lots of other companies, like Shopify, Stripe or Airbnb; and in all of them, I got rejected before even getting to the interview process.&lt;/p&gt;

&lt;p&gt;At first, all those rejections made me reconsider if I should even keep trying: if all those companies were rejecting me before even interviewing me, maybe I wasn't good as a software engineer. Maybe my experience wasn't enough, or maybe it was the lack of open source contributions. However, in the end I was contacted by Amazon, I entered the interview process, and ended up receiving a good offer from them. I tried to analyze what I did differently to reach further in the process, but the problem was that I couldn't compare that situation to the others, because on those I didn't even had the chance to prove my skills.&lt;/p&gt;

&lt;p&gt;Then it's when I realized one of the most valuable lessons I've learned in this interview process: the "me" that got rejected from lots of companies is the same "me" that got an offer from Amazon. My experience was the same, my skills were the same, my potential was the same. The only difference between those two days, is that one I was more lucky than the other: maybe that version of my CV highlighted exactly something that they where looking for or maybe it's simply that Linkedin search results showed me on the second page instead of the tenth. I'm not sure what may have been the root cause, but the consequence was that it allowed me to get to the interview process, where the outcome depended only on myself.&lt;/p&gt;

&lt;p&gt;Getting rejected is hard, because we tend to think that we've done something wrong and that there's something about ourselves that we should change. But it's important to realize that sometimes the reason for being rejected was completely out of our control, and that what we have to do is not to get discouraged and stop, but to keep trying until you get a chance to actually show what you're worth.&lt;/p&gt;

</description>
      <category>career</category>
      <category>interview</category>
      <category>growth</category>
      <category>advice</category>
    </item>
    <item>
      <title>Regret minimization</title>
      <dc:creator>Alex Gascón</dc:creator>
      <pubDate>Sat, 15 Jun 2019 23:26:51 +0000</pubDate>
      <link>https://forem.com/alexgascon/regret-minimization-a8c</link>
      <guid>https://forem.com/alexgascon/regret-minimization-a8c</guid>
      <description>&lt;p&gt;Lately I've been reading &lt;a href="https://www.amazon.com/One-Click-Jeff-Bezos-Amazon-com-ebook/dp/B004IYIUS8"&gt;&lt;em&gt;One Click: Jeff Bezos and the rise of Amazon.com&lt;/em&gt;&lt;/a&gt;, a book about Jeff Bezos and how Amazon came to be the internet giant that it is today. I'm still at the chapter 5/17, but what I found most curious so far is the fact that every time Bezos made a decision, not only about the business but also about his personal life, he took an incredibly analytic approach and studied all the possible outcomes and aspects that may influence the result.&lt;/p&gt;

&lt;p&gt;One of the ways in which this manifested was through what he called the "Regret Minimization Framework". Basically, it's a tool that helped him choose between a set of options by analyzing which one will be the one that he may regret less in the future. In the book, it's presented when he had to choose between continue working on his (really) high-paid job in a financial software company, or leaving it and start an Internet business.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I knew that when I was eighty there was no chance that I would regret walking away from my 1994 Wall Street bonus in the middle of the year. I wouldn't even have remembered that. But I did think there was a chance that I might regret significantly not participating in this thing called the Internet, that I believed passionately in. I also knew that if I had tried and failed, I wouldn't regret that" &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've had to make a big personal decision myself lately (switching to a new job and moving to another country, or continue at a job that still was a really good opportunity), and even if I didn't named it that way the "regret minimization" was definitely the most important factor that influenced my decision. If you think of it, it's probably the best way to make a decision because it forces you to think not only on the instant gratification that you will get from each option, but also on its long-term consequences. &lt;/p&gt;

&lt;p&gt;A detail that may seem insignificant but that for me it's one of the most important parts of the framework is how he considered both the good and the bad outcome. In the end, it's obvious that things will not always go as expected, and making sure that you won't regret a specific decision even if it goes completely wrong is probably one of the factors that you need to consider the most when you make the choice.&lt;/p&gt;

&lt;p&gt;Decision-making is the skill that will have the biggest impact in your life, and having a clear set of steps to follow during the process will undoubtedly help you get the best outcome from it. Are you comfortable to make that decision now, even considering that things may go wrong? Will you be able to change to another option if you think better about it in the future, and will there be any consequences? And, most importantly: will you be able to accept that decision 10, 20 or even 50 years from now?&lt;/p&gt;

</description>
      <category>career</category>
      <category>psychology</category>
      <category>motivation</category>
    </item>
    <item>
      <title>CPython, Cython, PyPy... An introductory guide to the different Python variants</title>
      <dc:creator>Alex Gascón</dc:creator>
      <pubDate>Sun, 20 Jan 2019 12:24:36 +0000</pubDate>
      <link>https://forem.com/alexgascon/cpython-cython-pypy-an-introductory-guide-to-the-different-python-variants-i4h</link>
      <guid>https://forem.com/alexgascon/cpython-cython-pypy-an-introductory-guide-to-the-different-python-variants-i4h</guid>
      <description>&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%2Fwww.python.org%2Fstatic%2Fcommunity_logos%2Fpython-logo-inkscape.svg" 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%2Fwww.python.org%2Fstatic%2Fcommunity_logos%2Fpython-logo-inkscape.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're into Python programming, chances are that you have heard the words CPython, Cython, Jython, PyPy... However, what are they exactly?&lt;/p&gt;

&lt;p&gt;In this article we will do a small introduction to some of these Python variants, so you can at least understand what they exactly refer to if you see them in any other context.&lt;/p&gt;

&lt;h3&gt;
  
  
  CPython
&lt;/h3&gt;

&lt;p&gt;CPython is the original Python implementation, the one you will probably use daily and that you can download officially from &lt;a href="//python.org"&gt;python.org&lt;/a&gt;. Its name comes from the fact that the Python code is compiled to bytecode using C.&lt;/p&gt;

&lt;h3&gt;
  
  
  Jython
&lt;/h3&gt;

&lt;p&gt;Jython is an alternative implementation that uses Java, instead of C, to obtain the bytecode. This enables this bytecode to run in the JVM, the Java Virtual Machine, like other languages such as Kotlin, Scala or Java itself.&lt;/p&gt;

&lt;p&gt;As Jython its just another implementation, the syntax remains unchanged: you can take all your CPython code and run it on Jython without needing to change a single line.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi5oonl9gqlox17eryxj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi5oonl9gqlox17eryxj.png" alt="Jython's logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Cython
&lt;/h3&gt;

&lt;p&gt;Cython is compiler that enables to write C extensions for Python, usually with the goal of making it more efficient. Unlike the previous examples, is not a different implementation: it uses CPython to run the Python code. It can be considered a superset of Python, as it contains all its functionality and adds the extra C capabilities on top of it.&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%2Favatars1.githubusercontent.com%2Fu%2F486082%3Fs%3D400%26v%3D4" 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%2Favatars1.githubusercontent.com%2Fu%2F486082%3Fs%3D400%26v%3D4" alt="Cython's logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These extensions are written in a specific syntax, much similar to the one you'll use for writing the equivalent C code. Let's see this example (from &lt;a href="https://en.wikipedia.org/wiki/Cython" rel="noopener noreferrer"&gt;Cython's Wikipedia site&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sum_up_to_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;

&lt;span class="n"&gt;cpdef&lt;/span&gt; &lt;span class="nf"&gt;sum_up_to_n&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;cdef&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  PyPy
&lt;/h3&gt;

&lt;p&gt;With PyPy, we have another Python implementation like CPython or Jython. The interesting point of this implementation is that it is implemented in... Python itself &lt;em&gt;(yes, probably this blew your mind, so keep reading to understand what we mean by that)&lt;/em&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%2Fpypy.org%2Fimage%2Fpypy-logo.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%2Fpypy.org%2Fimage%2Fpypy-logo.png" alt="Pypy's logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The core of PyPy is a Just-In-Time (JIT) compiler that it uses to compile the most repeated parts of your source code to the machine's native code (instead of bytecode, like CPython or Jython did). Running native code is more efficient than running bytecode, so this can result in great speed improvements even considering the time that will be used for compiling the code. &lt;/p&gt;

&lt;p&gt;PyPy has a lot of detailed information about its advantages (and disadvantages) in its website, as well as some performance tips and advice on which cases may not be best suited for it, so I encourage you to check it if you're interested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;After this article, you should be more familiar with the concepts of CPython, Jython, Cython and PyPy. However, chances are that you are interested in learning more about them, so I've prepared a list of links that can be really useful if want to dive deeper into the topic. I hope you find them useful!&lt;/p&gt;

&lt;h4&gt;
  
  
  Useful links
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.python.org/download/alternatives/" rel="noopener noreferrer"&gt;Python.org - Alternative Python implementations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.jython.org/jythonbook/en/1.0/index.html" rel="noopener noreferrer"&gt;The definitive guide to Jython&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cython.org/" rel="noopener noreferrer"&gt;Cython official website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypy.org/" rel="noopener noreferrer"&gt;PyPy official website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Just-in-time_compilation" rel="noopener noreferrer"&gt;Just-in-time compilation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.toptal.com/python/why-are-there-so-many-pythons" rel="noopener noreferrer"&gt;Article on Toptal about Python implementations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>pypy</category>
      <category>cython</category>
    </item>
    <item>
      <title>Combining SQL queries with UNION</title>
      <dc:creator>Alex Gascón</dc:creator>
      <pubDate>Sat, 24 Nov 2018 19:28:27 +0000</pubDate>
      <link>https://forem.com/alexgascon/combining-sql-queries-with-union-2l8j</link>
      <guid>https://forem.com/alexgascon/combining-sql-queries-with-union-2l8j</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiyhn2aug86im6jxvhoxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiyhn2aug86im6jxvhoxf.png" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Yesterday I was updating some Ruby gems on one of the projects that we have at my current company, and noticed one called &lt;a href="https://github.com/brianhempel/active_record_union" rel="noopener noreferrer"&gt;active_record_union&lt;/a&gt;. I have to admit that I had never heard about SQL's &lt;code&gt;UNION&lt;/code&gt; operation, so that gem generated a lot of curiosity on me. That's why I decided to get more information about it and write this learning!&lt;/p&gt;

&lt;h3&gt;
  
  
  SQL UNION
&lt;/h3&gt;

&lt;p&gt;The purpose of SQL Union is to combine the results of several queries into a single one. For example, let's assume that we have the following table in our DB, representing Users. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UserID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Japan&lt;/td&gt;
&lt;td&gt;27&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spain&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's assume that we want to search two types of users:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The users over 23 years old&lt;/li&gt;
&lt;li&gt;The users located on the United States&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to do that, you'll need the following two queries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM Users WHERE Country = "United States"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM Users WHERE Age &amp;gt; 23&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, this will require two DB calls; usually you want to obtain your result using as few DB calls as possible, and therefore this won't be an optimal solution. Besides, using different queries requires a manual processing of the results to combine them into one (as each query returns a result set)&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;UNION&lt;/code&gt; comes to play. Using &lt;code&gt;UNION&lt;/code&gt; you can both two queries into a single one, and therefore obtain the same results in a single DB operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"United States"&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UserID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Japan&lt;/td&gt;
&lt;td&gt;27&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By doing this, we will just need one DB operation, and also we will get all the matching users in the same result without needing to do any manual combination. &lt;/p&gt;

&lt;h4&gt;
  
  
  But, can't I just use OR?
&lt;/h4&gt;

&lt;p&gt;Probably you asked yourself that question when you saw the previous section. And you're right: you could also combine both queries into one by using several conditions linked with &lt;code&gt;OR&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"United States"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, &lt;strong&gt;the key is in the performance of each operation&lt;/strong&gt;. If you have indexes in the columns you're using to do the search, you will probably be better using &lt;code&gt;UNION&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;OR&lt;/code&gt; will make the SQL operation to use indexes just for one of the columns, or even not using them at all. On the other hand, using &lt;code&gt;UNION&lt;/code&gt; resolves both queries separately, enabling the use of the most optimal query plan for each query, which probably means using the index of each of the columns if they have one. You can find a more detailed explanation about it in the following links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/13750475/sql-performance-union-vs-or" rel="noopener noreferrer"&gt;SQL Performance UNION vs OR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/message-id/004f01c34c9d%24ce9edcc0%242766f30a%40development.greatgulfhomes.com" rel="noopener noreferrer"&gt;OR vs UNION&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Union over different tables
&lt;/h3&gt;

&lt;p&gt;Also, &lt;code&gt;UNION&lt;/code&gt; enables the obtention of results of several tables, instead of limiting to a simple one. For example, let's assume that now we have two tables: one for customers and one for suppliers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;CustomerID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spain&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SupplierID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;France&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we wanted to obtain all the customers or suppliers in USA, we could do something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'Customer'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CustomerID&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"United States"&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'Supplier'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SupplierID&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Suppliers&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"United States"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we would obtain something like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Customer&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Customer&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Supplier&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice how we didn't even need to specify the column names in the second query! As the columns are in the same position, &lt;code&gt;UNION&lt;/code&gt; automatically knows which column name has to be used. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(It's important to consider that this will only be possible if both operations returned the same number of columns, and if its types were similar)&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  UNION ALL
&lt;/h3&gt;

&lt;p&gt;Something that I didn't mention is that, when combining the results of the queries, &lt;code&gt;UNION&lt;/code&gt; does an extra operation to remove repeated rows. Going back to the first example, this would have been useful if instead of searching for those users older than 23, we would have wanted to search those older than 20: the user 1 matches both condition, and therefore he would have been present in both results. But to prevent us to have repeated data, &lt;code&gt;UNION&lt;/code&gt; would have filtered the results and returned his row only once. &lt;/p&gt;

&lt;p&gt;However, let's assume that we're completely sure that we won't have repetitions. For example, if we had a table like the following one:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UserID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;th&gt;Continent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;td&gt;America&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Japan&lt;/td&gt;
&lt;td&gt;Asia&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spain&lt;/td&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This time, let's assume that we want to retrieve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The users whose Country is United States&lt;/li&gt;
&lt;li&gt;The users whose Continent is Asia&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are absolutely certain that there will never be a coincidence between those two groups. Therefore, letting &lt;code&gt;UNION&lt;/code&gt; search for repetitions is a waste of time. &lt;/p&gt;

&lt;p&gt;For those cases, we can use the &lt;code&gt;UNION ALL&lt;/code&gt; operation: it also combines several queries, but without checking if there are repetitions in the result. Therefore, the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"United States"&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Continent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"Asia"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will return as a result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UserID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;th&gt;Continent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;United States&lt;/td&gt;
&lt;td&gt;America&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Japan&lt;/td&gt;
&lt;td&gt;Asia&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And it will actually be a faster than using a simple &lt;code&gt;UNION&lt;/code&gt; (how much faster will depend on the size of your result). &lt;/p&gt;

&lt;p&gt;However, it also requires you to be more careful about your operations,because if you combine two queries that can have repeated results...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"Spain"&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Continent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"Europe"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;UserID&lt;/th&gt;
&lt;th&gt;Country&lt;/th&gt;
&lt;th&gt;Continent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spain&lt;/td&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spain&lt;/td&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You will, in fact, end with repeated results. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The SQL &lt;code&gt;UNION&lt;/code&gt; statement is a really useful resource if you need to search by several conditions and want to take as much profit as possible from the indexes associated to the corresponding columns. It is also really useful if you want to combine results from two tables that have a similar structure (for example, if you're using Concrete Table Inheritance and only need the common data). &lt;/p&gt;

&lt;p&gt;However, it also has it's downsides: you need to be aware that the repetition removal step may slow down the query a little bit if you're retrieving a lot of rows; but the good part is that you can use &lt;code&gt;UNION ALL&lt;/code&gt; instead to skip that part. Of course, then you need to be completely sure that you won't have any repeated data among the different queries. As Uncle Ben would say: &lt;em&gt;"With great power, comes great responsibility"&lt;/em&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>programming</category>
      <category>database</category>
    </item>
  </channel>
</rss>
