EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL ALL Operator
 

PostgreSQL ALL Operator

Updated May 25, 2023

PostgreSQL ALL

 

 

Introduction to PostgreSQL ALL Operator

Deleted: The PostgreSQL ALL operator compares a particular value with the result set returned by a sub-query. It is mandatory to define the sub-query after the ALL operator, which is surrounded by the parenthesis. The PostgreSQL ALL operator always returns the comparison result is true if the result of the subquery returns no rows. We can use the PostgreSQL ALL operator with comparison_operator such as =, !=, >, >=, <, and <= etc.

Watch our Demo Courses and Videos

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

Syntax:

comparison_operator  ALL (subquery)

Explanation:

Precede the operator with the following:

  • equal (=)
  • not equal (!=)
  • greater than(>)
  • greater than or equal to (>=)
  • less than (<)
  • less than or equal to (<=)

Place the subquery surrounded by parentheses after the PostgreSQL ALL operator.

How does it work in PostgreSQL?

Let’s consider the operator’s working as per the result returned by the sub-query.

1. The result of the sub-query returns some rows

  • If a value to be compared is greater than the largest value returned by the sub-query result, then the expression column > ALL (subquery) evaluates to true.
  • If a value to be compared is greater than or equal to the largest value returned by the sub-query result, then the expression column >= ALL (subquery) evaluates to true.
  • The expression column ALL (subquery) evaluates to true if the result of the subquery provides the smallest value that is less than the compared value.
  • If a value to be compared is less than or equal to the smallest value returned by the sub-query result, then the expression column <= ALL (subquery) evaluates to true.
  • When a value being compared matches any value returned by the result of the subquery, the expression column = ALL (subquery) evaluates to true.
  • The expression column != ALL (subquery) evaluates to true when a value to be compared does not match any value returned by the sub-query result.

2. The result of the subquery returns no rows

The PostgreSQL ALL operator always returns the comparison result as true.

Examples of PostgreSQL ALL Operator

Consider the two tables named COUNTRIES and CITIES to see examples of the PostgreSQL operator.

Code:

CREATE table COUNTRIES
(
country_id serial PRIMARY KEY,
country_name VARCHAR (256) NOT null,
last_updated DATE NULL
);
CREATE table  CITIES
(
CITY_id serial PRIMARY KEY,
country_id INT NOT NULL,
city_name VARCHAR (256) NOT NULL,
last_updated DATE null,
FOREIGN KEY (country_id) REFERENCES COUNTRIES (country_id)
);

Now insert some data in both tables.

Code:

INSERT INTO COUNTRIES (country_name,last_updated)
VALUES
('India','06-01-2020'),
('US','07-01-2020'),
('CHINA','08-01-2020');
INSERT INTO CITIES (country_id, city_name,last_updated)
VALUES
(1,'Pune','06-02-2020'),
(1,'Mumbai', '07-02-2020'),
(2,'New York', '08-02-2020'),
(2,'Los Angeles', '09-02-2020'),
(3,'Beijing ', '10-02-2020'),
(3,'Shanghai', '11-02-2020');

Illustrate both tables and data inserted with the following tables and snapshots.

Code:

select * from COUNTRIES;

Output:

PostgreSQL ALL 1

Code:

select * from CITIES;

Output:

PostgreSQL ALL 2

Example #1

Let’s consider the following example: we will get records from the cities table where the country’s id is ‘3’.

Code:

SELECT   city_id,   country_id,   city_name
FROM    cities
WHERE
country_id = ALL (
SELECT   country_id
FROM    cities
WHERE    country_id ='3');

Output:

PostgreSQL ALL 3

Example #2

Let’s consider the following example where we will get records from the cities table where the city’s name is not Pune, Mumbai, and New York.

Code:

SELECT   city_id,   country_id,   city_name
FROM    cities
WHERE
city_id != ALL (
SELECT   city_id
FROM    cities
WHERE    city_name ='Pune'or city_name ='Mumbai'or city_name ='New York');

Output:

where the name of the city is not Pune, Mumbai and New York

Example #3

Let’s consider another example where we will get records from the cities table where the city id is greater than the city id of Pune city.

Code:

SELECT   city_id,   country_id,   city_name
FROM    cities
where   city_id > ALL (
SELECT   city_id
FROM    cities
WHERE    city_name ='Pune');

Output:

 where the city id is greater than city id of Pune city

Example #4

Similar to the above example, we can get records from the cities table where the city id is less than the city id of New York City.

Code:

SELECT   city_id,   country_id,   city_name
FROM    cities
where   city_id < ALL (
SELECT   city_id
FROM    cities
WHERE    city_name ='New York');

Output:

records from cities table

Recommended Articles

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

  1. PostgreSQL OID
  2. PostgreSQL Clustered Index
  3. Limit Offset in PostgreSQL
  4. PostgreSQL group_concat
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
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW