PostgreSQL WHERE NOT EQUAL Clause

Introduction

Making complex statements in queries when searching for data in tables is a common task. That’s because there are often times when you’ll want to run a second query on the first set of results that were returned. Although it can pose challenging, the good news is that you can do it seamlessly and fast with the handy PostgreSQL syntax WHERE NOT EQUAL clause. Learn how to drill down further and fine-tune the result response. Locate the data you need in record time.

Prerequisites

  • Download, install, configure, and then run PostgresSQL for your OS.

How to start PostgreSQL in LINUX

  • Use the following sudo start command give access permissions and start PostgreSQL on a LINUX OS:
sudo service postgresql start
  • Use the following status command to confirm PostgreSQL is running:
service postgresql status
  • You should see a result similar to this one below:
â— postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
Active: active (exited) since Thu 2019-08-01 14:51:20 PST; 36min ago
Process: 1230 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 1230 (code=exited, status=0/SUCCESS)

Aug 01 14:51:20 user-UX330UAK systemd[1]: Starting PostgreSQL RDBMS...
Aug 01 14:51:20 user-UX330UAK systemd[1]: Started PostgreSQL RDBMS.
lines 1-8/8 (END)

How to start PostgreSQL in Windows

For your Windows-based OS, follow these steps to verify PostgreSQL is running:

  1. Access the Control Panel
  2. Go to Administrative Tools
  3. Select Services
  4. Locate the PostgreSQL Server
  5. Start or restart the service if it stopped running.

Restarting PostgreSQL server in a Windows Machine

The purpose of the PostgreSQL Comparison Operator

To compare two assessments, use the PostgreSQL comparison operator symbol. If a NULL expression is present, it will return an UNKNOWN response to present invalidity. Otherwise, without NULL, you’ll get a TRUE or FALSE reply.

A simple example of using the PostgreSQL Where Clause

After you query using an SQL statement, use PostgreSQL Where to get selected data from those results. It’s based on the criteria that you set.

  • Here’s an easy example of how to the PostgreSQL WHERE clause script is written:
SELECT select_data
FROM target_table_name
WHERE criteria;

As shown in the above example, note that the WHERE clause is implemented after the SELECT operation’s clause FROM.

Specifically, rows returned in the result response from the SELECT operation are those that were filtered out by the WHERE clause. Furthermore, the rows that match the condition of true will show up in the result.

The three options for the condition of the WHERE results are: unknown, true, or false.

A list of comparison operators

It’s good to know that some statements help to refine the rows affected by comparison operators. For example, UPDATE, DELETE, and ORDER BY are statements that pair with the WHERE clause and there are others.

Let’s look at comparison operators now though, which are the refining symbols used with the WHERE clause for the data to be returned.

Here’s the entire list of comparison operators:

  • = — Equal.
  • != or < — Not Equal
  • >= — Greater Than or Equal
  • <= — Less Than or Equal
  • < — Less Than
  • > — Greater Than
  • AND — Logical Operator AND
  • OR — Logical Operator OR

Example of the PostgreSQL WHERE NOT EQUAL combination clause

Here’s how to use the NOT EQUAL comparison operator with the clause WHERE.

  • First, create a test database sample:
CREATE TABLE customer_info(
customer_id INT PRIMARY KEY NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
email TEXT NOT NULL
);
  • Add some test records like this:
INSERT INTO customer_info (customer_id, firstname, lastname, email)
VALUES
(1, 'Raymond', 'Elizario', 'relizario@example.com'),
(2, 'Vera', 'Dadula', 'vdadual@example.com'),
(3, 'Raymond', 'Escalona', 'rescalona@example.com');
  • Write some queries to test out on the database sample you just created:
SELECT * FROM customer_info WHERE lastname != 'Escalona';
  • Instead of the comparison operator used above, try <, another comparison operator of NOT EQUAL.
SELECT * FROM customer_info WHERE lastname != 'Escalona';
  • Take a closer look at the statement shown above as it doesn’t use the Less Than (<) comparison operator in that example. The operator NOT EQUAL (!=) is paired with the clause WHERE. The criteria states to find the customers that do not have the lastname of Escalona.

  • You should see a result similar to this:

testdatabase=# SELECT * FROM customer_info WHERE lastname != 'Escalona';
customer_id | firstname | lastname | email
-------------+-----------+----------+-----------------------
1 | Raymond | Elizario | relizario@example.com
2 | Vera | Dadula | vdadual@example.com
(2 ROWS)

An example of WHERE clause in PostgreSQL paired with NOT EQUAL comparison operator

  • You can use more than one comparison operator with the clause WHERE. Let’s try it with AND and the < NOT EQUAL comparison operators.
SELECT * FROM customer_info WHERE lastname < 'Elizario' AND firstname < 'Vera';
  • The example above illustrates a SELECT statement querying the customer_inf table. The WHERE clause is looking for first names in the table with specific criteria. It wants returned rows where Vera isn’t the firstname, therefore it must be unequal to that name. In addition, those rows where the Elizario isn’t the lastname, so it is also unequal to that name.

  • You should see a result close to this one:

customer_id | firstname | lastname | email
-------------+-----------+----------+-----------------------
3 | Raymond | Escalona | rescalona@example.com
(1 ROW)

Conclusion

This tutorial showed you how to use PostgreSQL WHERE NOT EQUAL clause. You learned how to use the WHERE statement along with SELECT and FROM to query tables. The list of comparison operators including AND, OR, and != not equal operators, give you the ability to create complex queries on the data in tables. Experiment with them in your projects to make your querying simpler, more stress-free, and straight to the point.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.