diff --git a/pykusto/query.py b/pykusto/query.py
index 42caba4..272bbaf 100644
--- a/pykusto/query.py
+++ b/pykusto/query.py
@@ -53,7 +53,12 @@ class Query:
         self._table = head if isinstance(head, Table) else None
 
     def __add__(self, other: 'Query'):
-        other._head = self
+        other_base = other
+        while other_base._head is not None:
+            if other_base._head._head is None:
+                break
+            other_base = other_base._head
+        other_base._head = self
         return other
 
     def where(self, predicate: BooleanType) -> 'WhereQuery':

query.__add__ only handles short queries
diff --git a/test/test_query.py b/test/test_query.py
index a228e03..034feec 100644
--- a/test/test_query.py
+++ b/test/test_query.py
@@ -15,9 +15,21 @@ class TestQuery(TestBase):
         )
 
     def test_add_queries(self):
-        query = Query().where(col.foo > 4) + Query().take(5) + Query().sort_by(col.bar, Order.ASC, Nulls.LAST)
+        query = (Query().where(col.foo > 4) +
+                 Query().take(5) +
+                 Query().where(col.foo > 1).sort_by(col.bar, Order.ASC, Nulls.LAST))
         self.assertEqual(
-            " | where foo > 4 | take 5 | sort by bar asc nulls last",
+            " | where foo > 4 | take 5 | where foo > 1 | sort by bar asc nulls last",
+            query.render(),
+        )
+
+    def test_add_queries_with_table(self):
+        mock_kusto_client = MockKustoClient()
+        table = PyKustoClient(mock_kusto_client)['test_db']['test_table']
+        b = Query().take(5).take(2).sort_by(col.bar, Order.ASC, Nulls.LAST)
+        query = Query(table).where(col.foo > 4) + b
+        self.assertEqual(
+            "test_table | where foo > 4 | take 5 | take 2 | sort by bar asc nulls last",
             query.render(),
         )
 

pip install -e .[dev]
parse_log_pytest
pytest
pytest
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
adal==1.2.7
attrs @ file:///croot/attrs_1668696182826/work
azure-kusto-data==0.0.31
certifi @ file:///croot/certifi_1671487769961/work/certifi
cffi==1.15.1
charset-normalizer==3.4.1
cryptography==44.0.2
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
idna==3.10
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1648562407465/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
packaging @ file:///croot/packaging_1671697413597/work
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycparser==2.21
PyJWT==2.8.0
-e git+https://github.com/Azure/pykusto.git@aff79137b6d310f33a2085ece2fbe41886c50c11#egg=pykusto
pytest==7.1.2
python-dateutil==2.9.0.post0
requests==2.31.0
six==1.17.0
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typing_extensions @ file:///croot/typing_extensions_1669924550328/work
urllib3==2.0.7
zipp @ file:///croot/zipp_1672387121353/work

name: pykusto
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib-metadata=4.11.3=py37h06a4308_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - typing_extensions=4.4.0=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - adal==1.2.7
      - azure-kusto-data==0.0.31
      - cffi==1.15.1
      - charset-normalizer==3.4.1
      - cryptography==44.0.2
      - idna==3.10
      - pycparser==2.21
      - pyjwt==2.8.0
      - python-dateutil==2.9.0.post0
      - requests==2.31.0
      - six==1.17.0
      - urllib3==2.0.7
prefix: /opt/conda/envs/pykusto

test/test_query.py::TestQuery::test_add_queries test/test_query.py::TestQuery::test_add_queries_with_table
test/test_query.py::TestQuery::test_count test/test_query.py::TestQuery::test_custom test/test_query.py::TestQuery::test_distinct test/test_query.py::TestQuery::test_distinct_all test/test_query.py::TestQuery::test_extend test/test_query.py::TestQuery::test_extend_assign_to_multiple_columns test/test_query.py::TestQuery::test_extend_generate_column_name test/test_query.py::TestQuery::test_join_no_joined_table test/test_query.py::TestQuery::test_join_no_on test/test_query.py::TestQuery::test_join_with_table test/test_query.py::TestQuery::test_join_with_table_and_query test/test_query.py::TestQuery::test_limit test/test_query.py::TestQuery::test_mv_expand test/test_query.py::TestQuery::test_mv_expand_args test/test_query.py::TestQuery::test_mv_expand_no_args test/test_query.py::TestQuery::test_order test/test_query.py::TestQuery::test_order_expression_in_arg test/test_query.py::TestQuery::test_project test/test_query.py::TestQuery::test_project_assign_to_multiple_columns test/test_query.py::TestQuery::test_project_away test/test_query.py::TestQuery::test_project_away_wildcard test/test_query.py::TestQuery::test_project_rename test/test_query.py::TestQuery::test_project_unspecified_column test/test_query.py::TestQuery::test_project_with_expression test/test_query.py::TestQuery::test_sample test/test_query.py::TestQuery::test_sanity test/test_query.py::TestQuery::test_sort test/test_query.py::TestQuery::test_sort_multiple_cols test/test_query.py::TestQuery::test_summarize test/test_query.py::TestQuery::test_summarize_by test/test_query.py::TestQuery::test_summarize_by_expression test/test_query.py::TestQuery::test_take test/test_query.py::TestQuery::test_top test/test_query.py::TestQuery::test_udf test/test_query.py::TestQuery::test_where
pykusto/query.py
pykusto/query.py