'Multiline SQLite in JupyterLab seems to be virtually unusable

I'm trying to use SQLite queries in JupyterLab. Virtually all multiline queries I try end up with an "unexpected indent" or "invalid syntax" error. Single-line queries seem to work fine, but multiline never works, with few, apparently random exceptions. Examples:

  1. Single line, works as expected.

    %sql SELECT employee_id, last_name FROM employee
    
  2. Multiline, unexpected indent.

    %sql SELECT employee_id, 
    last_name 
    FROM employee
    
  3. Multiline, works. (Found the \ trick here on SO. It also says multiline should work with the %%sql magic, but it is not recognised as such.)

    %sql SELECT employee_id, \
    last_name \
    FROM employee
    
  4. Multiline, but without querying last_name. Doesn't work, unexpected indent.

     %sql SELECT employee_id \   
     FROM employee
    

I've tried all sorts of variations of indent, it never works. I've tried triple quotes, parentheses, nothing works. If I can't make multiline queries, SQL is unusable in JupyterLab. It can't be so stupidly complicated, so I must be doing something wrong. Any idea what it might be?



Solution 1:[1]

I found the answer. %%sql needs to be the first thing written in the cell. If there's even a comment before it (as it was in my case), everything breaks down as I illustrated above. That's odd anyway, because writing %%sql anywhere else will result in an error message saying that the %%sql line magic was not found.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicola