Combining Multiple Queries

Union / Except / Intersect

SqlKata allows you to combine multiple queries using one of the following available operators union, intersect and except by providing the following methods Union, UnionAll, Intersect, IntersectAll, Except and ExceptAll.

the above methods accept either an instance of Query or a labmda expression

var phones = new Query("Phones");
var laptops = new Query("Laptops");

var mobiles = laptops.Union(phones);
(SELECT * FROM [Laptops]) UNION (SELECT * FROM [Phones])

Or by using the labmda overload

var mobiles = new Query("Laptops").ExceptAll(q => q.From("OldLaptops"));
(SELECT * FROM [Laptops]) EXCEPT ALL (SELECT * FROM [OldLaptops])

Combining Raw Expressions

You can always use the CombineRaw method to append raw expressions

var mobiles = new Query("Laptops").CombineRaw("union all select * from OldLaptops");
SELECT * FROM [Laptops] union all select * from OldLaptops

Off course you can use the table identifier characters [ and ] to instruct SqlKata to wrap the tables/columns keywords.

var mobiles = new Query("Laptops").CombineRaw("union all select * from [OldLaptops]");
SELECT * FROM [Laptops] union all select * from [OldLaptops]