COMPUTER ENGINEER

Jump to examples
Blog
SQL Server / T-SQL6 min study
10/ 65

Querying and aggregation

LIKE, IN, BETWEEN and ranges

Understand patterns, membership and boundaries.

UnderstandStudy an examplePractise

Understand the concept

In LIKE, % means zero or more characters and _ means one character. SQL Server also supports bracketed character groups, which are not portable across every SQL product. Case and accent matching depend on collation.

IN accepts separate values, not one comma-separated string. BETWEEN includes both boundaries. For timestamps, ending a month at 23:59:59 can miss more precise values. A half-open interval that includes the start and excludes the next period’s start is more reliable.

WHEN TO USE IT

Use these for search, selected categories and time-period filters.

COMMON PITFALL

Assuming BETWEEN excludes the upper boundary, or forgetting NULL in NOT IN.

01 / PRACTICE

One topic. Three levels of depth.

T-SQL / SQL Server 2019+
SELECT FullName FROM lab.Customer WHERE FullName LIKE N'D%';

Finds Deniz; % represents the remaining characters.

This page does not execute SQL. Practise in the downloadable SqlStudioLab using SSMS. Before rerunning temporary-table examples, remove only the relevant #table. Views, procedures and functions create persistent lab objects; never use a production connection.

02 / YOUR TURN

Try it yourself first.

Find products priced between 800 and 1800, including both boundaries.

Show the solution and explanation
Worked solution
SELECT ProductName, Price FROM lab.Product WHERE Price BETWEEN 800 AND 1800;

Mouse and Keyboard. Prefer half-open ranges for timestamp periods.