You are given a table Files containing file names and their text content.
Table Structure:
file_name(varchar): Unique file identifiercontent(text): The text content of the file
Write a SQL query to find the number of files that contain the words 'bull' and 'bear' as standalone words. The words must be surrounded by spaces or be at the beginning/end of the content.
Important: Words like 'bullet', 'bears', 'bull.', or 'bear,' should NOT be counted as they are not standalone occurrences.
Return the result showing each word ('bull' and 'bear') along with the count of files containing that word.
Table Schema
| Column Name | Type | Description |
|---|---|---|
file_name
PK
|
varchar | Unique identifier for each file |
content
|
text | Text content of the file |
Input & Output
| file_name | content |
|---|---|
| doc1.txt | The bull market is trending upward |
| doc2.txt | A bear was spotted in the woods |
| doc3.txt | The bullet train is very fast |
| doc4.txt | Bull and bear are both animals |
| word | n_files |
|---|---|
| bear | 2 |
| bull | 2 |
Analysis: Files doc1.txt and doc4.txt contain standalone 'bull'. Files doc2.txt and doc4.txt contain standalone 'bear'. File doc3.txt contains 'bullet' which is not counted as it's not a standalone 'bull'.
| file_name | content |
|---|---|
| file1.txt | bull. |
| file2.txt | bear! |
| file3.txt | bears are dangerous |
| file4.txt | no matches here |
| word | n_files |
|---|---|
| bear | 1 |
| bull | 1 |
Word Boundaries: 'bull.' and 'bear!' are counted as standalone words since punctuation marks serve as word boundaries. 'bears' is not counted as it contains additional letters.
Constraints
-
1 ≤ number of files ≤ 1000 -
file_namecontains unique values -
contentcan be empty or contain up to 10,000 characters - Words are case-sensitive