Category Archives: SQL Commands

SQL ROLLUP, CUBE, GROUPING SETS – Totals and Subtotals

What about upgrading GROUP BY clause by use of useful commands? GROUP BY is used in a clause with aggregate operations. When it is used, aggregation happens throughout all columns. Using this simple “grouping” does not enable us to do totals and subtotals. But there is plenty of operators which can easily query for totals.… Read More »

SQL TRY CATCH and Error Handling with Examples

SQL TRY CATCH command is designed to control code flow in case that error situation occurs (error handling). This construction consists of  blocks. It is also possible to use transaction (more in article on Transactions). If an error occurs in the first block – TRY..END like that is activated code in CATCH..END block. TRY CATCH… Read More »

SQL UNION ALL – Unification of Queries with Example

Operator UNION ALL in SQL makes it possible to unify 2 query results and does not remove duplicities in queries. Operator returns records of both queries with no regard for existing duplicities. UNION ALL belongs to group called SET operators together with UNION, EXCEPT, INTERSECT Syntax SELECT Column FROM dbo.Table WHERE Condition   UNION ALL SELECT Column… Read More »

SQL CTE (Common Table Expressions) With Examples – More Organized Queries and Procedures

SQL CTE or so called Common table expression is simply put a temporary table (or better said it is a result set) in form of an expression. We can refer to it in SELECT, INSERT, DELETE or UPDATE script after its declaration using clause WITH. We can make recursive queries using CTE (it is even… Read More »

SQL OVER() with PARTITION BY – Clause for Gourmets With Examples

OVER clause can belong to commands working with so-called Window functions in SQL. These types of functions enable us to look at the data not only in their aggregated form (using GROUP BY), but we can also look at functional operations via multiple other attributes. We, in fact, create windows of some sort and we… Read More »

SQL INSERT – Inserting Rows into Database Table (3 methods) + Common mistakes

SQL Command INSERT is suitable for situations when we want to insert entries into the table. Insertion of rows can be done in multiple ways (further description below): You can insert the values that you fixedly choose (INSERT INTO … VALUES (value1, value2, …)) You can insert the values into the table using script (SELECT… Read More »

SQL | Triggers in SQL Server – Definition, Types, Syntax and Examples

A trigger is a database object (procedure) that works as a watchdog for certain event. Using database triggers we can catch this event and initiate some additional action – such as logging or rejecting the action. The event that initiates the action may be: INSERT or UPDATE records in the table DELETE records from the… Read More »