Category Archives: SQL Tutorials

Datový skladT-SQL is a language used for working with databases on the MS SQL Server platform. You will find a lot of articles and tutorials about T-SQL in this section. If you want to learn SQL or just remind yourself how to work with SQL function, check out the SQL tutorial. I want every article to contain an example of usage and a code example too. List of Articles in category »

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 UNION Operator – Unification of Sets with Example

UNION operator makes it possible to connect 2 results of query and remove all existing duplicities in them. In other words, operator actually performs DISTINCT in final unification of records. Syntax SELECT Column FROM dbo.Table WHERE Condition      UNION SELECT Column FROM dbo.Table WHERE Condition; Syntax is same as in all Set operators. Result… Read More »

SQL SUBSTRING Function – Get Part of String With Example

SUBSTRING function is used in sql to select part of text string in text. This function belongs to the group of text functions – string functions. I created a big summary of T-SQL text functions – you can find it here. Syntax SELECT SUBSTRING(text, <start position of the text character>, <the number of characters to… Read More »

SQL BETWEEN Operator – Defining Range in a Condition with Examples

This operator is used in SQL whenever it is needed to prove if certain value belongs to some range. This operator is used together with “AND” command which defines given range. Operator can be used in most of the clauses but is most common in WHERE for defining limiting requirement. Syntax SELECT [Date] FROM [dbo].[Table]… Read More »

SQL Handling Transaction COMMIT ROLLBACK – Prevent Troubles

What we call a transaction in SQL is logical set or sequence of operations which belong to this transaction. SQL Transactions can be used if we perform some changes in a databases. Advantage of transactions managing is that performed changes are reversible and we always need to use command COMMIT for confirmation or use ROLLBACK… 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 DELETE TABLE Statement, Difference DELETE and TRUNCATE, Deleting Large Amounts Of Data

We distinguish two types of commands with data deleting purpose – SQL commands DELETE and TRUNCATE. Each is suitable for different situations. It is good to know them both and be able to choose the right one in a specific situation. DELETE Syntax (First Command is Optional): DELETE FROM dbo.Table WHERE <Condition>; or DELETE dbo.Table WHERE… Read More »

SQL CREATE TABLE- How to Create a Database Table (SQL Server)

Creating a table in the SQL Server database is a basic skill. While creating it using SQL command CREATE TABLE we define: Name of the table together with database definition and scheme List of the columns with appropriate data types, constraints and definition if the column can be empty (NULL vs. NOT NULL) Table can… Read More »

SQL ISNULL and COALESCE Functions – What Are The Differences And Usage

ISNULL and COALESCE are functions used for work with null values (NULL). Both functions return first value out of previously defined entry parameters. There are also some differences between both functions. ISNULL Syntax: SELECT ISNULL(<Column>, <Column Replacement – other column or logic>) FROM dbo.Table  COALESCE Syntax: SELECT COALESCE(<Column>, <Column Replacement – other column or logic… Read More »

SQL View – Create, Alter, Drop View? Do you know Indexed view?

Views have definitely their place in databases and data warehouses. We are talking about objects which are good to use since they do not contain data (do not take up place in storage). They consist only of table query. More complex views can be more expensive for logical operations mostly with complicated queries with multiple… Read More »