Detecting Missing Gaps in a Sequence 🔍
SELECT t1.id + 1 AS missing_id FROM table_name t1 LEFT JOIN table_name t2 ON t1.id + 1 = t2.id WHERE t2.id IS NULL;
- Join each row to the next expected row.
- Filter out rows where the next ID exists.
sys.dm_os_wait_stats is a Dynamic Management View that provides information about the waits that are occurring in SQL Server. It captures data on the different types of waits and their durations, which helps in identifying performance bottlenecks.
Key Columns:
Use Case:
Analyzing wait statistics helps diagnose performance issues by showing where SQL Server spends time waiting for resources. For instance, high waits on PAGEIOLATCH might indicate slow disk I/O, while high waits on CXPACKET might suggest parallelism issues.
Example Query:
SELECT wait_type, wait_time_ms, waiting_tasks_count, signal_wait_time_ms, resource_wait_time_ms FROM sys.dm_os_wait_stats WHERE wait_type NOT LIKE 'SQLTRACE%' -- Exclude irrelevant wait types ORDER BY wait_time_ms DESC;
The FIND_IN_SET() function returns the position of a string within a list of strings.
Syntax
FIND_IN_SET(string, string_list)
Parameter Values
string - Required. The string to search for string_list - Required. The list of string values to be searched (separated by commas)
Return Values
If string is not found in string_list, this function returns 0 If string or string_list is NULL, this function returns NULL If string_list is an empty string (""), this function returns 0
Example
SELECT FIND_IN_SET("q", "s,q,l");
SELECT AVG(salary) AS median FROM ( SELECT salary FROM employees ORDER BY salary LIMIT 2 - (SELECT COUNT(*) FROM employees) % 2 OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees) ) subquery;