Open In App

How to Select Rows with no Matching Entry in Another Table in SQLite?

In database management, selecting rows from one table that does not have matching entries in another table means returning the rows that are present in one table but do not have the same entry in any other table. This scenario often arises in various data validation and analysis processes.

In this article, We will learn about How to select rows with no matching entry in another table in SQLite by understanding various methods along with the examples and so on.



How to Select Rows with No Matching Entry in Another Table in SQLite?

When working with databases, it’s often necessary to query data from one table that has no corresponding entry in another table. In SQLite, this can be achieved using a combination of various methods. Let’s see some methods that help us to Select Rows with No Matching Entries in Another Table in SQLite as follows.

  1. Using LEFT JOIN with IS NULL
  2. Utilizing Subqueries
  3. Using NOT EXISTS

To understand How to select rows with no matching entry in another table in SQLite we need a table on which we will perform various operations and queries. Here we will consider a table called ORDERS and CUSTOMERS where the ORDERS table contains ORDERID, ORDERDATE. Here the CUSTOMERS table contains CUSTOMERSID, and CUSTOMERSNAME as Columns. Here the CUSTOMERSID which is a PRIMARY KEY acts as a Foreign Key for ORDERs TABLE.



Orders table

CUSTOMERS TABLE:

Customer Table

These two tables “ORDERS” and “CUSTOMERS” are taken as an example for explaining the concept of select rows with no matching entry in another table.

1. Using LEFT JOIN with IS NULL

SELECT Customers.CustomerID, Customers.CustomerName
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.CustomerID IS NULL;

Output:

Output of the row that is present in customers table but not in orders table

From the two table given above, there is only one row (i.e. row with CUSTOMERID=3) with no matching entry in another table.

2. Utilizing Subqueries


SELECT CustomerID, CustomerName
FROM Customers
WHERE CustomerID NOT IN (SELECT DISTINCT CustomerID FROM Orders);

Output:

Output of the row that is present in customers table but not in orders table

From the two table given above, there is only one row (i.e. row with CUSTOMERID=3) with no matching entry in another table.

3. Using NOT EXISTS

SELECT CustomerID, CustomerName
FROM Customers
WHERE NOT EXISTS (SELECT 1 FROM Orders WHERE Customers.CustomerID = Orders.CustomerID);

Output:

Output of the row that is present in customers table but not in orders table

From the two table given above, there is only one row (i.e. row with CUSTOMERID=3) with no matching entry in another table.

Conclusion

Overall, selecting rows with no matching entry in another table is a common task in database querying, essential for various data analysis and validation processes. By mastering techniques such as LEFT JOIN, subqueries, or NOT IN/NOT EXISTS, you can efficiently handle such scenarios in SQLite databases.


Article Tags :