STRCMP() Function in SQL

Last Updated : 10 Sep, 2026

The STRCMP() function in SQL is used to compare two strings. It returns a value based on whether the first string is equal to, less than or greater than the second string.

  • Returns 0 when both strings are equal.
  • Returns -1 when the first string is less than the second string.
  • Returns 1 when the first string is greater than the second string.

Syntax

STRCMP(string1, string2)

Where:

  • string1: Specifies the first string to compare.
  • string2: Specifies the second string to compare.

Note: STRCMP() is mainly supported in MySQL. SQL Server uses comparison operators such as =, < and >, while SQLite does not provide a built-in STRCMP() function.

Example 1: Compare Two Equal Strings

SELECT STRCMP('SQL', 'SQL') AS result;

Output:

1
  • Both strings are equal, so the function returns 0.

Example 2: Compare Two Different Strings

SELECT STRCMP('Apple', 'Banana') AS result;

Output:

2
  • Apple comes before Banana, so the function returns -1.

Example 3: Compare Strings from a Table

Consider the following products table:

3

Now, compare each product name with 'Laptop':

SELECT product_name,
STRCMP(product_name, 'Laptop') AS comparison_result
FROM products;

Output:

4
  • The STRCMP() function compares each product_name with 'Laptop' and returns the comparison result.
Comment

Explore