Skip to content
Question

Q.Find the output of the following SQL queries :

(i) SELECT SUBSTR(“FIT INDIA MOVEMENT”,5);
(ii) SELECT INSTR(“ARTIFICIAL INTELLIGENCE”, “IA”);
CBSECBSE Class XII Board 2022Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This answer explains how SQL's SUBSTR function extracts a portion of a string and INSTR finds the starting position of a substring, then provides the output for the given queries.

In the world of databases, managing and manipulating text data is a common and crucial task. SQL (Structured Query Language) provides a rich set of functions specifically designed for this purpose, known as string functions. These functions allow us to perform various operations like extracting parts of a string, finding specific characters or sequences, changing case, and much more. Understanding these functions is essential for anyone working with textual information in a database.

Let's delve into two such important string functions: SUBSTR and INSTR.

Understanding SUBSTR

The SUBSTR function, short for "substring," is used to extract a portion of a string. Think of it like taking a slice out of a longer piece of text. It requires at least two arguments: the original string you want to work with, and the starting position from where you want to begin extracting. Optionally, you can also specify the length of the substring you wish to extract.

  • Syntax: SUBSTR(string, start_position, [length])
  • string: This is the original text from which you want to extract a part.
  • start_position: This is an integer indicating where the extraction should begin. Importantly, SQL string positions are typically 1-based, meaning the first character is at position 1, the second at position 2, and so on.
  • length (optional): This is an integer specifying how many characters to extract from the start_position. If this argument is omitted, SUBSTR will extract all characters from the start_position right up to the end of the original string.
Important

SQL string functions like SUBSTR and INSTR typically use 1-based indexing, meaning the first character of a string is at position 1, not 0.

Let's apply this to your first query:

(i) SELECT SUBSTR(“FIT INDIA MOVEMENT”,5);

Here, the original string is "FIT INDIA MOVEMENT". The start_position is 5. Since the length argument is omitted, the function will extract all characters from the 5th position until the end of the string.

Let's count the characters:

F (1), I (2), T (3), (space) (4), I (5), N (6), D (7), I (8), A (9), (space) (10), M (11), O (12), V (13), E (14), M (15), E (16), N (17), T (18).

The character at position 5 is 'I'. Therefore, SUBSTR will return the substring starting from this 'I' and continuing to the end.

Output for (i): INDIA MOVEMENT

Understanding INSTR

The INSTR function, short for "in string" or "instrument," is used to find the starting position of a specific substring within a larger string. It tells you exactly where a particular sequence of characters first appears.

  • Syntax: INSTR(string, substring)
  • string: This is the main text in which you want to search.
  • substring: This is the sequence of characters you are looking for within the main string. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.