title: Replace the following if conditions in SQLite
body: <p>I am trying to replace the following SQL query in SQLite but seems like I don't have much options available to write this query completely in SQLite. I am aware of using the where not exists of SQLite but not sure how to use the same when there are nested if conditions after if not exists condition like below. I can take care of this issue in code behind but wanted to know if this can be taken care off completely in a SQLite query. </p>
  
  <pre><code>IF NOT EXISTS (SELECT col1 from tb1 where tbl.id= @someId)
      BEGIN
          SET NOCOUNT ON;
  
          IF @someparam=@otherparam
              SELECT col1,col2 from tb1
              where tb1.somecolumn='xyz' or tb1.someothercolumn='pqr' 
              order by case col1 
                  WHEN 'something' THEN 1
                  WHEN 'someother' THEN 2
                  END
  
          IF @someparam=@otherparam2 OR @someparam = 'xyz'
              SELECT col1,col2 from tb1
              where tb1.somecolumn='abc' or tb1.someothercolumn='def' 
              order by case col1 
                 WHEN 'something' THEN 2
                  WHEN 'someother' THEN 1
                  END
      END
  ELSE
      BEGIN
          SET NOCOUNT ON;
          IF @someparam2=@otherparam3
  SELECT col1,col2 from (select distinct col1, col2 from tb3, tb1 where somecolumn not in 
  (select acolumn from tb1 where ggg = hhh) and tb1.somecolumn = 'zzz') as wwww
              order by case col1
                 WHEN 'nnn' THEN 1
                 WHEN 'mmm' THEN 2
                 WHEN 'kkk' THEN 3
                  END 
  
          IF @someparam2=@otherparam4 OR @someparam = 'xyz'
              SELECT col1,col2 from tb1
              where tb1.somecolumn='ccc' or tb1.someothercolumn='ddd'
              order by case col1
                 WHEN 'nnn' THEN 2
                 WHEN 'mmm' THEN 3
                 WHEN 'kkk' THEN 1
                  END 
      END
  END
  </code></pre>