SQLite is a server-less database engine and it is written in C programming language. The main moto for developing SQLite is to escape from using complex database engines like MYSQL etc. It has become one of the most popular database engines as we use it in Television, Mobile Phones, web browsers, and many more.
It is written simply so that it can be embedded into other applications. As we know we have a large database, so to find the specific string it is the best operator to search for the specific pattern.
In the article, We will be learning about how the SQLite GLOB Operator works, and along with that we will be also learning about how the SQLite GLOB function works with the examples.
GLOB Operator in SQLite
It is used to match only the text values against a pattern using wildcards. Here wildcard is nothing but the special character ( " * ", " "), and if the search expression is matched to the pattern expression then it returns true.
SQLite GLOB operator is case-sensitive. In simple words GLOB operator checks for the value of the specific string that is going to match the specific pattern or not.
- The asterisk sign (*)
- The question mark (?)
The asterisk sign represents many numbers or characters whereas the question mark represents only a single number or a character.
Example of GLOB Operator
To understand the GLOB Operator in SQLite we need a table on which we will perform various operation and queries. So here we have students table which consist of stu_id, first_name, fees, and email as Columns. If you don't know How To Create a Table in SQLite then refer this.
After inserting some data into the students table our table looks like:
Students TableExample 1: Glob Operator Starting With Specified Pattern
In this examples, We are going to use the Wildcard at the back of the specified pattern. Basically we are searching for the pattern that is going to start with the specified pattern.
Syntax:
SELECT FROM table_name
WHERE column GLOB 'XXXX*'
Query:
Let's Retrieve all student records from the students
table whose first names begin with the letter "R".
SELECT * from students
WHERE first_name GLOB 'R*';
Output:
Glob operator starting with specified patternExplanation: In the above Query We have fetch everything from the students table where the first character of first name of every student is R.
Example 2: Glob Operator In Between Pattern
Here we are going to search for the pattern that is inbetween the text. We are going to add wildcards infront and at the back and then we are going to find out the pattern based on the specified pattern.
Syntax:
SELECT FROM table_name
WHERE column GLOB '*XXXX*'
Query:
Let's Find and retrieve all information for faculty members whose names contain the letter "a" between the name from the faculty
table.
SELECT * FROM faculty
WHERE name GLOB '*a*';
Output:
Glob operator in between pattern
Explanation: In the above Query, We have seen that "*a*" which means we are asking for the first_name that contains the letter "a" between the names in the students table.
Example 3: Glob Operator Starting And Ending Pattern
In this example we are going to search for the pattern that is going to starts and end. Here we are going to specify the starting and ending of the pattern.
Syntax:
SELECT FROM table_name
WHERE column GLOB 'X???X'
Explanation of Syntax: Select statement to fetch the data followed by the table name and WHERE clause followed by the columname and here comes the GLOB clause with the pattern that is going to find in starting and ending of the pattern.
Query:
Let's Find and retrieve all student information for those with fees starting with 3, ending with 0, and having exactly three characters in between, as recorded in the students
table.
SELECT * FROM students
WHERE fees GLOB '3???0';
Output:
Glob operator starting and ending tagExplanation: In the above Query, We have fetch the everything from the students table for fees columns in which 3 should be at first position and 0 at the last positions and also there should be 3 character between them i.e. 3???0.
Example 4: Glob Operator Positions
In this below example we are going to search for the pattern that is going to be in the certain positions. Here we are going to use the wildcards wherever needed.
Syntax:
SELECT FROM table_name
WHERE column GLOB '?XXXX*'
Explanation of Syntax: Select statement to fetch the data followed by the table name and WHERE clause followed by the columname and here comes the GLOB clause with the pattern that is going to fetch the data by using the certain position of the pattern.
Query:
Let's retrieve full details for all students in the students
table whose recorded fees include the pattern "00" at any position in the fees.
SELECT * FROM students
WHERE fees GLOB '?00*';
Output:
Glob operator positions
Explanation: In the above Query, We have fetched everything from the students table based on the condition. It means we are asking for the fees that starts with the any digit and ends with the any digit but it should contain 0's in the second and thirds positions and hence whole table is retrieved as it matches the pattern.
Conclusion
SQLite GLOB operator is similar to LIKE but however Glob operator uses the UNIX file and it is CASE SENSITIVE. Using this function we can find our desired output very easily by writing the pattern in the huge databases. Glob operator is used to match the text values from the pattern that we specify.
We can specify the patterns as such that should be able to give the resulting ouput. The asterisk(*) wild card is used when we need to find multiple characters or numbers or zero. The question mark(?) wild card is used when we need to search for the single pattern or character. By the end of the article you will get knowledge on how the SQLite Glob operator works and the functionality of it.
Similar Reads
PL/SQL WITH Clause The PL/SQL WITH clause is a powerful feature that enhances the readability and performance of your SQL queries. It allows you to define temporary result sets, which can be referenced multiple times within a single query. This feature is particularly useful for simplifying complex queries and improvi
5 min read
PL/SQL WHERE Clause The WHERE clause in PL/SQL is essential for filtering records based on specified conditions. It is used in SELECT, UPDATE, and DELETE statements to limit the rows affected or retrieved, allowing precise control over data manipulation and retrieval.In this article, We will learn about the WHERE Claus
3 min read
SQL Clauses Structured Query Language (SQL) is a powerful language used to manage and manipulate relational databases. One of the essential features of SQL is its clauses, which allow users to filter, sort, group, and limit data efficiently. SQL clauses simplify querying and enhance database performance by retr
7 min read
PL/SQL HAVING Clause The PL/SQL HAVING clause is a powerful tool used in SQL for filtering records in groups defined by the GROUP BY clause. While the WHERE clause filters individual rows, the HAVING clause filters groups based on aggregate functions like SUM, COUNT, MIN, and MAX. This clause is essential when we want t
5 min read
SQL | USING Clause The SQL USING clause is a crucial feature in SQL that simplifies table join operations by allowing developers to specify common columns between tables. It enhances query readability and reduces redundancy by eliminating the need to qualify column names with table aliases.In this article, we will exp
4 min read
SQL | WHERE Clause The SQL WHERE clause allows filtering of records in queries. Whether you are retrieving data, updating records, or deleting entries from a database, the WHERE clause plays an important role in defining which rows will be affected by the query. Without WHERE clause, SQL queries would return all rows
4 min read