https://laptrinhvb.net/bai-viet/co-so-du-lieu/--SQL-SERVER---Huong-dan-su-dung-ham-WITH-de-de-quy-trong-sql/fcd08ab504203715.html#:~:text=%C4%90%E1%BB%87%20quy%20l%C3%A0%20qu%C3%A1%20tr%C3%ACnh,ng%C6%B0%E1%BB%9Di%20s%E1%BB%AD%20d%E1%BB%A5ng%20m%C3%A1y%20t%C3%ADnh. https://www.youtube.com/watch?v=Mt9UEDAMXa8 WITH temp ( id , name , alevel ) as ( Select id , name , 0 as aLevel From Category Where parent_id is null Union All Select b . id , b . name , a . alevel + 1 From temp as a , Category as b Where a . id = b . parent_id ) Select * From temp