Report SQL Clauses in Conceptual Evaluation Order
StringsAuto-graded · 17 test casesLast updated
Detect which of six SQL clauses appear in a query string and return them in SQL-92 logical evaluation order, using case-insensitive whole-word matching.
Report SQL Clauses in Conceptual Evaluation Order
A SQL query is written starting with SELECT, but a database conceptually evaluates its clauses in a different sequence. Under the SQL-92 model, the logical evaluation order of the six clauses is:
FROMWHEREGROUP BYHAVINGSELECTORDER BY
Given the text of a query, determine which of these six clauses are present and return their names in the logical evaluation order above.
Task
Implement a function that takes a query string and returns a list of clause names, containing only the clauses found in the text, arranged in the logical order listed above.
Rules
- Matching is case-insensitive:
select,Select, andSELECTall count asSELECT. - A keyword only counts as a whole word. Identifiers that merely contain a keyword, such as
FROMAGE,my_from, orSELECTED, do not count. Letters, digits, and underscore are word characters. GROUP BYandORDER BYeach count as one clause. The two words may be separated by any amount of whitespace (spaces, tabs, newlines), but at least one whitespace character is required.- The output order depends only on the fixed logical order, not on where the keywords appear in the text. A clause appears at most once in the result, no matter how many times it occurs.
- This is a keyword detector, not a SQL parser. Keywords inside string literals, comments, or subqueries are counted like any other occurrence.
- Output names must be exactly:
"FROM","WHERE","GROUP BY","HAVING","SELECT","ORDER BY"(uppercase, with a single space in the two-word names). - If no clause is found (including for an empty string), return an empty list.
Constraints
queryis a string; its length may be zero.- Only the six clauses above need to be detected.
- Return a list of strings.
Examples
Example 1
Input: clauses_in_logical_order('SELECT foo, COUNT(foo) FROM pokes WHERE foo > 10 GROUP BY foo HAVING COUNT(foo) > 1 ORDER BY foo;')
Output: ['FROM', 'WHERE', 'GROUP BY', 'HAVING', 'SELECT', 'ORDER BY']
Hints
Hint 1
Write the six clause names once, in logical order, and decide for each whether it occurs in the query. Filtering that fixed list keeps the right order automatically.
Hint 2
Normalize case first (for example by uppercasing), then use a regular expression with word boundaries so that identifiers like FROMAGE or my_from are not matched.
Hint 3
For GROUP BY and ORDER BY, allow one or more whitespace characters between the two words so tabs, newlines, and repeated spaces still match.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The output order is fixed by the SQL-92 logical model, so it never depends on where keywords sit in the text. That makes the problem a detection task rather than a sorting task.
- Store the six clauses in a list, already in logical order:
FROM,WHERE,GROUP BY,HAVING,SELECT,ORDER BY. Pair each with a regular expression. - Uppercase the query so matching is case-insensitive.
- Walk the list in order and keep each clause whose pattern is found in the query.
Patterns use \b word boundaries so only whole keywords match. FROMAGE, my_from, and SELECTED have no boundary at the relevant position, so they are rejected. For the two-word clauses, \s+ between the words accepts any run of whitespace, including tabs and newlines.
Why it is correct
- Because the list is in logical order and we only filter it, the result is always in logical order, with each clause appearing at most once.
- A clause is included exactly when a whole-word, case-insensitive occurrence exists anywhere in the text, which is the rule in the statement. Keywords in literals, comments, or subqueries count as well, since no parsing is done.
- An empty query matches nothing and yields
[].
The logical order itself comes from dependencies: WHERE filters raw rows before any aggregate exists, GROUP BY forms groups, HAVING filters those groups, SELECT computes output columns, and ORDER BY sorts the final output (so it can see SELECT aliases).
Complexity
- Time: O(n) for a query of length n. Uppercasing is linear, and six simple regex searches are each linear, with six being a constant.
- Space: O(n) for the uppercased copy; the output has at most six strings.
Reference solution
import re
from typing import List
def clauses_in_logical_order(query: str) -> List[str]:
# Clauses in SQL-92 logical evaluation order, each with a whole-word pattern.
clause_patterns = [
("FROM", r"\bFROM\b"),
("WHERE", r"\bWHERE\b"),
("GROUP BY", r"\bGROUP\s+BY\b"),
("HAVING", r"\bHAVING\b"),
("SELECT", r"\bSELECT\b"),
("ORDER BY", r"\bORDER\s+BY\b"),
]
upper_query = query.upper()
return [
clause
for clause, pattern in clause_patterns
if re.search(pattern, upper_query)
]