ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SQL - Find First Non-Repeating Character

SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1WITH CharCounts AS (
2SELECT
3SUBSTRING(input_string, n, 1) AS char,
4COUNT(*) AS count
5FROM
6(SELECT 'abcdefgabc' AS input_string) AS s
7CROSS JOIN
8(SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10) AS numbers
9WHERE n <= LENGTH(input_string)
10GROUP BY char
11)
12SELECT
13char
14FROM
15(SELECT
16s.input_string,
17SUBSTRING(s.input_string, n, 1) AS current_char,
18ROW_NUMBER() OVER (ORDER BY n) AS char_order
19FROM
20(SELECT 'abcdefgabc' AS input_string) AS s
21CROSS JOIN
22(SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10) AS numbers
23WHERE n <= LENGTH(s.input_string)) AS indexed_chars
24JOIN
25CharCounts ON indexed_chars.current_char = CharCounts.char
26WHERE
27CharCounts.count = 1
28ORDER BY
29indexed_chars.char_order
30LIMIT 1;
Algorithm description viewbox

SQL - Find First Non-Repeating Character

Algorithm description:

This SQL query identifies the first character in a given string that appears only once. It's useful for data cleaning tasks where you need to find unique identifiers or anomalies within text fields. For example, in a log file, you might use this to find the first unique error code.

Algorithm explanation:

The query first uses a Common Table Expression (CTE) called `CharCounts` to count the occurrences of each character in the input string. It achieves this by generating a sequence of numbers up to the length of the string and then using `SUBSTRING` to extract each character. The `GROUP BY` clause aggregates these counts per character. In the main query, it re-iterates through the string, assigning a row number to each character based on its position. It then joins this with the `CharCounts` CTE. By filtering for characters where the count is exactly 1 and ordering by the original character position, it selects the first non-repeating character. The time complexity is dominated by the string processing and counting, which is O(N*M) where N is the number of characters in the string and M is the maximum possible length of the string (due to the numbers table, though practically it's O(N^2) in naive implementations or O(N) if the numbers table is optimized). Space complexity is O(K) where K is the number of unique characters.

Pseudocode:

1. Define a CTE `CharCounts`.
2. Inside `CharCounts`, for each character in the input string, count its total occurrences.
3. In the main query, iterate through the input string again, keeping track of character positions.
4. Join the current character's position with the `CharCounts`.
5. Filter for characters whose count is 1.
6. Order the results by their original position in the string.
7. Return the first character from the ordered results.