EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Compare Strings
 

PostgreSQL Compare Strings

Priya Pedamkar
Article byPriya Pedamkar

Updated May 18, 2023

PostgreSQL Compare Strings

 

 

Introduction to PostgreSQL Compare Strings

PostgreSQL Compare Strings compare the two string and return the result as we have specified the input, we can also update the rows using comparing strings in PostgreSQL. We can compare the string using a like clause in PostgreSQL, we can also compare the string using the =, !=, <>, <, >, <= and >= character string operator. Basically, the character string operator in PostgreSQL is used to compare the string and return the result as we specified input within the query. Comparison operator in PostgreSQL compares the two string values with each other and retrieves or update the rows.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Compare Strings in PostgreSQL?

We used the like and comparison operator to compare two strings in PostgreSQL. Comparison operator is very useful while comparing two strings. We are using the stud_str table to describe an example of compare string in PostgreSQL as follows.

Below is the table and data description of the stud_str table.

Code:

select * from stud_str;
\d+ stud_str;

Output:

PostgreSQL Compare Strings 1

Below is the comparison operator which we have used in PostgreSQL while comparing string in PostgreSQL.

  • < (Less than): It will return the true value when the left string is less than the right string.
  • > (Greater than): It will return the true value when the left string is greater than the right string.
  • <= (Less than or equal to): It will return the true value when the left string is less than equal to the right string.
  • >= (Greater than): It will return the true value when the left string is greater than or equal to the right string.
  • <> or != (Not equal): It will return the true value when the given string is not equal to each other.
  • = (Equal): It will return the true value when the two string are the same, and the string type is the same.

Examples of PostgreSQL Compare Strings

Given below are the examples mentioned:

Example #1

Compare string using the equality operator.

  • We can compare the string using the equality operator. Below example shows that we are comparing the string using the equality operator and retrieving the result using select operations.
  • In the below example, we have compared the string ABC with PQR to retrieve the result from the stud_str table.

Code:

select * from stud_str where first_name = 'ABC' and last_name = 'PQR';

Output:

PostgreSQL Compare Strings 2

  • Below example shows that we are comparing the string and updating the rows using update operations.
  • In the below example, we have compared the string ABC with PQR to update the rows from the stud_str table.

Code:

update stud_str set id = 11 where first_name = 'ABC' and last_name = 'PQR';
select * from stud_str;

Output:

PostgreSQL Compare Strings 3

Example #2

Compare string using greater than operator.

  • We can compare the string using the greater than operator. Below example shows that we are comparing the string using the greater than operator and retrieving the result using select operations.

Code:

select * from stud_str where first_name > 'ABC' and last_name > 'PQR';

Output:

using greater than operator

  • Below example shows that we are comparing the string using the greater than operator and updating the rows using update operations.

Code:

update stud_str set id = 12 where first_name > 'ABC' and last_name > 'PQR';
select * from stud_str;

Output:

using greater than operator and updating the rows using update operations

Example #3

Compare string using the like operator.

  • We can compare the string using the like operator. Below example shows that we have comparing the string using the like operator and retrieving the result using select operations.

Code:

select * from stud_str where first_name like '%BC' and last_name like 'PQ%';

Output:

using like operator

  • Below example shows that we are comparing the string using the like operator and updating the rows using update operations.

Code:

update stud_str set id = 101 where first_name like '%BC' and last_name like 'PQ%';
select * from stud_str;

Output:

PostgreSQL Compare Strings 8

Example #4

Compare string using less than the operator.

  • We can compare the string using less than the operator. Below example shows that we are comparing the string using less than operator and retrieving the result using select operations.

Code:

select * from stud_str where first_name < 'ABC' and last_name < 'PQR';

Output:

using less than operator

Example #5

Compare string using less than equal to operator.

  • We can compare the string using less than equal to the operator. Below example shows that we are comparing the string using less than equal to the operator and retrieving the result using select operations.

Code:

select * from stud_str where first_name <= 'ABC' and last_name <= 'PQR';

Output:

PostgreSQL Compare Strings 10JPG

  • Below example shows that we are comparing the string using less than equal to the operator and updating the rows using update operations.

Code:

update stud_str set id = 105 where first_name <= 'ABC' and last_name <= 'PQR';
select * from stud_str;

Output:

PostgreSQL Compare Strings 11JPG

Example #6

Compare string using greater than equal to operator.

  • We can compare the string using greater than equal to operator. Below example shows that we are comparing the string using greater than equal to the operator and retrieving the result using select operations.

Code:

select * from stud_str where first_name >= 'ABC' and last_name >= 'PQR';

Output:

PostgreSQL Compare Strings 12JPG

Conclusion

We have used =, !=, <>, <, >, <= and >= comparison operator to compare the string in PostgreSQL. Also, we have used a like operator to compare the string. We have compared the string using the select and update operations in PostgreSQL.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Compare Strings” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Recursive Query
  2. hstore in PostgreSQL
  3. PostgreSQL DECODE()
  4. PostgreSQL Inner Join

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW