EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial Wildcards in PostgreSQL
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

Wildcards in PostgreSQL

By Priya PedamkarPriya Pedamkar

Wildcards in PostgreSQL

Introduction to Wildcards in PostgreSQL

Wildcards in PostgreSQL is used to find matching rows values from tables; it is also used to find matching patterns rows from tables, Wildcards is also used to find matching rows, column and tables names; the output of the wildcard operator will return matching name, which was table name, column name or rows, In PostgreSQL like operator is used to match text values against a pattern using wildcards, If search expression that we have using in query can be matched to the pattern expression, wildcard operator is essential in PostgreSQL.

Explain all Wildcards in PostgreSQL

We will explain all wildcards in PostgreSQL one by one are as follows. The following are the wildcard operatory used in PostgreSQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • Using percentage (%) wildcard
  • Using underscore (_) wildcard

1. Using % Wildcard

  • PostgreSQL wildcard is used to match text values from matching patterns. Like operator is used to find matching text values from the table.
  • There are two types of wildcard operators used in PostgreSQL.
  1. Percentage (%) wildcard
  2. Underscore (_) wildcard
  • The percentage (%) sign represents one, zero or multiple no of characters, which have used percentage in a different form to retrieve the output.
  • We have using the employee table to describe the wildcard operator in PostgreSQL.

Table Example

testing=# CREATE TABLE Employee ( emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, PRIMARY KEY (emp_name));
testing=# INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'ABC', 'Pune', '1234567890', 20000);
testing=# INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'PQR', 'Pune', '1234567890', 20000);
testing=# INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'XYZ', 'Mumbai', '1234567890', 35000);
testing=# INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (2, 'BBS', 'Mumbai', '1234567890', 45000);
testing=# INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (2, 'RBS', 'Delhi', '1234567890', 50000);

Output:

Wildcards in PostgreSQL output 1

Syntax

Please find below syntax of percentage (%) wildcard operator are as follows.

  • %xxx% : Select column_name1, …, column_nameN from table_name where column_name like ‘%xxx%’;
  • xxx% : Select column_name1, …, column_nameN from table_name where column_name like ‘xxx%’;
  • %xxx : Select column_name1, …, column_nameN from table_name where column_name like ‘%xxx’;
  • xxx : Select column_name1, …, column_nameN from table_name where column_name like ‘xxx’;

Please find below the parameter description of the above syntax are as follows.

  • Column1 to columnN – Column is used in the query to fetch data from the table.
  • Table name – Table name used in the query to fetch data from that table.
  • Percentage (%) – Percentage wildcard (%) operator used to fetch matching data from the table.
  • Like – Like operator is used to fetch data from the table.
  • Where – Where clause is used to fetch data from the table.

Example

Please find below an example of the percentage (%) wildcard operator is as follows.

1. Like ‘%xxxx%.’

The below example returns all rows that matching Pune text from the employee address from the employee table.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_address like '%Pune%';

Output:

Wildcards in PostgreSQL output 2

2. Like ‘%xx.’

The below example returns all rows which matching “un” text from the employee address from the employee table.

Which starts matching patterns from the right side.

There are no matching rows in the below query, so that it will return an empty set.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_address like '%un'

Output:

Wildcards in PostgreSQL output 3

3. Like ‘xx%.’

The below example returns all rows which matching “un” text from the employee address from the employee table.

Which starts matching patterns from the left side.

There are no matching rows in the below query, so that it will return an empty set.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_address like 'un%';

Output:

Wildcards in PostgreSQL output 4

4. Like ‘xxx.’

The below example returns all rows which matching “une” text from the employee address from the employee table.

Which starts matching patterns from both the side.

There is no matching in the below query, so that it will return an empty set.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_address like 'une';

Output:

output 5

Using the underscore (_) operator

Please find below syntax and example of underscore (_) wildcard operators.

Syntax

Please find below syntax of Underscore (_) wildcard operator are as follows.

  • _xxx_ : Select column_name1, …, column_nameN from table_name where column_name like ‘_xxx_’;
  • xxx_ : Select column_name1, …, column_nameN from table_name where column_name like ‘xxx_’;
  • _xxx : Select column_name1, …, column_nameN from table_name where column_name like ‘_xxx’;
  • xxx : Select column_name1, …, column_nameN from table_name where column_name like ‘xxx’;

Please find parameter description of above syntax are as follows.

  • Column1 to columnN – Column is used to fetch data from a query.
  • Table name – Table name used in the query to fetch data from a table.
  • Underscore (_) – Underscore wildcard (_) operator is used to fetch matching data from the table.
  • Like – Like operator is used to fetch data from the table.
  • Where – Where clause is used to fetch data from the table.

Example

Here are the following examples mention below

1. Like ‘_xxx_.’

Below is the example of like ‘_xxx_’ wildcard operator is as follows.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_salary::text like '_000_';

Output:

output 6

2. Like ‘_xxxx.’

Below is the example of like ‘_xxxx’ wildcard operator is as follows.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_salary::text like '_0000';

Output:

output 7

3. Like ‘x____.’

Below is the example of like ‘x____’ wildcard operator is as follows.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_salary::text like '2____';

Output:

output 8

4. Like ‘_____.’

Below is the example of like ‘_____’ wildcard operator is as follows.

testing=# select emp_id, emp_name, emp_address, emp_salary from employee where emp_salary::text like '_____';

Output:

output 9

Conclusion

Wildcard operator is essential in PostgreSQL. Basically, we used percentage (%) and underscore (_) wildcard operator to fetch matching rows from the table. There are many ways to retrieve data from the table using wildcard operator like (‘%xxx%’, ‘%xxx’, ‘xxx%’, ‘xxx’, ‘____’, ‘_xxx’, ‘xxx_’ and ‘_xx_’).

Recommended Articles

This is a guide to Wildcards in PostgreSQL. Here we discuss the explanation of all Wildcards in PostgreSQL along with the examples. You can also refer to our other related articles to learn more –

  1. PostgreSQL Architecture
  2. PostgreSQL String Functions
  3. PostgreSQL Interview Questions
  4. PostgreSQL Operators
  5. Guide to Oracle WILDCARDS
  6. PostgreSQL Like | Top 11 Examples
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more