w3resource logo


postgresql where

PostgreSQL WHERE

Secondary Nav

WHERE Clause

The PostgreSQL WHERE clause is used to control a PostgreSQL SELECT query, i.e. records or rows can be fetched according to an expression or some conditions supplied by the user.

In a PostgreSQL SELECT statement the FROM clause sends the rows into a consequent table temporarily, therefore each row of the resultant table is checked against the search condition. Rows which match the condition remains in the output table. The search condition required at least one column of the table specified after FROM clause.

Syntax

WHERE search_condition

Parameter

Parameter Description
search_condition A value expression. It returns a value of type boolean.

Create Table

 CREATE table actor(actor_id character(15), first_name character(15),last_name character(15),actor_age numeric(2), last_update time without time zone
 

Insert data

 INSERT INTO actor values ('ACT001','Alan','Nixon','15:22:43')INSERT INTO actor values ('ACT002','Zeon','Haus','07:15:14')INSERT INTO actor values ('ACT003','Ramsekhar','Alfanso','11:47:23')INSERT INTO actor values ('ACT004','McKord','Hill','09:36:45')
 

Table Structure

postgresql where sample table structure

Data available in the table

postgresql where sample table1

PostgreSQL WHERE example1

If we want to fetch all rows from the actor table which satisfy the condition last_name is 'Haus' the following PostgreSQL SELECT statement can be used.

SQL

 SELECT * FROM actorWHERE last_name='Haus' 
 

Output :

postgresql where sample table3

PHP with PostgreSQL WHERE Clause example 1

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8">
 <title>PostreSQL PHP WHERE Example 1</title>
<meta name="description" content="If we want to fetch all rows from the actor table which satisfy the condition last_name is 'Haus' the following PostgreSQL SELECT statement can be used.">
</head>
<body>
<h1>List of all actors whose last name is Haus</h1> 
<?php$db = pg_connect("host=localhost port=5432 dbname=w3r user=w3r_admin password=admin123");$result = pg_query($db,"SELECT * FROM actor WHERE last_name='Haus' ");
echo "<table>";while($row=pg_fetch_assoc($result)){echo "<tr>";
echo "<td align='center' width='200'>" . $row['actor_id'] . "</td>";
echo "<td align='center' width='200'>" . $row['first_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['last_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['actor_age'] . "</td>";
echo "<td align='center' width='200'>" . $row['last_update'] . "</td>";
echo "</tr>";}
echo "</table>";
?>
</body>
</html>

PostgreSQL WHERE example2

If we want to fetch all rows from the actor table which satisfy the condition actor_age is 26 the following PostgreSQL SELECT statement can be used.

 SELECT * FROM actor WHERE actor_age=26 
 

Output :

postgresql where sample table4

PHP with PostgreSQL WHERE Clause example 2

  <!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="utf-8">
  <title>PostreSQL PHP WHERE Example 2</title>
  <meta name="description" content="If we want to fetch all rows from the actor table which satisfy the condition actor_age is 26 the following PostgreSQL SELECT statement can be used.">
  </head>
  <body> 
  <h1>List of all actors those who are 26 </h1>
   <?php$db = pg_connect("host=localhost port=5432 dbname=w3r user=w3r_admin password=admin123");$result = pg_query($db,"SELECT * FROM actor WHERE actor_age=26 ");
   echo "<table>";while($row=pg_fetch_assoc($result)){echo "<tr>";
   echo "<td align='center' width='200'>" . $row['actor_id'] . "</td>";
   echo "<td align='center' width='200'>" . $row['first_name'] . "</td>";
   echo "<td align='center' width='200'>" . $row['last_name'] . "</td>";
   echo "<td align='center' width='200'>" . $row['actor_age'] . "</td>";
   echo "<td align='center' width='200'>" . $row['last_update'] . "</td>";
   echo "</tr>";}echo "</table>";?>
   </body>
   </html> 
   

PostgreSQL WHERE example3

If we want to fetch all rows from the actor table which satisfy the condition actor_age is more than 26 the following PostgreSQL SELECT statement can be used.

   SELECT *FROM actor WHERE actor_age>26
   

Output :

postgresql where sample table5

PHP with PostgreSQL WHERE clause example 3

   <!DOCTYPE html>
   <html lang="en">
   <head>
   <meta charset="utf-8">
   <title>PostreSQL PHP WHERE Example 2</title>
   <meta name="description" content="If we want to fetch all rows from the actor table which satisfy the condition actor_age is 26 the following PostgreSQL SELECT statement can be used.">
   </head>
   <body> 
   <h1>List of all actors those who are older than 26 </h1>
   <?php$db = pg_connect("host=localhost port=5432 dbname=w3r user=w3r_admin password=admin123");$result = pg_query($db,"SELECT * FROM actor WHERE actor_age>26 ");
   echo "<table>";while($row=pg_fetch_assoc($result)){echo "<tr>";
   echo "<td align='center' width='200'>" . $row['actor_id'] . "</td>";
   echo "<td align='center' width='200'>" . $row['first_name'] . "</td>";
   echo "<td align='center' width='200'>" . $row['last_name'] . "</td>";
   echo "<td align='center' width='200'>" . $row['actor_age'] . "</td>";
   echo "<td align='center' width='200'>" . $row['last_update'] . "</td>";
   echo "</tr>";}echo "</table>";?>
   </body>
   </html>
   


Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program