Querying and aggregation
LIKE, IN, BETWEEN and ranges
Understand patterns, membership and boundaries.
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.
Use these for search, selected categories and time-period filters.
Assuming BETWEEN excludes the upper boundary, or forgetting NULL in NOT IN.
One topic. Three levels of depth.
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.
Try it yourself first.
Find products priced between 800 and 1800, including both boundaries.
Show the solution and explanation
SELECT ProductName, Price FROM lab.Product WHERE Price BETWEEN 800 AND 1800;Mouse and Keyboard. Prefer half-open ranges for timestamp periods.