0

I am new to SQLite and SQL. I was playing with a few nested queries and found that I was repeatedly using some subqueries. I wanted to know if there is any way to simplify the queries for better readability. Consider the following query.

SELECT *
FROM
    table1
INNER JOIN
(
    SELECT *
    FROM
        table2
    WHERE
        col1=1234
) t
ON table1.col3=t.col5

I wanted to simplify this by something equivalent to the following (in C macro syntax):

#define MYMACRO(X) SELECT * FROM table2 WHERE col1=X
SELECT * FROM table1 INNER JOIN MYMACRO(1234) t ON table1.col3=t.col5

Is this possible ?

Note: I know I can simplify the first query so that it would not be nested. I am just using it for explaining my question.

Rahul
  • 963
  • 9
  • 14

1 Answers1

0

WITH is the keyword you're looking for.

With MyMacro AS SELECT * FROM table2 WHERE col1=@parameter

SELECT * FROM table1 INNER JOIN MyMacro ON table1.col3=MyMacro.col5
OwlsSleeping
  • 1,487
  • 2
  • 11
  • 19
  • Sorry, no variables in SQLite. This goes over alternatives https://stackoverflow.com/questions/7739444/declare-variable-in-sqlite-and-use-it – OwlsSleeping Aug 29 '18 at 10:16